小编Jos*_*ipt的帖子

为什么 Apache poi 的 Workbook.close() 方法将内容写入输入文件?

Apache poi 的文档(3.17 版)说

无效关闭()

抛出 java.io.IOException

关闭从中读取工作簿的底层输入资源(文件或流)。

我的代码从模板文件创建一个工作簿,对其进行处理并将其写入新文件。模板文件应保持不变。但是当我调用 close() 方法时,文件的更改方式与输出文件的更改方式相同。

有人可以解释一下吗?在 close() 方法中是否有类似内置 write() 调用的东西?这是错误还是功能?

到目前为止,我的解决方案是放弃 close() 调用,但不知何故感觉不完整。

    String inPath = "/home/elmicha/test/template.xlsx";
    String outPath = "/home/elmicha/test/out.xlsx";

    try {
        Workbook xlsxFile = WorkbookFactory.create(new File(inPath));

        xlsxFile.getSheetAt(0).createRow(0).createCell(0).setCellValue("test");

        try (FileOutputStream pOuts = new FileOutputStream(outPath)) {
            xlsxFile.write(pOuts);
            
        xlsxFile.close();
        
        }

    } catch (IOException | InvalidFormatException | EncryptedDocumentException ex) {
        //...
    }
Run Code Online (Sandbox Code Playgroud)

java excel apache-poi

3
推荐指数
1
解决办法
3858
查看次数

标签 统计

apache-poi ×1

excel ×1

java ×1