Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutosizeColumns on SXSSFWorkbook

Is it possible to autoSizeColumns on a streaming SXSSFWorkbook? I implemented an export functionality to export a list of objects to excel. At first I used the XSSFWorkbook (not streaming) and after all the cells were created, I autosized all the columns, resulting in a nice excel file.

For performance issues we wanted to change the workbook to the streaming version, but this resulted in a NullPointer at org.apache.poi.ss.util.SheetUtil.getCellWidth.

Is it possible to call autoSizeColumns for a SXSSFWorkbook?

Im using poi-ooxml 3.9, but I have the same issue in 3.8.

like image 903
sterym Avatar asked Jan 24 '13 08:01

sterym


People also ask

How do you autosize a column in SXSSFWorkbook?

Call trackAllColumnsForAutoSizing() on the SXSSFSheet object before calling autoSizeColumn(columnIndex) and it will be fixed. NOTE: As per documentation, track required columns before writing one or more rows to the sheet.

What is SXSSFWorkbook?

public class SXSSFWorkbook extends java.lang.Object implements Workbook. Streaming version of XSSFWorkbook implementing the "BigGridDemo" strategy. This allows to write very large files without running out of memory as only a configurable portion of the rows are kept in memory at any one time.


2 Answers

You need to make sure every cell has a value.

We use the following code to set a string value to a cell:

Cell c = row.createCell(i);
c.setCellValue(text == null ? "" : text );

** Cell should never be null values else it throws NullPointerException. Hence set the value as shown above.

Thanks a lot, this helped!!

like image 199
cremersstijn Avatar answered Oct 12 '22 01:10

cremersstijn


Use sheet.isColumnTrackedForAutoSizing(0); for first and subsequently used for other column, i have faced exception whenever code executed autoSizeColumn(0) get executed. by using above code i have resolved the issue and it's good to expand the column width too based on the text.

like image 45
Virendra khade Avatar answered Oct 12 '22 02:10

Virendra khade