使用 OpenXML 在 Word 中插入换行符 [英] Inserting newlines in Word using OpenXML
问题描述
我正在使用 openxml WordProcessingDocument 打开 Word 模板并用字符串替换占位符 x1.这很好用,除非我需要字符串包含换行符.如何用文本替换 x1 可能包含单词会识别的换行符?我试过 但这些都不起作用
I am using openxml WordProcessingDocument to open a Word template and replace placeholder x1 with a string. This works fine unless I need the string to contain a newline. How can I replace x1 with text may contain newlines that word would recognise? I have tried but these do not work
为了进一步解释当打开 word 模板时,我将其读入 StreamReader,然后使用 .Replace 替换 x1.
Just to explain further when the word template is opened I read it into a StreamReader then use .Replace to replace x1.
推荐答案
要插入换行符,您必须在 Run
中添加一个 Break
实例.
To insert newlines, you have to add a Break
instance to the Run
.
示例:
run.AppendChild(new Text("Hello"));
run.AppendChild(new Break());
run.AppendChild(new Text("world"));
生成的 XML 将类似于:
The XML produced will be something like:
<w:r>
<w:t>Hello</w:t>
<w:br/>
<w:t>world</w:t>
</w:r>
这篇关于使用 OpenXML 在 Word 中插入换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!