I am trying to write data in an existing excel (xlsx) file using Apache POI. The code does not give any error and the sysout shows the updated data in the cell. However the same is not reflected in the excel workbook when I open it. This is my code snippet -
try {
InputStream is = new FileInputStream(excelFileName);
Workbook wb = WorkbookFactory.create(is);
Sheet sheet = wb.getSheet(sheetName);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell == null) {
cell = row.createCell(3);
}
is.close();
cell.setCellType(Cell.CELL_TYPE_STRING);
System.out.println(cell.getStringCellValue());
FileOutputStream os = new FileOutputStream(excelFileName);
cell.setCellValue("test");
wb.write(os);
os.close();
wb.close();
} catch (IOException | InvalidFormatException e) {
e.printStackTrace();
}