Reading template xlsm and writing large data to existing worksheet using SXSSF (POI)

Sva picture Sva · Oct 23, 2013 · Viewed 8.9k times · Source

I am able to read existing xlsm using XSSF and write data into worksheet using SXSSF. Finally output it as another xlsm with Outputstream.
SXSSF is mentioned for writing xlsx in documentation
Is it right approach to read and write xlsm for huge data or the file will get corrupted if done by this solution ?
This is the sample code that works,

public static void main(String[] args) throws Throwable {

    OPCPackage pkg = OPCPackage.open(new File("sample.xlsm"));
        XSSFWorkbook wb_template;
        wb_template = new XSSFWorkbook(
            pkg
        );
        System.out.println("package loaded");
    SXSSFWorkbook wb = new SXSSFWorkbook(wb_template); 
    wb.setCompressTempFiles(true);

    SXSSFSheet sh = (SXSSFSheet) wb.createSheet();
    sh.setRandomAccessWindowSize(100);// keep 100 rows in memory, exceeding rows will be flushed to disk
    for(int rownum = 4; rownum < 5000; rownum++){
       Row row = sh.createRow(rownum);
       for(int cellnum = 0; cellnum < 10; cellnum++){
        Cell cell = row.createCell(cellnum);
        String address = new CellReference(cell).formatAsString();
        cell.setCellValue(address);
       }

    }
    FileOutputStream out = new FileOutputStream(new File("C:\\ouput\\new_file.xlsm"));
    wb.write(out);
    out.close();  }

Answer

Levenal picture Levenal · Oct 23, 2013

I don't see much wrong with your current code, however on the apache website it says that SXSSF leaves temporary files on your computer, and that you must call dispose as below:

Note that SXSSF allocates temporary files that you must always clean up explicitly, by calling the dispose method.
SXSSFWorkbook wb = new SXSSFWorkbook(100);
// dispose of temporary files backing this workbook on disk
wb.dispose();

I would recommend performing the dispose as it seems to prevent leftover information being stored. On whether it is the right approach, I would suggest taking a test file and giving it a couple of goes, how well this code executes will will be affected by your system (cpu power, memory e.t.c) and the size of the documents you are processing.

Good Luck!