简体   繁体   English

java apache poi 内存清空

[英]java apache poi memory emptying

i have a code that goes something like this:我有一个类似这样的代码:

public class excelwriter {

    public static void main(String[] args) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("sheet1");

        for (int o = 1; o < 1000000; o++) {
            Row row = sheet.createRow(o);

            for (int i = 0; i < 100000; i++) {
                Cell cell = row.createCell(i); 
            }      
        }

        try (FileOutputStream outputStream = new FileOutputStream("C:/JavaBooks.xlsx")) {
            workbook.write(outputStream);
        }
    }
}

My problem is the heapspace error, to which i get sooner or later using this code, as the workbook object stores all the cells.我的问题是堆空间错误,我迟早会使用此代码解决该错误,因为工作簿对象存储所有单元格。 How can i modify the code, that java after writing a row with all the cells, writes it to xlsx and throws the data away (or after 1000 rows if that makes it faster).我如何修改代码,即在写入包含所有单元格的一行之后的 Java 代码,将其写入 xlsx 并将数据丢弃(或者在 1000 行之后,如果这使它更快)。 Thank you谢谢

Just try replacing XSSFWorkbook with SXSSFWorkbook , then POI will write the intermediate data to temporary files and copy the result together in the call to write() .只需尝试将XSSFWorkbook替换为SXSSFWorkbook ,然后 POI 会将中间数据写入临时文件并将结果一起复制到write()的调用中。

See the description at http://poi.apache.org/spreadsheet/index.html and http://poi.apache.org/spreadsheet/how-to.html#sxssf请参阅http://poi.apache.org/spreadsheet/index.htmlhttp://poi.apache.org/spreadsheet/how-to.html#sxssf 上的说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM