使用jxl生成两级动态下拉菜单

yzyz0812 2014-09-24 02:24:00


我有一个excel表,想用jxl读取人名那一列和商店名称的那一列,制作一个动态的下拉菜单,但是人名和商店名都是会变化的,这样怎么实现呢?下拉菜单想实现类似于淘宝填收货地址那样的样式
...全文
183 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yzyz0812 2014-09-24
  • 打赏
  • 举报
回复
我需要的菜单是在Java中做,不是在Excel里
ghx287524027 2014-09-24
  • 打赏
  • 举报
回复
目前2003和2007的下拉表其实只是到32766个,范围再高也没有用了。 方法需要sheet,要做下拉的单元格,以及打算放置数据的单元格,还有数据了。 返回的是截止点的下个单元格的位置,可以为下个单元格放置数据。 另外,jxl不支持2007,但poi支持而且做这个也简单
ghx287524027 2014-09-24
  • 打赏
  • 举报
回复
/** * @param sheet * @param target 要设置数据验证性的单元格位置 * @param current 要存放验证性数据的单元格位置 * @param step * @param data * @return */ private static int[] buildDataValidationCell(WritableSheet sheet, int[] target, int[] current, List<String> data) { try { if (data.size() == 0) return current; List<String> strings = new ArrayList<String>(); for (String d : data) { if (d!= null&& !"".equals(d.trim())) strings.add(d); } if(strings.size()>65535) throw new RuntimeException("excel2003单列上限,数据验证只能使用单列或单行"); //excel2003上限是65535,所以隐藏的行数超过5w就换另外一列, //列上限是255。暂时不考虑行列都到上限的情况 if (current[1] > 50000 ||(current[1] > 50000 && strings.size()>15535)) { current[0] = current[0] + 1; current[1] = 0; } //构建下拉数据范围 buildDataValidationRange(sheet, target, current, strings); //设置隐藏 CellView cv = new CellView(); cv.setHidden(true); sheet.setColumnView(current[0], cv); //填充数据 for (String s : strings) { Label b = new Label(current[0], current[1], s); sheet.addCell(b); current[1] += 1; } } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } return current; } //构建下拉数据范围 private static void buildDataValidationRange(WritableSheet sheet, int[] target, int[] current, List<String> strings) throws WriteException, RowsExceededException { WritableCellFeatures wcf = null; Label targetCell = new Label(target[0], target[1], strings.get(0));//数据验证初始cell wcf = new WritableCellFeatures(); StringBuffer buff = new StringBuffer();//数据验证范围 CellReferenceHelper.getCellReference(current[0] , true,current[1], true, buff);//起点 buff.append(":"); CellReferenceHelper.getCellReference(current[0] , true,current[1] + strings.size() - 1, true, buff);//终点 wcf.setDataValidationRange(buff.toString());//设置数据验证性 targetCell.setCellFeatures(wcf); sheet.addCell(targetCell); }

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧