使用jxl生成两级动态下拉菜单 Javajxlexcel 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /** * @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); } 目前2003和2007的下拉表其实只是到32766个,范围再高也没有用了。 方法需要sheet,要做下拉的单元格,以及打算放置数据的单元格,还有数据了。 返回的是截止点的下个单元格的位置,可以为下个单元格放置数据。 另外,jxl不支持2007,但poi支持而且做这个也简单 我需要的菜单是在Java中做,不是在Excel里 Reentrantlock 跟 Semaphore 有什么区别呢 两个指向null的字符串为什么会相等? 关于流的问题,很头痛 log4j中的方法 一个eclipse的简单问题 有关 java 异常的 异常表现 谢谢sundeveloper的解答,请进来接分 InstallAnyway发布java时遇到的问题,大侠帮忙!急! 空格问题 请大家看一看 更新cache,这样写好吗? linux下面运行java的问题---求助
* @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);
}
方法需要sheet,要做下拉的单元格,以及打算放置数据的单元格,还有数据了。
返回的是截止点的下个单元格的位置,可以为下个单元格放置数据。 另外,jxl不支持2007,但poi支持而且做这个也简单