|
|
|
@ -73,11 +73,20 @@ public class PoiUtil {
|
|
|
|
|
public static void addValidation(Workbook workbook, int sheetIdx, String options, int firstRow, int lastRow, int firstCol, int lastCol) {
|
|
|
|
|
String[] optionArray = options.split(",");
|
|
|
|
|
Sheet sheet = workbook.getSheetAt(sheetIdx);
|
|
|
|
|
|
|
|
|
|
// 删除已有的校验
|
|
|
|
|
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
|
|
|
|
|
DataValidationConstraint dvConstraint = dvHelper.createCustomConstraint("DELETE_ALL");
|
|
|
|
|
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); // 设置一个范围,例如整个表格范围
|
|
|
|
|
DataValidation validation = dvHelper.createValidation(dvConstraint, addressList);
|
|
|
|
|
validation.setSuppressDropDownArrow(true);
|
|
|
|
|
sheet.addValidationData(validation);
|
|
|
|
|
|
|
|
|
|
// 创建数据验证对象
|
|
|
|
|
DataValidationHelper validationHelper = sheet.getDataValidationHelper();
|
|
|
|
|
DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(optionArray);
|
|
|
|
|
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol); // 指定单元格范围
|
|
|
|
|
DataValidation validation = validationHelper.createValidation(constraint, addressList);
|
|
|
|
|
addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol); // 指定单元格范围
|
|
|
|
|
validation = validationHelper.createValidation(constraint, addressList);
|
|
|
|
|
// 应用数据验证到单元格
|
|
|
|
|
sheet.addValidationData(validation);
|
|
|
|
|
}
|
|
|
|
|