POI操作EXCEL增加下拉框

系列文章目录



POI操作EXCEL增加下拉框

有时候通过excel将数据批量导入到系统,而业务操作人员对于一些列不想手动输入,而是采用下拉框的方式来进行选择

采用隐藏sheet页的方式来进行操作

String sheetName = "supplier_hidden_sheet";

HSSFSheet supplierSheet = workbook.createSheet(sheetName);//隐藏的sheet,用于存放下拉框的限定值
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);
// 这是表示从隐藏sheet页的哪列哪行到哪列哪行  如该格式 supplier_hidden_sheet!$A$1:$A$73 
String cell = """+sheetName +"!$"+col+"$1:$"+col+"$"+suppliers.size()+""";
// 引用ShtDictionary 的单元格
DVConstraint constraint = DVConstraint.createFormulaListConstraint("INDIRECT("+cell+ ")");
// 数据检验为该格式  =INDIRECT("supplier_hidden_sheet!$A$1:$A$73")
HSSFDataValidation dataValidate = new HSSFDataValidation(regions, constraint);
sheet.addValidationData(dataValidate);
workbook.setSheetHidden(1,true);
经验分享 程序员 微信小程序 职场和发展