我一直在使用 OOXML api 来更新 .docx 中的自定义 xml 部分。代码更新文档中的自定义 xml 部分。我的问题是,当我在控制台应用程序中使用时,相同的代码会替换并生成完美的 .docx,但在 ASP.NET 应用程序中使用时它不会替换或生成 .docx。有问题的代码片段如下:
string tmp = string.Format("{0}.docx", Guid.NewGuid());
File.Copy(FileName, tmp);
_xml = ReadXML(XmlPath);
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(tmp, true)) {
var mainPart = wordDoc.MainDocumentPart;
mainPart.DeleteParts<CustomXmlPart>(mainPart.CustomXmlParts);
//Add a new customXML part and then add content
var customXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
//copy the XML into the new part...
using (var ts = new StreamWriter(customXmlPart.GetStream())) {
ts.Write(_xml);
ts.Flush();
}
}
我很清楚为什么会发生这种情况。感谢任何帮助谢谢