jxl 生成excel下拉框

头段时间要做个excel导出。。其中一列要求做下拉列表的样子。
目前2003和2007的下拉表其实只是到32766个,范围再高也没有用了。
方法需要sheet,要做下拉的单元格,以及打算放置数据的单元格,还有数据了。
返回的是截止点的下个单元格的位置,可以为下个单元格放置数据。

另外,这真是个蛋疼的东西。。jxl不支持2007。。poi支持而且做这个也简单,但是这个项目一直是jxl。。哎。。。

	/**
* @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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值