我需要基于一个模板生成几个word文档。在每个文档中都需要替换某些字符串(所有字符串都包含在一对花括号中,以便轻松区分它们)。
我试过了 :
** 阅读整个模板并使用 Regex.Replace 替换所有出现的单词
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
docText = sr.ReadToEnd();
foreach (var key in keysToSearch)
docText = new Regex(key, RegexOptions.IgnoreCase).Replace(docText, replaceText);
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
sw.Write(docText);
}
** 并且还可以访问每个 Text 元素
using ( WordprocessingDocument doc =
WordprocessingDocument.Open(@"yourpath\testdocument.docx", true))
{
var body = doc.MainDocumentPart.Document.Body;
var paras = body.Elements<Paragraph>();
foreach (var para in paras)
{
foreach (var run in para.Elements<Run>())
{
foreach (var text in run.Elements<Text>())
{
if (text.Text.Contains("{{FullName}}"))
{
text.Text = text.Text.Replace("{{FullName}}", "replaced-text");
}
}
}
}
}
但上述解决方案均无效,因为在 .docx 文件中,所有这些大括号都存储在不同的文本元素 ( <w:t>
) 中:
<w:r w:rsidR="00786C31" w:rsidRPr="00475C3D">
<w:rPr>
<w:b/>
<w:sz w:val="36"/>
<w:highlight w:val="yellow"/>
<w:lang w:val="es-PE"/>
</w:rPr>
<w:t>{{</w:t>
</w:r>
<w:proofErr w:type="spellStart"/>
<w:r w:rsidR="001B7FC4" w:rsidRPr="00475C3D">
<w:rPr>
<w:b/>
<w:sz w:val="36"/>
<w:highlight w:val="yellow"/>
<w:lang w:val="es-PE"/>
</w:rPr>
<w:t>FullName</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
<w:r w:rsidR="00786C31" w:rsidRPr="00475C3D">
<w:rPr>
<w:b/>
<w:sz w:val="36"/>
<w:highlight w:val="yellow"/>
<w:lang w:val="es-PE"/>
</w:rPr>
<w:t>}}</w:t>
</w:r>
有什么方法可以使用特殊字符在模板中标记变量,以便我可以轻松找到并替换它们?