是否可以在word文档中添加一块WordprocessingML (而不是整个文档)?我目前有一个docx作为我的模板,它包含一个ContentControl。我想通过AltChunk插入WordprocessingML的一部分,但是我插入的内容呈现为文本,而不是像我希望的那样被实现到文档中。
我目前使用的方法是:
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呈现为文本,而不是将其嵌入到文档中。
对于我可能会出错的地方有什么建议吗?
发布于 2012-08-09 12:53:29
看看DocumentBuilder -开源示例和指南,它展示了如何准确地做你想要做的事情。
http://openxmldeveloper.org/wiki/w/wiki/documentbuilder.aspx
先看下面的视频:
http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2011/08/17/new-screen-cast-short-and-sweet-intro-to-documentbuilder-2-0.aspx
-Eric
https://stackoverflow.com/questions/11868287
复制相似问题