C# StringBuilder:我无法设置特定的 Microsoft Word 换行符
C# StringBuilder : i can't set a specific Microsoft Word line feed
我已经在 word 中创建了一个模板,基本上是一个 word 文档,其中包含一些要在 C# 控制台应用程序中替换的 [Something],我正在尝试将特定字符设置为换行符。
确实,在 word 中,根据您使用的换行符,它会允许或不允许分页到 table 单元格中。
非常清楚,当您点击显示所有字符按钮时,您可以看到所有特殊字符。
我要补充的是这个:good linefeed but, i tried all unicode character i found on wikipedia and i always have this kind of linefeed bad return
我使用这个库来操作 docx 文档:github.com/WordDocX/DocX
这是我在 DocX github 库的 "Examples" 项目中使用的代码:
private static void ModifyTemplate()
{
// Loading the template
using (DocX document = DocX.Load("D:\DocX-master\Examples\docs\Template.docx"))
{
var sb = new StringBuilder("");
sb.Clear();
sb.AppendLine("bla bla bla bla bla bla bla bla.");
//Testing all the unicode codes i found here : https://en.wikipedia.org/wiki/Newline
sb.Append("u000D\u000D").Append("u000A\u000A").Append("u0085\u0085").Append("u000B\u000B").Append("u000C\u000C").Append("u2028\u2028").Append("u2029\u2029");
sb.AppendLine("AppendLine");
document.ReplaceText("[test]", sb.ToString());
//Testing with a different encoding
String test = Encoding.UTF8.GetString(Encoding.ASCII.GetBytes(sb.ToString()));
document.ReplaceText("[test]", test);
#region Saving the modified template on the disk
// Save all changes to this document.
document.SaveAs(@"docs\Result.docx");
#endregion
}// Release this document from memory.
}
我的 word 模板基本上是一个新的 docx,里面有文本 [Test] 和 [Test2]
此代码的结果:
抱歉,我无法 post 图片...所以我无法向您展示最终结果...但相信我,none 这个 unicode 似乎产生了正确的结果
总而言之,无论我使用什么unicode代码,都不可能有好的return char
我应该使用什么 unicode 代码?或者我应该使用什么编码技巧来获得我寻找的换行字符?
看来您需要使用“InsertParagraph
”
Paragraph p1 = document.InsertParagraph();
p1.Append("New Para 1");
Paragraph p2 = document.InsertParagraph();
p2.Append("New Para 2");
Paragraph p3 = document.InsertParagraph();
p3.Append("New Para 3");
感谢 Aruk 的提示,我设法解决了这个问题。
通过在我想要的特定单元格上使用 InsertParagraph,我可以获得想要的换行符,一个 Word 可以理解和使用。
还有一个问题,我失去了我以前的标签的风格 [something],但这应该不是什么需要解决的问题
非常感谢
我已经在 word 中创建了一个模板,基本上是一个 word 文档,其中包含一些要在 C# 控制台应用程序中替换的 [Something],我正在尝试将特定字符设置为换行符。
确实,在 word 中,根据您使用的换行符,它会允许或不允许分页到 table 单元格中。
非常清楚,当您点击显示所有字符按钮时,您可以看到所有特殊字符。
我要补充的是这个:good linefeed but, i tried all unicode character i found on wikipedia and i always have this kind of linefeed bad return
我使用这个库来操作 docx 文档:github.com/WordDocX/DocX
这是我在 DocX github 库的 "Examples" 项目中使用的代码:
private static void ModifyTemplate()
{
// Loading the template
using (DocX document = DocX.Load("D:\DocX-master\Examples\docs\Template.docx"))
{
var sb = new StringBuilder("");
sb.Clear();
sb.AppendLine("bla bla bla bla bla bla bla bla.");
//Testing all the unicode codes i found here : https://en.wikipedia.org/wiki/Newline
sb.Append("u000D\u000D").Append("u000A\u000A").Append("u0085\u0085").Append("u000B\u000B").Append("u000C\u000C").Append("u2028\u2028").Append("u2029\u2029");
sb.AppendLine("AppendLine");
document.ReplaceText("[test]", sb.ToString());
//Testing with a different encoding
String test = Encoding.UTF8.GetString(Encoding.ASCII.GetBytes(sb.ToString()));
document.ReplaceText("[test]", test);
#region Saving the modified template on the disk
// Save all changes to this document.
document.SaveAs(@"docs\Result.docx");
#endregion
}// Release this document from memory.
}
我的 word 模板基本上是一个新的 docx,里面有文本 [Test] 和 [Test2]
此代码的结果:
抱歉,我无法 post 图片...所以我无法向您展示最终结果...但相信我,none 这个 unicode 似乎产生了正确的结果
总而言之,无论我使用什么unicode代码,都不可能有好的return char
我应该使用什么 unicode 代码?或者我应该使用什么编码技巧来获得我寻找的换行字符?
看来您需要使用“InsertParagraph
”
Paragraph p1 = document.InsertParagraph();
p1.Append("New Para 1");
Paragraph p2 = document.InsertParagraph();
p2.Append("New Para 2");
Paragraph p3 = document.InsertParagraph();
p3.Append("New Para 3");
感谢 Aruk 的提示,我设法解决了这个问题。
通过在我想要的特定单元格上使用 InsertParagraph,我可以获得想要的换行符,一个 Word 可以理解和使用。
还有一个问题,我失去了我以前的标签的风格 [something],但这应该不是什么需要解决的问题
非常感谢