openXmlSdk 在 运行 元素中插入新行
openXmlSdk insert new line inside of a Run Element
我在 Run
元素中有文本。我正在尝试用 line break
替换字符串中的 \r
。
正文如下
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
并且 Run
元素的 innerXml
被翻译成
<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.<w:br /><w:br />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("<w:br />", "<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.
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();
}
}
}
}
<?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>
我在 Run
元素中有文本。我正在尝试用 line break
替换字符串中的 \r
。
正文如下
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
并且 Run
元素的 innerXml
被翻译成
<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.<w:br /><w:br />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("<w:br />", "<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.
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();
}
}
}
}
<?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>