java - 使用 Java Apache POI 更新 Excel 文件中的单元格

标签 java excel apache-poi

我正在尝试使用 Java ( Apache POI ) 更新现有 Excel 文件中的空单元格,这是我编写的代码,我没有收到任何错误,并且值也没有更改。

        FileInputStream file = new FileInputStream(new File("recap.xlsx"));
        
 
        
        XSSFWorkbook workbook = new XSSFWorkbook(file);
         
        
         XSSFSheet sheet = workbook.getSheetAt(0);
         Cell cell = null;
         
       //Retrieve the row and check for null
         XSSFRow sheetrow = sheet.getRow(7);
         if(sheetrow == null){
             sheetrow = sheet.createRow(7);
             System.out.println("its null 1 ");
         }
         //Update the value of cell
         cell = sheetrow.getCell(7);
         if(cell == null){
             cell = sheetrow.createCell(7);
             System.out.println("its null  2 !");
         }
         
         
         cell.setCellValue("Second");

     file.close();

     workbook.close();

我在控制台中得到“its null 2!”。

有什么解决办法吗?

谢谢:)

最佳答案

您需要打开输出流并写入工作簿,如下所示:

file.close();

FileOutputStream outputStream = new FileOutputStream("recap.xlsx");
workbook.write(outputStream);

workbook.close();
outputStream.close();

此外,请确保在此写入操作后关闭工作簿和输出流。

关于java - 使用 Java Apache POI 更新 Excel 文件中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69135875/

相关文章:

java - Apache POI 图像比例图像

java - 识别没有Javadoc的java文件

java - Spring 休息 : Removing empty objects from response in JSON format

excel - Excel 工作表中的独立计时器宏

excel - 使用 VBA 方法通过 Bing API 获取距离

java - 如何使用 apache poi 更改 docx 的 style.xml 中的字体大小

java - POI - null 后文件意外结束

java - Android - fragment 不显示

java - Liferay <门户组件 :actionURL>

c# - 在 C# 中读取 Excel 文件