将Apache POI中具有最大长度的文本写入单元格

时间:2022-02-23 18:01:02

For an application I have to test if a text will fit into a cell in Apache POI.

对于应用程序,我必须测试文本是否适合Apache POI中的单元格。

To test some things quickly, I wrote a small demo application, where I wrote the maximal allowed amount of characters to a cell. I encountered that the number of characters I could write to a cell

为了快速测试一些东西,我写了一个小的演示应用程序,在那里我向单元格写了最大允许的字符数。我遇到了我可以写入单元格的字符数

So, this is the demo application:

所以,这是演示应用程序:

Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("Test");

for (int idx = 0; idx < 5; idx++) {
    Row row = sheet.createRow(idx);
    Cell cell = row.createCell(0);
    cell.setCellValue(StringUtils.repeat("-", wb.getSpreadsheetVersion().getMaxTextLength()));
}

FileOutputStream out = new FileOutputStream("test.xls");
wb.write(out);

out.close();
wb.close();

But when opening the created excel file, I failed to read the cells I've written to, prompting that the cell could only contain between 1 and 255 characters. But this is different than what I get from "wb.getSpreadsheetVersion().getMaxTextLength()". Does some characters in the text require some special treatment? Note that when writing more than 255 times "-" directly into excel, I do not have any problems.

但是当打开创建的excel文件时,我无法读取我写入的单元格,提示单元格只能包含1到255个字符。但这与我从“wb.getSpreadsheetVersion()。getMaxTextLength()”得到的不同。文中的某些字符是否需要一些特殊处理?请注意,当将超过255次“ - ”直接写入excel时,我没有任何问题。

Using "*" instead of "-" works as expected.

使用“*”而不是“ - ”按预期工作。

1 个解决方案

#1


2  

  • Limitation of characters in Excel cell - 32767 (2^15-1)
  • Excel单元格中字符的限制 - 32767(2 ^ 15-1)

  • Limitation of characters in Excel cell with formula - 255 (2^8-1)
  • Excel公式中字符的限制公式 - 255(2 ^ 8-1)

Thus, if your first character is = or + you go for the lower limit.

因此,如果您的第一个字符是=或+,则为下限。

#1


2  

  • Limitation of characters in Excel cell - 32767 (2^15-1)
  • Excel单元格中字符的限制 - 32767(2 ^ 15-1)

  • Limitation of characters in Excel cell with formula - 255 (2^8-1)
  • Excel公式中字符的限制公式 - 255(2 ^ 8-1)

Thus, if your first character is = or + you go for the lower limit.

因此,如果您的第一个字符是=或+,则为下限。