Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When trying to copy over cell styles from an old Excel file, after writing 32357 cells, the font disappears and when finding the root of the problem, I came across the error that it's throwing this:

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)

and to copy over the CellStyle from the old Excel file, I did this:

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

But I don't understand why it's doing that because when I do rows.get(2506).getCell(2).getCellStyle().getFont() it returns an XSSFFont object (the XSSFFont that style.getFont() should be returning) and doesn't throw an Exception.

I would really appreciate if someone could help me with this problem.

like image 462
silverAndroid Avatar asked Oct 25 '25 03:10

silverAndroid


1 Answers

Apache POI is limited to 32767 Fonts per workbook

You will need to find a way to reuse the fonts and cell styles.

like image 195
Phill Treddenick Avatar answered Oct 26 '25 18:10

Phill Treddenick