使用 OpenXML 在 Word 中插入换行符
我正在使用 openxml WordProcessingDocument 打开 Word 模板并将占位符 x1 替换为字符串。除非我需要字符串包含换行符,否则这工作正常。 如何将 x1 替换为可能包含 word 可以识别的换行符的文本?我已经尝试过 \n \r 但这些不起作用
只是为了进一步解释打开单词模板时我将其读入 StreamReader 然后使用 .Replace 替换 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 \n \r but these do not work
Just to explain further when the word template is opened I read it into a StreamReader then use .Replace to replace x1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要插入换行符,您必须向
Run
添加一个Break
实例。示例:
生成的 XML 类似于:
To insert newlines, you have to add a
Break
instance to theRun
.Example:
The XML produced will be something like:
下面是一个 C# 函数,它将获取一个字符串,按换行符将其分割并以 OpenXML 形式呈现。使用时,实例化一个 Run 并使用字符串将其传递到函数中。
Here's a C# function that will take a string, split it on line breaks and render it in OpenXML. To use, instantiate a Run and pass it into the function with a string.
尽管这个问题已经得到解答,但我还有另一种方法来解决以下问题:
如何使用 OpenXML 制作 XXX?
在这种情况下,您可以利用强大的 Microsoft OpenXML 生产力工具 strong>(也称为OpenXmlSdkTool)。 在此处下载。
Altough this question is already answered I have another approach to solve questions like :
How can I make XXX with OpenXML??
In this cases you could make use of the powerful Microsoft OpenXML productivity tool (also known as OpenXmlSdkTool). Download here.
我有同样的问题,在我的例子中
标签有效。I have the same issue and in my case
<w:br />
tag worked.