使用 Apache POI 在 Excel 中的一定数量的单元格后字体消失

Font disappears after a certain amount of cells in Excel using Apache POI

当尝试从旧的 Excel 文件复制单元格样式时,在写入 32357 个单元格后,字体消失了,当找到问题的根源时,我遇到了它抛出的错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -32768
    at java.util.ArrayList.elementData(ArrayList.java:400)
    at java.util.ArrayList.get(ArrayList.java:413)
    at org.apache.poi.xssf.model.StylesTable.getFontAt(StylesTable.java:210)
    at org.apache.poi.xssf.usermodel.XSSFCellStyle.getFont(XSSFCellStyle.java:561)
    at Compare.writeRows(Compare.java:415)
    at Compare.main(Compare.java:44)

为了从旧的 Excel 文件中复制 CellStyle,我这样做了:

XSSFCellStyle style = workbook.createCellStyle();
style.cloneStyleFrom(oldCell.getCellStyle());
newCell.setCellStyle(style);
style.getFont(); //throws error here
newCell.setCellType(oldCell.getCellType());

但我不明白为什么要这样做,因为当我这样做时 rows.get(2506).getCell(2).getCellStyle().getFont() 它 returns 一个 XSSFFont 对象(XSSFFontstyle.getFont()应该返回)并且不会抛出 Exception.

如果有人能帮助我解决这个问题,我将不胜感激。

Apache POI is limited to 32767 Fonts per workbook

您需要找到重用字体和单元格样式的方法。