我正在尝试使用Apache POI在现有的excel(xlsx)文件中写入数据 . 代码不会给出任何错误,sysout会在单元格中显示更新的数据 . 但是,当我打开它时,excel工作簿中没有反映出来 . 这是我的代码片段 -
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();
}