I need to auto-fit all rows in large (30k+ rows) xlsx file.
The following code via apache poi works on small files, but goes out with OutOfMemoryError
on large ones:
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
row.setHeight((short) -1);
}
workbook.write(outputStream);
Update: Unfortunately, increasing heap size is not an option - OutOfMemoryError
appears at -Xmx1024m
and 30k rows is not an upper limit.
Try using the event API. See Event API (HSSF only) and XSSF and SAX (Event API) in the POI documentation for details. A couple of quotes from that page:
HSSF:
XSSF:
For output, one possible approach is described in the blog post Streaming xlsx files. (Basically, use XSSF to generate a container XML file, then stream the actual content as plain text into the appropriate xml part of the xlsx zip archive.)