/**
* excel添加下拉數(shù)據(jù)校驗
* @param sheet 哪個 sheet 頁添加校驗
* @param dataSource 數(shù)據(jù)源數(shù)組
* @param col 第幾列校驗(0開始)
* @return
*/
public static DataValidation createDataValidation(Sheet sheet, String[] dataSource, int col) {
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 65535, col, col);
DataValidationHelper helper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = helper.createExplicitListConstraint(dataSource);
DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
//處理Excel兼容性問題
if (dataValidation instanceof XSSFDataValidation) {
dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowErrorBox(true);
} else {
dataValidation.setSuppressDropDownArrow(false);
}
dataValidation.setEmptyCellAllowed(true);
dataValidation.setShowPromptBox(true);
dataValidation.createPromptBox("提示", "只能選擇下拉框里面的數(shù)據(jù)");
return dataValidation;
}
使用方式
sheet.addValidationData(ExcelUtil.createDataValidation(sheet, datas, columnFields.indexOf("staff_duty")));