使用 poi 保存 excel 内容时,出现类似的错误信息:
Exception in thread "main" org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : The part /docProps/app.xml fail to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@4fbf1942
网上很多都说是 输出流 在关闭的情况下继续使用的结果,目前我还没有找到合适的解决这个 错误的办法。
下面提供一个思路,可以绕过这个 bug 。
我的业务逻辑 读取 模板excel,编写新的 excel ,不可修改 模板 excel 的内容。
思路:
1,拷贝 模板 excel 到一个临时文件(a.excel)
2,根据临时文件填充内容,生成新的 excel (b.excel,确保 和 临时文件的名称不一样!这里很重要)
3,保存 新的 excel
4,关闭 poi 对象
5,删除临时文件 (a.excel)
核心代码如下:
1,拷贝 模板 excel 到一个临时文件(a.excel)
private void c