我遇到错误“ Word在abc.docx中发现了不可读的内容。您要恢复此文档的内容吗?”,同时打开Word(.docx)文件。
我已经尝试了Internet上提供的所有解决方案,但没有成功。下面是我将内容写入流中的代码。
using (MemoryStream editingMemoryStream = new MemoryStream())
{
dailyTemplate.Position = 0;
dailyTemplate.CopyTo(editingMemoryStream);
editingMemoryStream.Position = 0;
using (WordprocessingDocument newDoc = WordprocessingDocument.Open(editingMemoryStream, true))
{
WP.Body templateBody = newDoc.MainDocumentPart.Document.Body;
var main = newDoc.MainDocumentPart;
//* some code is here*//
var clone = templateBody.CloneNode(true);
parent.Document.Body.AppendChild(new WP.Paragraph(new WP.Run(clone)));
parent.Document.Save();
}
}
该文件包含2个带有一些标签的表。
[看一下您的代码,好像您要在以下两行代码中将w:body
元素(Body
实例)添加到w:r
元素(Run
实例):
var clone = templateBody.CloneNode(true);
parent.Document.Body.AppendChild(new WP.Paragraph(new WP.Run(clone)));
在上面的摘录中,clone
是具有其所有子元素的Body
实例(w:body
元素)。我只能假设parent
是其他MainDocumentPart
的WordprocessingDocument
。您要为新的Paragraph
(w:p
)附加一个w:r
(Run
)和一个w:body
(Body
),并为其附加子项。这是无效的Open XML,很可能是Word抱怨的原因。