POI创建Excel下拉列表框来设置表格数据有效性,这里需要使用几个重要的对象,关系依赖图如下
具体代码可以参考如下
// 创建工作薄
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("下拉列表框");
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
.createExplicitListConstraint(new String[] { "男", "女" });
// 设置区域边界
CellRangeAddressList addressList = new CellRangeAddressList(0, 10, 0, 0);
XSSFDataValidation validation = (XSSFDataValidation) dvHelper
.createValidation(dvConstraint, addressList);
// 输入非法数据时,弹窗警告框
validation.setShowErrorBox(true);
// 设置提示框
validation.createPromptBox("温馨提示", "请选择性别!!!");
validation.setShowPromptBox(true);
sheet.addValidationData(validation);
FileOutputStream out = new FileOutputStream(filePath);
workbook.write(out);
out.close();