POI操作EXCEL增加下拉框
依赖
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency>
|
有时候通过excel将数据批量导入到系统,而业务操作人员对于一些列不想手动输入,而是采用下拉框的方式来进行选择
采用隐藏sheet页的方式来进行操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| String sheetName = "supplier_hidden_sheet";
HSSFSheet supplierSheet = workbook.createSheet(sheetName); int count = 0; for(String supplierName : suppliers){ supplierSheet.createRow(count++).createCell(0).setCellValue(supplierName); } String col = "A";
CellRangeAddressList regions = new CellRangeAddressList(1, dataList.size(), supplierColumn, supplierColumn);
String cell = "\""+sheetName +"!$"+col+"$1:$"+col+"$"+suppliers.size()+"\"";
DVConstraint constraint = DVConstraint.createFormulaListConstraint("INDIRECT("+cell+ ")");
HSSFDataValidation dataValidate = new HSSFDataValidation(regions, constraint); sheet.addValidationData(dataValidate); workbook.setSheetHidden(1,true);
|
可以在EXCEL中的数据有效性看到数据来源