openXmlSdk在运行元素内插入新行

时间:2018-07-27 23:50:55

标签: c# openxml-sdk

我在Run元素中有文本。我正在尝试用\r替换字符串中的line break

文本如下

This is an example project for testing purposes. \rThis is all sample data, none of this is real information. \r\rThis field allows for the entry of more information, a larger text field for example purposes

innerXml元素的Run转换为

<w:rPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
   <w:rFonts w:cstheme="minorHAnsi" />
      </w:rPr>
        <w:t xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
           This is an example project for testing purposes.  This is all sample data, none of this is real information.&lt;w:br /&gt;&lt;w:br /&gt;This field allows for the entry of more information, a larger text field for example purposes.</w:t>

生成文档时不会插入换行符。

如何用换行符替换<w:t> </w:t>中的每个'\ r'?

我尝试过。

s.InnerXml = s.InnerXml.Replace("&lt;w:br /&gt;", "<w:br />");

我也尝试过直接将其替换为字符串,但这也不起作用。

它只是作为字符串出现

This is an example project for testing purposes.  This is all sample data, none of this is real information.<w:br xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" /><w:br xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" />This field allows for the entry of more information, a larger text field for example purposes.

1 个答案:

答案 0 :(得分:4)

The documentation指出Text元素包含文字。 SDK不会假设您写入Text元素的字符串中的换行符。怎么知道您要休息还是要段落?

如果要使用文字字符串构建文档,则需要做一些工作:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace OXmlTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var wordDocument = WordprocessingDocument
                .Create("c:\\deleteme\\testdoc.docx", WordprocessingDocumentType.Document))
            {
                MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

                mainPart.Document = new Document();
                Body body = mainPart.Document.AppendChild(new Body());
                Paragraph p = body.AppendChild(new Paragraph());                
                Run r = p.AppendChild(new Run());

                string theString = "This is an example project for testing purposes. \rThis is all sample data, none of this is real information. \r\rThis field allows for the entry of more information, a larger text field for example purposes";

                foreach (string s in theString.Split(new char[] { '\r' }))
                {
                    r.AppendChild(new Text(s));
                    r.AppendChild(new Break());
                }

                wordDocument.Save();
            }
        }
    }
}

Final Document

<?xml version="1.0" encoding="utf-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:body>
        <w:p>
            <w:r>
                <w:t>This is an example project for testing purposes. </w:t>
                <w:br />
                <w:t>This is all sample data, none of this is real information. </w:t>
                <w:br />
                <w:t/>
                <w:br />
                <w:t>This field allows for the entry of more information, a larger text field for example purposes</w:t>
                <w:br />
            </w:r>
        </w:p>
    </w:body>
</w:document>