Jeh*_*hof 7 c# search replace openxml
我正在尝试使用Open XML SDK 2.5从.docx-Templates创建报告.在模板中,我定义了一些被实际值替换的占位符.占位符可以在各种模式中定义,例如
<#Name#>
or
<!#Name#!>
or
#Name#
or
{{Name}}
Run Code Online (Sandbox Code Playgroud)
占位符的模式也可以采用其他格式,只要可以在文本中清楚地标识占位符即可.
我目前面临的问题是一个占位符多间经常分裂<w:t>
-elements( 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>
Run Code Online (Sandbox Code Playgroud)
这里占位符#FirstName#
很容易识别,因为它在一个<w:t>
元素内,但是占位符#LastName#
在多个<w:t>
元素之间分割,所以我不能在文档上的文本上使用简单的正则表达式
Regex placeholderRegex = new Regex(@"#[\w]*#");
document.MainDocumentPart.Document.Body.Descendants<Text>().Where(t=> placeholderRegex.IsMatch(t.Text))
Run Code Online (Sandbox Code Playgroud)
我无法控制模板的定义方式,也不会对用户如何创建模板施加约束.对我来说,当占位符分成多个<w:t>
元素时也不清楚.
另一个使用{{[\w]*}}
占位符模式的示例.
文字(Docx)
{{Ort}}
And this {{placeholder}} is within the text
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
那么我现在的问题是使用Open XML SDK搜索和替换占位符的方式是什么?SDK中是否有一些功能可以帮助我?有没有人解决这个问题并提供帮助?
小智 2
是的,MS Word 应用程序甚至将单个单词拆分为多个运行/文本元素(出于某种原因)。不,Open XML SDK 功能中不提供查找/替换功能。但是您可以创建自己的最简单的段落/运行/文本结构。您将需要:
归档时间: |
|
查看次数: |
2436 次 |
最近记录: |