Use case: I have a cost calculator implemented in excel. I have to insert a huge number of inputs to be entered into the input cells in a sheet and get the outputs from the same sheet. I have 3 excel sheets like that which are ~3MB in size. The excel sheets are in .xlsm formats.
Technologies used: Java 1.8 and Apache POI 4.0.1
Problem: Writing input values to the excel files that are >3MB is fast (takes ~10 sec for around 12 inputs). But the same action on excel file that is <3MB too slow (~10 sec for only one input).
I have seem that some people here say to use SXSSFWorkbook. I replaced the XSSFWorkbook with that, but it is giving me Null Pointer Exception. Also, this is used for large excel sheets that are above 100MB, right?
Code for writing values:
public void setData1(String filePath, String sheetName, int rowNum, int colNum, Object data) throws IOException {
File file = new File(filePath);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
Messages.terminate(file.getName() + " file is missing.");
}
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheet(sheetName);
Row row = sheet.getRow(rowNum);
Cell cell = row.getCell(colNum);
if(data instanceof Integer) {
cell.setCellValue((int)data);
}
else if (data instanceof String) {
cell.setCellValue(String.valueOf((String) data));
}
else if(data instanceof Double) {
if((double)data == Math.floor((double)data)) {
cell.setCellValue((int)Math.floor((double)data));
}
else {
cell.setCellValue((double)data);
}
}
FileOutputStream fos = new FileOutputStream(new File(filePath));
BaseFormulaEvaluator.evaluateAllFormulaCells(workbook);
workbook.write(fos);
workbook.close();
fos.close();
fis.close();
}