【发布时间】:2018-04-04 00:33:43
【问题描述】:
我有以下代码通过“SaveAs”将通过 OpenXML SDK 的 Word 文档保存到新文档中。然后我想尝试读取从 ASP.Net 创建的文件,但是我无法这样做,因为该文件被锁定并且在重新启动应用程序池之前不会被释放。
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(tempfile, true))
{
wordDoc.ChangeDocumentType(WordprocessingDocumentType.Document);
Body body = wordDoc.MainDocumentPart.Document.Body;
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Testing"));
wordDoc.SaveAs(tempfileMerged);
}
wordDoc 是通过 using 处理的,但我不确定如何释放对从“SaveAs”生成的文件的锁定,也不知道为什么在这种情况下它无论如何都会有文件锁定?
【问题讨论】:
-
using只跟踪 using 语句中声明的元素的释放。在你的情况下wordDoc,所以第一次使用不会跟踪合并的文件。
标签: c# openxml-sdk