Java实现后端⽣成excel表格模板--下拉框实现实现⽅法:
//创建下拉框
private static void creatDropDownList(Sheet taskInfoSheet, DataValidationHelper helper, String[] list,
Integer firstRow, Integer lastRow, Integer firstCol, Integer lastCol){
//获取需要⽣成下拉框的格
CellRangeAddressList addressList =new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
//设置下拉框数据
DataValidationConstraint constraint = ateExplicitListConstraint(list);
DataValidation dataValidation = ateValidation(constraint, addressList);
//处理Excel兼容性问题
if(dataValidation instanceof XSSFDataValidation){
dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowErrorBox(true);
}else{
dataValidation.setSuppressDropDownArrow(false);
}
taskInfoSheet.addValidationData(dataValidation);
}
使⽤:
//创建HSSFWorkbook对象(excel的⽂档对象)
XSSFWorkbook wb =new XSSFWorkbook();
//建⽴新的sheet对象(excel的表单)
XSSFSheet sheet = wb.createSheet("⼈员表");
DataValidationHelper DataValidationHelper();
String[] dlData ={"A","B","C","D","E","F","G","H"};
//参数:表单对象,数据验证对象,下拉框数据,在第⼏⾏到第⼏⾏带有下拉框,第⼏列到第⼏列有下拉框
数据验证怎么设置下拉菜单creatDropDownList(sheet,helper,dlData,1,10,6,6);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论