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);
	}

你可能感兴趣的:(Excel,WCF)