java POI 利用隐藏sheet实现导出下拉数据太多为空
java POI 利用隐藏sheet实现导出下拉数据太多为空:
1.创建隐藏sheet
// 创建隐藏sheet
List<String> phaseNameList = getPhaseNames(); if (CollectionUtils.isNotEmpty(phaseNameList)) { wb.createSheet("销售阶段"); wb.setSheetHidden(wb.getSheetIndex(wb.getSheet("销售阶段")), true); Sheet sheet = wb.getSheet("销售阶段"); sheet.createRow(0);//sheet.getLastRowNum无法区分 有一行和没有 所以这里先建一行 String colName = "A"; int colNum = Integer.valueOf(colName.charAt(0)) - 65; Row row; //创建数据行 sheet.setColumnWidth(colNum, 4000); //设置每列的列宽 for (int j = 0; j < phaseNameList.size(); j++) { if (sheet.getLastRowNum() < j) { row = sheet.createRow(j); //创建数据行 } else { row = sheet.getRow(j); } //设置对应单元格的值 row.createCell(colNum).setCellValue(phaseNameList.get(j)); } }
2.将隐藏sheet的数据绑定在单元格
String colName = "A"; List<String> phaseNames = new ArrayList<>(Arrays.asList(getPhaseNames())); int row = CollectionUtils.isEmpty(phaseNames) ? 1 : phaseNames.size(); String strFormula = "销售阶段!$" + colName + "$1:$" + colName + "$" + row; XSSFDataValidationConstraint constraint = new XSSFDataValidationConstraint(DataValidationConstraint.ValidationType.LIST,strFormula); // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列 CellRangeAddressList regions = new CellRangeAddressList(0, 65535, columnIndex, columnIndex); // 数据有效性对象 DataValidationHelper help = new XSSFDataValidationHelper((XSSFSheet) xssfSheet); DataValidation validation = help.createValidation(constraint, regions); xssfSheet.addValidationData(validation);
随笔看心情