Open XML - 格式不正确

发布于 2024-09-16 09:19:17 字数 2416 浏览 9 评论 0原文

关于这篇文章,我一直在大量使用 Word 的 Open XML 和库可用的。大多数人似乎不知道的是,有一些很棒的 资源 用于以编程方式处理 Word 的 Open XML 格式(我的意思不是手动将 XML 写入文件流!)。

我的问题是关于我正在做的事情。基本上,我手动创建邮件合并。现在,邮件合并实际上工作正常,但工作不正常的是将邮件合并字段放置在文档中。这就是我正在做的事情:

string mergeFieldName = m.Value.Replace("[", string.Empty).Replace("]", string.Empty);
Body body = new Body();
DocumentFormat.OpenXml.Wordprocessing.Paragraph p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
SimpleField simpleField = new SimpleField() { Instruction = " MERGEFIELD " + mergeFieldName + " " };
Run run = new Run();
RunProperties runProperties = new RunProperties();
NoProof noProof = new NoProof();
runProperties.Append(noProof);
Text text = new Text("«" + mergeFieldName + "»");
run.Append(runProperties);
run.Append(text);
simpleField.Append(run);
p.Append(simpleField);
body.Append(p);

docXml.InnerXml = docXml.InnerXml.Replace(m.Value, body.InnerXml);

变量 m 实际上是一个 Match 对象,基于我的正则表达式匹配方括号内的所有内容。 (您可能想知道为什么我这样做 - 这与这里无关)

我的问题是生成的 XML 无效,因为我有以下内容:

<w:p w:rsidR="00945DC1" w:rsidRDefault="006878CA">
  <w:r>
    <w:t xml:space="preserve">
      <w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:fldSimple w:instr=" MERGEFIELD FullName ">
          <w:r>
            <w:rPr>
              <w:noProof />
            </w:rPr>
            <w:t>«FullName»</w:t>
          </w:r>
        </w:fldSimple>
      </w:p>
    </w:t>
  </w:r>
</w:p>

这是来自手动创建的文档的工作 XML 的示例:

<w:p w:rsidR="001B2242" w:rsidRDefault="001B2242">
  <w:r>
    <w:tab/>
  </w:r>
  <w:fldSimple w:instr=" MERGEFIELD &quot;SalutationName&quot; ">
    <w:r>
      <w:rPr>
        <w:noProof/>
      </w:rPr>
      <w:t>«SalutationName»</w:t>
    </w:r>
  </w:fldSimple>
</w:p>

实际我收到的错误是“无法打开文件,因为内容有问题”。如果我不写出邮件合并字段,加载文档,然后插入邮件合并字段,它就可以工作。所以实际的邮件合并是正确的,只是邮件合并字段不正确。 (有关如何合并邮件的信息,请参阅我参考的帖子)。

对这个有什么想法吗?

干杯

In relation to this post I've been working heavily with Word's Open XML and the libraries available. What seems to be unknown to most is that there is some great resources out there for programatically dealing with the Open XML format for Word (and I don't mean manually writing out XML to the filestream!).

My question is regarding something I'm doing. Basically, I'm manually creating a mail merge. Now, the mail merge actually works fine, what doesn't work fine, however, is placing the mail merge fields within the document. This is what I'm doing:

string mergeFieldName = m.Value.Replace("[", string.Empty).Replace("]", string.Empty);
Body body = new Body();
DocumentFormat.OpenXml.Wordprocessing.Paragraph p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
SimpleField simpleField = new SimpleField() { Instruction = " MERGEFIELD " + mergeFieldName + " " };
Run run = new Run();
RunProperties runProperties = new RunProperties();
NoProof noProof = new NoProof();
runProperties.Append(noProof);
Text text = new Text("«" + mergeFieldName + "»");
run.Append(runProperties);
run.Append(text);
simpleField.Append(run);
p.Append(simpleField);
body.Append(p);

docXml.InnerXml = docXml.InnerXml.Replace(m.Value, body.InnerXml);

The variable m is actually a Match object based on my regular expression matching everything within square brackets. (You might be wondering why I'm doing this like I am - that's not relevant here)

My problem is the resulting XML is invalid because I have the following:

<w:p w:rsidR="00945DC1" w:rsidRDefault="006878CA">
  <w:r>
    <w:t xml:space="preserve">
      <w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:fldSimple w:instr=" MERGEFIELD FullName ">
          <w:r>
            <w:rPr>
              <w:noProof />
            </w:rPr>
            <w:t>«FullName»</w:t>
          </w:r>
        </w:fldSimple>
      </w:p>
    </w:t>
  </w:r>
</w:p>

And here's an example of the working XML from a manually created document:

<w:p w:rsidR="001B2242" w:rsidRDefault="001B2242">
  <w:r>
    <w:tab/>
  </w:r>
  <w:fldSimple w:instr=" MERGEFIELD "SalutationName" ">
    <w:r>
      <w:rPr>
        <w:noProof/>
      </w:rPr>
      <w:t>«SalutationName»</w:t>
    </w:r>
  </w:fldSimple>
</w:p>

The actual error I get is "The file cannot be opened because there are problems with the contents". If I don't write the mail merge fields out, load the document, and then insert mail merge fields, it works. So the actual mail merge is correct, it's just the mail merge fields that aren't correct. (See my referenced post for how to get the mail merge out).

Any ideas on this one?

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风疏影 2024-09-23 09:19:17

我看到的是你替换它是错误的。您正在将要创建的新 XML 放入 aw:t 标记内,该标记是一个文本标记(如果我没记错的话),

请跳过正文创建,并将段落 p 的 InnerXml 替换为 m.Values 父级的 innerXml。

也就是说,如果我理解正确的话。

What I see is that you are replacing it wrong. You are putting the new XML you are creating inside a w:t tag which is a text tag If i'm not mistaken'

Skip the Body creation and replace the InnerXml of the Paragraph p with the innerXml of m.Values parent.

That is if I'm understanding this correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文