The part /docProps/app.xml fail to be saved in the stream with marshaller org.apache.poi.openxml4j

使用 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
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用NPOI库中的WordToPdfConverter类将Word文档转换为PDF格式。以下是一个简单的示例代码: ```csharp using NPOI.XWPF.UserModel; using NPOI.XWPF.UserModel.Extensions; using NPOI.OpenXmlFormats.Wordprocessing; using NPOI.Util; using System.IO; // 读取Word文档 XWPFDocument doc = new XWPFDocument(File.OpenRead("doc.docx")); // 创建PDF输出流 FileStream pdfStream = new FileStream("doc.pdf", FileMode.Create); // 将Word文档转换为PDF格式 doc.EnsureMinimumCompatibility(XWPFDocument.MINIMUM_COMPATIBILITY); doc.GetPackage().SetPartContentType("/word/document.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"); doc.GetPackage().SetPartContentType("/word/styles.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"); doc.GetPackage().SetPartContentType("/word/fontTable.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"); doc.GetPackage().SetPartContentType("/word/settings.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"); doc.GetPackage().SetPartContentType("/word/webSettings.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml"); doc.GetPackage().SetPartContentType("/word/theme/theme1.xml", "application/vnd.openxmlformats-officedocument.theme+xml"); doc.GetPackage().SetPartContentType("/docProps/core.xml", "application/vnd.openxmlformats-package.core-properties+xml"); doc.GetPackage().SetPartContentType("/docProps/app.xml", "application/vnd.openxmlformats-officedocument.extended-properties+xml"); doc.GetPackage().SetPartContentType("/word/_rels/document.xml.rels", "application/vnd.openxmlformats-package.relationships+xml"); doc.Write(pdfStream); // 关闭流 pdfStream.Close(); doc.Close(); ``` 在代码中,我们首先读取要转换的Word文档,然后创建一个PDF输出流,最后使用WordToPdfConverter类将文档转换为PDF格式并写入输出流。请注意,此代码示例仅适用于使用NPOI创建的Word文档。如果您需要转换其他格式的Word文档,可能需要使用其他库或工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值