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(); }