使用Apache POI生成Excel下拉列表时的限制
2022-09-03 09:20:21
我正在尝试生成一个带有一些验证的excel文件,我已经阅读了poi开发指南来实现它。在实现过程中,我得到了一个异常()。POI将所有下拉选项连接到“0”去利值字符串中,并检查其长度并给我例外。:(
我使用的是最新版本的 POI 3.8 beta 5。
我的代码是:String literals in formulas can't be bigger than 255 characters ASCII
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short) 0);
//CellRangeAddressList from org.apache.poi.ss.util package
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(getCountries());
DataValidation dataValidation = new HSSFDataValidation(addressList, dvConstraint);
dataValidation.setSuppressDropDownArrow(false);
sheet.addValidationData(dataValidation);
FileOutputStream fileOut = new FileOutputStream("c:\\test.xls");
wb.write(fileOut);
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
之后,我尝试了XSSFWorkBook,代码如下:
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("new sheet");
DataValidationHelper validationHelper = new XSSFDataValidationHelper(sheet);
DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(getCountries());
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
DataValidation dataValidation = validationHelper.createValidation(constraint, addressList);
dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
dataValidation.setSuppressDropDownArrow(true);
sheet.addValidationData(dataValidation);
FileOutputStream fileOut = new FileOutputStream("c:\\test.xlsx");
不幸的是,这样的结果是逗号在一个单元格中减去长字符串,没有成功:
但是在Excel中手动,我可以创建带有此长国家/地区列表的下拉单元格。
有没有办法生成带有长字符串的下拉列表,或者API不支持?