POI操作EXCEL增加下拉框


文章目录


POI操作EXCEL增加下拉框

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

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

java 复制代码
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);

可以在EXCEL中的数据有效性看到数据来源

参考文献

相关推荐
Zzq_Fighting14 小时前
Excel批量匹配数据
excel
E_ICEBLUE15 小时前
Python 实现 Excel 转 Markdown,支持工作表、单元格区域和批量处理
python·excel·markdown·格式转换
拆房老料15 小时前
ONLYOFFICE 集群部署实战:9.3 多实例方案与 9.4 架构变化
开源·word·excel·开源软件·ppt
ms365copilot17 小时前
Excel Copilot挖掘数据洞察,不用精通函数也能做数据分析 (1)
数据分析·excel·copilot
babe小鑫17 小时前
精算学专业秋招:Excel、SQL、R和Python应该先练什么
sql·r语言·excel
医疗信息化王工1 天前
DataForge:基于 Python 的数据库批量导出 Excel 工具——从架构到部署的全流程实战
数据库·python·excel
1314lay_10071 天前
通过Sql Server创建EXCEL文件:master..xp_cmdshell
数据库·excel
2601_962300812 天前
Python + Requests + Pytest + Excel + Allure:接口自动化测试项目实战
python·excel·pytest·allure·requests
ms365copilot2 天前
Excel Copilot一键美化图表,无需手动调格式
excel·copilot·outlook
2501_933670792 天前
内部审计校招的数据化趋势:SQL、Excel、Power BI和证书选择
数据库·sql·excel