使用 OpenXML 在 Word 中插入换行符

发布于 2024-09-02 08:44:17 字数 210 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若能看破又如何 2024-09-09 08:44:17

要插入换行符,您必须向 Run 添加一个 Break 实例。

示例:

run.AppendChild(new Text("Hello"));
run.AppendChild(new Break());
run.AppendChild(new Text("world"));

生成的 XML 类似于:

<w:r>
  <w:t>Hello</w:t>
  <w:br/>
  <w:t>world</w:t>
</w:r>

To insert newlines, you have to add a Break instance to the Run.

Example:

run.AppendChild(new Text("Hello"));
run.AppendChild(new Break());
run.AppendChild(new Text("world"));

The XML produced will be something like:

<w:r>
  <w:t>Hello</w:t>
  <w:br/>
  <w:t>world</w:t>
</w:r>
伴我心暖 2024-09-09 08:44:17

下面是一个 C# 函数,它将获取一个字符串,按换行符将其分割并以 OpenXML 形式呈现。使用时,实例化一个 Run 并使用字符串将其传递到函数中。

void parseTextForOpenXML(Run run, string textualData)
{
        string[] newLineArray = { Environment.NewLine };
        string[] textArray = textualData.Split( newLineArray, StringSplitOptions.None );
        
        bool first = true;
        
        foreach (string line in textArray)
        {
            if(!first)
            {
                run.Append(new Break());
            }
            
            first = false;
            
            Text txt = new Text();
            txt.Text = line;
            run.Append(txt);
        }
}

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.

void parseTextForOpenXML(Run run, string textualData)
{
        string[] newLineArray = { Environment.NewLine };
        string[] textArray = textualData.Split( newLineArray, StringSplitOptions.None );
        
        bool first = true;
        
        foreach (string line in textArray)
        {
            if(!first)
            {
                run.Append(new Break());
            }
            
            first = false;
            
            Text txt = new Text();
            txt.Text = line;
            run.Append(txt);
        }
}
冷弦 2024-09-09 08:44:17

尽管这个问题已经得到解答,但我还有另一种方法来解决以下问题:

如何使用 OpenXML 制作 XXX?

在这种情况下,您可以利用强大的 Microsoft OpenXML 生产力工具 strong>(也称为OpenXmlSdkTool)。 在此处下载。

  1. 创建一个新的 Office 文档
  2. 添加您想要使用 OpenXML SDK 重现的文档的部分。
  3. 使用Microsoft OpenXML 生产力工具打开 Office 文档
  4. 单击“反映代码”
  5. 在右侧,您现在将看到文档反映为 C# 代码。

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.

  1. Create a new office document
  2. Add the parts to the document which you would like to reproduce with OpenXML SDK.
  3. Open the office document with Microsoft OpenXML productivity tool
  4. Click on "Reflect Code"
  5. On the right side you will see now you document reflected into C# code.
风向决定发型 2024-09-09 08:44:17

我有同样的问题,在我的例子中 标签有效。

I have the same issue and in my case <w:br /> tag worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文