//在名称管理器中创建名称
public HSSFName createName(HSSFWorkbook wb, String name, String expression){
HSSFName refer = wb.createName();
refer.setRefersToFormula(expression);
refer.setNameName(name);
return refer;
}
// 使用已定义的数据源方式设置一个数据验证(选项较多的情况)
public DataValidation getDataValidation(String name, int firstRow, int endRow, int firstCol, int endCol){
//加载下拉列表内容
DVConstraint constraint = DVConstraint.createFormulaListConstraint(name);
//设置数据有效性加载在哪个单元格上。
CellRangeAddressList regions=new CellRangeAddressList(firstRow,endRow,firstCol,endCol);
//数据有效性对象
DataValidation data_validation_list = new HSSFDataValidation(regions,constraint);
return data_validation_list;
}
//使用自定义数据源(下拉选项少的情况)
public DataValidation getDataValidation( int firstRow, int endRow, int firstCol, int endCol){
//加载下拉列表内容
String[] list={"合格","不合格"};
DVConstraint constraint = DVConstraint.createExplicitListConstraint(list);
//设置数据有效性加载在哪个单元格上。
CellRangeAddressList regions=new CellRangeAddressList(firstRow,endRow,firstCol,endCol);
//数据有效性对象
DataValidation data_validation_list = new HSSFDataValidation(regions,constraint);
return data_validation_list;
}
//调用以上方法
//为地市下拉框创建名称
POIExcelUtil.createName(wb, "city", "数据字典!$B$2:$B$"+rowcity);
//为地市下拉框设置数据有效性
DataValidation dataValidation = POIExcelUtil.setDataValidation("city", 2, row-1, 6, 6);
sheet.addValidationData(dataValidation);
----------------------------------------------------------------------------------------------------------------------------------------
//设置隐藏页标志
workbook.setSheetHidden(workbook.getSheetIndex(hideSheetName), true);