使用 Apache POI 生成 Excel 下拉列表时的限制
我正在尝试生成带有一些验证的 Excel 文件,我已阅读 poi开发指南来实现它。在实现过程中,我遇到了一个异常(公式中的字符串文字不能大于 255 个 ASCII 字符
)。 POI 将所有下拉选项连接成“0”分隔字符串并检查其长度并给我例外。 :(
我正在使用最新版本的 POI 3.8 beta 5。
我的代码是:
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 不能支持?
I'm trying to generate an excel file with some validations, I've read the poi dev guides for implementing it. During implementation, I got an exception (String literals in formulas can't be bigger than 255 characters ASCII
). POI concatenates all drop down options into '0' deliminated string and checking its length and giving me exception. :(
I'm using latest version of POI 3.8 beta 5.
And my code is:
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();
}
After that I have tried with XSSFWorkBook with this code:
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");
Unfortunately, no success with such result which is comma delimenated long string in one cell:
But manually in excel, I can create dropdown cells with this long country list.
Is there any way generate dropdown with long strings, or API does not support?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我了解,Excel本身不允许输入超过255个字符的验证范围字符串,这不是POI限制。现在我正在使用 命名范围和命名单元并且它工作正常为我。因此,我必须将验证范围标记放在另一个工作表中(隐藏),并从真实工作表中引用所需的单元格范围。这是我的工作代码:
I understood it, Excel itself does not allow entering validation range string more than 255 characters, this was not POI limitation. And now I'm using Named Ranges and Named Cells and it's working properly for me. So I had to put my validation range tokens in another sheet(made hidden) and I referenced desired cell ranges from my real sheet. Here is my working code:
Õzbek 提供的强大解决方案只需稍作修改即可与 NPOI 完美配合(在 .NET 上使用 C#)。
这是我的代码,为 C# 编码人员提供方便。它接受工作表和位置元素作为输入,并且当它们按列排列时,还可以处理多个下拉列表。
The formidable solution provided by Õzbek needs only slight modification to work flawlessly with NPOI (using C# on .NET).
Here's my code, provided as convenience for C# coders. It takes a sheet and positional elements as input and can also handle multiple dropdown, when they are arranged in columns.
上面的代码工作正常。但是,如果我使用 XSSF 类而不是 HSSF,则无法显示线程中的异常
要使用 XSSF 接口创建下拉列表,我在
使用 apache poi XSSF 接口创建下拉列表
Above code works fine. But if I use XSSF classes instead of HSSF, it fails saying Exception in thread
To create dropdown list using XSSF interfaces I got the solution at
create dropdown list using apache poi XSSF interfaces
这就是我使用 XSSF 模型使用 FormulaListConstraint 的方式
This is how I used the FormulaListConstraint using the XSSF model