首页 > 解决方案 > 如何在 JAVA 中使用 apache POI 在 excel 中写入单元格?

问题描述

如何将excel文件写入特定的单元格。我有一个方法可以接受我必须写入的文件位置和行号。

我尝试的是读取文件然后创建一个单元格并将值设置到其中,但是当我查看它时,单元格仍然是空白的。

    public void writeInExcelRowCell(String file , int rowNum) throws Exception {
        FileInputStream excelFile = new FileInputStream(new File(file));
        Workbook workbook = new XSSFWorkbook(excelFile);

        Sheet sheet = workbook.getSheetAt(0);
        sheet.getRow(rowNum).createCell(2).setCellValue("DONE");
        sheet.getRow(rowNum).createCell(3).setCellValue("PENDING");
        workbook.close();        
        
    }

标签: javaapache-poi

解决方案


您所做的是正确的,但忘记写下您的更改。您需要打开一个 Outputstream 来写入这些更改。

    FileOutputStream outputStream = new FileOutputStream("yourfile");
    workbook.write(outputStream);
    workbook.close();
    outputStream.close();

希望这有效!


推荐阅读