【问题标题】:Adding a chunk of WordprocessingML to word document将一块 WordprocessingML 添加到 word 文档
【发布时间】:2012-08-08 15:47:04
【问题描述】:

是否可以将 WordprocessingML 的“块”添加到 word 文档(而不是整个文档)?我目前有一个用作模板的 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>

我已经尝试添加 &lt;w:document&gt;&lt;w:body&gt; 元素来包装它,但无论我在尝试什么文档只是将 WordprocessingML 呈现为文本,如上所示,而不是将其嵌入到文件。

关于我可能出错的地方有什么建议吗?

【问题讨论】:

    标签: .net ms-office openxml office-2010


    【解决方案1】:

    查看 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,这看起来很有希望,但是我收到错误“不是 Open XML 文档”。使用 DocumentBuilder.BuildDocument() 时。这是因为我的 sn-p 不是完整的 wml 文档吗?反正围绕这个?
    猜你喜欢
    • 2019-11-25
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多