How to Edit existing large excel file with SXSSF Streaming api

丶灬走出姿态 提交于 2020-07-23 07:37:25

问题


I have a large .xlsx excel sheet with 400,000 rows. I want to read and write in this existing workbook.

When i tried to read it in java with Apache poi, with following code:

FileInputStream fileInputStream = new FileInputStream(new File(excelPath));
Workbook wb = new XSSFWorkbook(fileInputStream);

Second line of this code takes RAM upto 5gb.

Apache POI has given a SXSSF Streaming API to handle large Excel file.

http://poi.apache.org/components/spreadsheet/how-to.html#sxssf

Now, when I instantiate SXSSF workbook with constructor without any parameter, it creates new Workbook and does not persist existing data of workbook. And other constructor of SXSSF workbook takes instance of XSSF workbook. And the problem starts arise here. When i made instance of XSSF workbook for my excel file, RAM goes high and OUTOFMEMORY exception thrown.

Is there any way to do read and write opration on existing Large excel workbook with more then 400,000 rows.


回答1:


Look at the bottom of the "Overview" page of POI. It has this table:

Spreadsheet API Feature Summary

The last column shows that SXSSF can only write file, not read them.

To read files, streaming, the third column shows that you need to use the XSSF eventmodel.

So, to modify a file, streaming, so as t not use a lot of memory, you need to read with one API and writing a new file with another API.



来源:https://stackoverflow.com/questions/58500415/how-to-edit-existing-large-excel-file-with-sxssf-streaming-api

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