使用的OpenXML插入换行符在Word中 [英] Inserting newlines in Word using OpenXML

查看:3976
本文介绍了使用的OpenXML插入换行符在Word中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OPENXML WordProcessingDocument打开一个Word模板,并用字符串替换占位符X1。除非我需要的字符串包含换行符这工作得很好。
我怎样才能用文字代替X1可能包含换行符这个词会承认?我已经试过的\\ n \\ r但这些不工作

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

只是为了进一步解释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.

推荐答案

要插入新行,你有一​​个中断实例添加到运行

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆