将WordprocessingML添加到word文档中
问题描述:
是否可以将WordprocessingML的“块”添加到word文档(而不是整个文档)?我目前有一个我用作模板的docx,它包含一个ContentControl。我想通过AltChunk插入一段WordprocessingML,但插入的内容呈现为文本,而不是像我希望的那样在文档中实现。将WordprocessingML添加到word文档中
我目前使用的方法是:
var main = doc.MainDocumentPart;
// Create new AltChunk
string altChunkId = "altChunkId";
AlternativeFormatImportPart chunk = main.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);
// Populate altChunk. stream = MemoryStream containing WordprocessingML
chunk.Feed(stream);
// Replace content control with altChunk info
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
// Get SdtBlock to replace
SdtBlock block = ContentControlHelpers.GetSdtBlock(doc, "ContentControlId");
OpenXmlElement parent = block.Parent;
parent.InsertAfter(altChunk, block);
block.Remove();
,我试图插入WordproccessingML的样品是本(经由XML + XSLT生成。):
<w:p>
<w:pPr>
<w:pStyle w:val="heading2" />
</w:pPr>
<w:r>
<w:t>Product 1</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="heading3" />
</w:pPr>
<w:r>
<w:t>Europe</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="heading4" />
</w:pPr>
<w:r>
<w:t>France</w:t>
</w:r>
</w:p>
我已经尝试添加<w:document>
和<w:body>
元素来包装它,但无论我想要的文档只是将WordprocessingML作为文本,因为它显示在上面,而不是将其嵌入到文档中。
任何关于我可能会出错的建议?
答
看看DocumentBuilder - 开源示例和指导,展示如何做到你想要的。
http://openxmldeveloper.org/wiki/w/wiki/documentbuilder.aspx
观看以下视频第一:
-Eric
感谢埃里克,这看起来很有希望,但是我收到错误 “不是一个开放的XML文档。”当使用DocumentBuilder.BuildDocument()时。这是因为我的片段不是完整的wml文档?无论如何围绕这个? – Fermin 2012-08-13 10:09:58