Socket Exception while generating bulk excel file using Apache Poi

故事扮演 提交于 2019-12-11 08:16:19

问题


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();

回答1:


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



来源:https://stackoverflow.com/questions/15424978/socket-exception-while-generating-bulk-excel-file-using-apache-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!