c# - 搜索和替换拆分为多个的占位符 <w :t>-Elements

标签 c# search replace openxml

我正在尝试使用 Open XML SDK 2.5 从 .docx-Templates 创建报告。在模板中,我定义了一些由实际值替换的占位符。占位符可以在各种模式中定义,例如

<#Name#>
or
<!#Name#!>
or
#Name#
or
{{Name}}

占位符的架构也可以采用其他格式,只要占位符可以在文本中清楚地识别即可。

我目前面临的问题是一个占位符经常被分割成多个 <w:t> -DocumentFormat.OpenXml.Wordprocessing.Text 中的元素 ( <w:p> ) -元素(DocumentFormat.OpenXml.Wordprocessing.Paragraph)。一个例子

<w:p w:rsidR="003137E0" w:rsidRDefault="008C62F1" w:rsidP="00D43D55">
  <w:r>
    <w:t xml:space="preserve">#FirstName# </w:t>
  </w:r>
  <w:r w:rsidR="00C93A70">
    <w:t>#LastName</w:t>
  </w:r>
  <w:r w:rsidR="005F49B7">
    <w:t>#</w:t>
  </w:r>
</w:p>

这里是占位符 #FirstName#很容易识别,因为它在一个 <w:t> 之内- 元素,但占位符 #LastName#被分成多个 <w:t> - 元素,这样我就不能像

这样在文档的文本上使用简单的正则表达式
Regex placeholderRegex = new Regex(@"#[\w]*#");

document.MainDocumentPart.Document.Body.Descendants<Text>().Where(t=> placeholderRegex.IsMatch(t.Text))

我无法控制模板的定义方式,我也不会限制用户创建模板的方式。对我来说,当一个占位符被分成多个 <w:t> 时也不清楚。 -元素。

另一个使用 {{[\w]*}} 的例子作为占位符的模式。

文本(Docx)

{{Ort}}
And this {{placeholder}} is within the text 

XML (OpenXML)

<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
  <w:body>
    <w:p w:rsidR="007B60F2" w:rsidRDefault="00BB7370" w:rsidP="00D43D55">
      <w:pPr>
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
      </w:pPr>
      <w:r w:rsidRPr="00114EA7">
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:t>{{</w:t>
      </w:r>
      <w:r w:rsidR="00C93A70" w:rsidRPr="00114EA7">
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:t>Ort</w:t>
      </w:r>
      <w:r w:rsidR="00114EA7" w:rsidRPr="00114EA7">
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:t>}}</w:t>
      </w:r>
    </w:p>
    <w:p w:rsidR="00EC3BED" w:rsidRPr="00114EA7" w:rsidRDefault="00C310E0" w:rsidP="00D43D55">
      <w:pPr>
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
      </w:pPr>
      <w:r w:rsidRPr="00114EA7">
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:t xml:space="preserve">This is a text with a </w:t>
      </w:r>
      <w:r w:rsidR="00A07A5D">
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:t>{{</w:t>
      </w:r>
      <w:r w:rsidRPr="00114EA7">
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:t>placeholder</w:t>
      </w:r>
      <w:r w:rsidR="00A07A5D">
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:t>}</w:t>
      </w:r>
      <w:r w:rsidR="00114EA7" w:rsidRPr="00114EA7">
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:t>}</w:t>
      </w:r>
      <w:bookmarkStart w:id="0" w:name="_GoBack" />
      <w:bookmarkEnd w:id="0" />
      <w:r w:rsidR="00A07A5D">
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:t>.</w:t>
      </w:r>
    </w:p>
    <w:sectPr w:rsidR="00EC3BED" w:rsidRPr="00114EA7" w:rsidSect="00237721">
      <w:pgSz w:w="11906" w:h="16838" />
      <w:pgMar w:top="1417" w:right="1417" w:bottom="1134" w:left="1417" w:header="708" w:footer="708" w:gutter="0" />
      <w:cols w:space="708" />
      <w:docGrid w:linePitch="360" />
    </w:sectPr>
  </w:body>
</w:document>

所以我现在的问题是,使用 Open XML SDK 搜索和替换占位符的方法是什么? SDK 中是否有一些功能可以帮助我?有没有其他人解决了这个问题并提供帮助?

最佳答案

请参阅docx4j does not replace variables获取解决问题的 Java 源代码的链接。

您可以在 C# 中实现类似的东西,或者通过 http://www.nuget.org/packages/docx4j.NET/3.0.1 使用该代码

关于c# - 搜索和替换拆分为多个的占位符 <w :t>-Elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23005630/

相关文章:

perl - 如何使用 Perl 替换文件中间的行?

jquery - 用 jquery 替换反斜杠

bash - awk/sed : How to do a recursive find/replace of a string in files with a certain file extension?

c# - 安排 C# 应用程序

c# - 为什么我的继承类没有隐藏我的基类方法?

c# - 如何避免字典中的空键错误?

c# - 在 ASP.NET 中键入时显示搜索结果

c# - 将 ObservableCollection 保存到文件 (.txt)

search - 视觉相似性算法(用于 CBIR)

python - 如何获取一个字符串并返回字典中与该单词仅相差一个字母的所有单词的列表?