[英]Creating a Microsoft Word Interop Document from Open Xml Document
尝试从打开的xml文件创建一个互操作应用程序。 我正在使用Open Xml SDK中单词文件的反射代码。 当我尝试将Open Xml文档的xml插入互操作文档中时
doc.Range().InsertXML(package.MainDocumentPart.Document.OuterXml);
该行引发System.Runtime.InteropServices.COMException
,该错误表示无法将XML插入该位置。
这是完整的代码
public void CreatePackage()
{
using (MemoryStream mem = new MemoryStream())
{
using (WordprocessingDocument package = WordprocessingDocument.Create(mem, WordprocessingDocumentType.Document))
{
CreateParts(package);
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Add(System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
doc.Range().InsertXML(package.MainDocumentPart.Document.OuterXml);
doc.Activate();
}
}
}
您可以这样:
public void CreatePackage()
{
using (MemoryStream mem = new MemoryStream())
{
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(mem, WordprocessingDocumentType.Document))
{
CreateParts(wordDocument);
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Add(System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
XDocument xDoc = OPCHelper.OpcToFlatOpc(wordDocument.Package);
string openxml = xDoc.ToString();
doc.Range().InsertXML(openxml);
doc.Activate();
}
}
}
要获取OPCHelper
类的代码,可以从此处使用该实用程序,以从模板生成Word文档 。
有关更多信息,请参阅此文档,还将开放XML文档转换为平面OPC格式
我会将OpenXML保存到临时文件中,并使用互操作在Word中打开该文件。 但是我不知道interop是否应该或不支持XML,所以我无法回答为什么InsertXML不起作用。 我怀疑它不期望使用OpenXML类型的XML,而是其他一些东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.