0

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). Thank you

2
  • 2
    SXSSF (Streaming Usermodel API). Commented Oct 21, 2015 at 21:09
  • thanks:), now i am ashamed i haven't googled something else that would bring me to sxssf Commented Oct 22, 2015 at 11:10

1 Answer 1

1

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

See the description at http://poi.apache.org/spreadsheet/index.html and http://poi.apache.org/spreadsheet/how-to.html#sxssf

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.