我使用OpenXML SDK编写了以下代码,通过“SaveAs”将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);
}
通过using关闭了wordDoc,但是我不确定如何释放通过“SaveAs”生成的文件上的锁定,并且无论如何都不确定为什么会出现文件锁定情况。
using
语句只跟踪在using
语句中声明的元素的释放。在您的情况下,是wordDoc
,因此第一个using
语句不会跟踪合并后的文件。 - Cleptus