OpenXML 更新 customXMLPart
我一直在使用 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();
}
}
我处于正方形,为什么会发生这种情况。任何帮助表示赞赏 谢谢
I have been using OOXML api to update the custom xml part in a .docx. The code updates the custom xml part in the document. My problem is that the same code replaces and generates perfect .docx when I use in a console App, but it doesn't replaces nor generates .docx when used in ASP.NET application. The code snippet in question is as follows:
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();
}
}
I am at square why is this happening. Any help is appreciated
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更好的方法可能是有选择地仅更新您有兴趣替换的 XML 部分。
检查我的答案这里基本上同样的问题。
在控制台中使用相同的代码后,我目前正在 MVC 应用程序中执行此操作,因此我不确定什么可能/会导致控制台/web.xml 之间出现问题。
WordProcessingDocument.Open()
的签名似乎也已更改,因此自动保存可能无法正常工作。另外,是否有可能抛出异常,而该异常以某种方式被调用者吞噬?也许将 try/catch 放入外部using(...){...}
中。A better approach to this is probably to selectively update only the XML Part you are interested in replacing.
Check my answer here to basically the same question.
I'm currently doing this in an MVC app after starting with the same code in console, so I'm not sure what could/would be causing a problem between console/web.
It also looks like the signature has changed for
WordProcessingDocument.Open()
so perhaps the AutoSave isn't working. Also, is it possible an exception is being thrown that is somehow being swallowed by the caller? Maybe toss a try/catch into the outerusing(...){...}
.