使用 Apache POI 进行低内存写入/读取 [英] Low memory writing/reading with Apache POI
问题描述
我正在尝试编写一个非常大的 XLSX 文件(超过 400 万个单元格),但遇到了一些内存问题.
I'm trying to write a pretty large XLSX file (4M+ cells) and I'm having some memory issues.
我无法使用 SXSSF,因为我还需要读取模板中的现有单元格.
I can't use SXSSF since I also need to read the existing cells in the template.
我可以做些什么来减少内存占用?
也许结合流式阅读和流式写作?
Is there anything I can do to reduce the memory footprint?
Perhaps combine streaming reading and streaming writing?
推荐答案
要处理低内存的大数据,最好的,我认为唯一的选择是 SXSSF api-s.如果您需要读取现有单元格的一些数据,我假设您不需要同时需要整个 4M+.在这种基于您的应用程序要求的情况下,您可以自己处理窗口大小并仅将特定时间所需的数据量保留在内存中.您可以首先查看以下示例:http://poi.apache.org/spreadsheet/how-to.html#sxssf
To handle large data with low memory, the best and I think the only option is SXSSF api-s. If you need to read some data of the existing cells, I assume you do not need the entire 4M+ at the same time. In such a case based on your application requirement, you can handle the window size yourself and keep in memory only the amount of data you need at a particular time. You can start by looking at the example at : http://poi.apache.org/spreadsheet/how-to.html#sxssf
有点像
SXSSFWorkbook wb = new SXSSFWorkbook(-1); // turn off auto-flushing and accumulate all rows in memory
// manually control how rows are flushed to disk
if(rownum % NOR == 0) {
((SXSSFSheet)sh).flushRows(NOR); // retain NOR last rows and flush all others
希望这会有所帮助.
这篇关于使用 Apache POI 进行低内存写入/读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!