I am getting SocketConnectionException
while generating Excel file for bulk data (more than 0.5 million records).
The code of my web application writes to `outputstream. Here's a snippet of code:
while (sr.next()) {
counter++; //advance counter
view = (DataClass) sr.get(0);
try {
//writing fields values for Activity Report file
reportService.writeExcelFieldsValue(rowCounter,sheet,view,user,exportedFields);
rowCounter++;
} catch (Exception e) {
throw new RuntimeException(e);
}
if (counter == chunkSize || sr.isLast()) {
counter = 0; //reset counter
//Clear the session after a chunk and before next chunk
getSession().clear();
}
}
wb.write(bos);
bos.flush();
POI provides a low-memory footprint SXSSF API built on top of XSSF.
SXSSF is an API-compatible streaming extension of XSSF to be used when very large spreadsheets have to be produced, and heap space is limited. SXSSF achieves its low memory footprint by limiting access to the rows that are within a sliding window, while XSSF gives access to all rows in the document. Older rows that are no longer in the window become inaccessible, as they are written to the disk.
In auto-flush mode the size of the access window can be specified, to hold a certain number of rows in memory. When that value is reached, the creation of an additional row causes the row with the lowest index to to be removed from the access window and written to disk. Or, the window size can be set to grow dynamically; it can be trimmed periodically by an explicit call to flushRows(int keepRows) as needed.
Due to the streaming nature of the implementation, there are the following limitations when compared to XSSF:
- Only a limited number of rows are accessible at a point in time.
- Sheet.clone() is not supported. Formula evaluation is not supported
- I think this link might help you
I think you'll want to use the XSSF EventModel code. See the POI documentation to get started. or For more details, click here
Also this link may help you.
Writing a large resultset to an Excel file using POI