C# &引用\r\n“;在word文档C中显示为小方形框#

C# &引用\r\n“;在word文档C中显示为小方形框#,c#,ms-word,aspose,C#,Ms Word,Aspose,我正在运行时将一些包含“\r\n”的文本附加到word文档中。 但当我看到word文档时,它们被替换为小方形框:-( 我试着用System.Environment.NewLine替换它们,但仍然看到了这些小框 有什么想法吗?你没有试过一种或另一种方法吗?例如,\r或\n,因为Word将分别解释回车符和换行符。唯一一次使用环境。换行符位于纯ASCII文本文件中。Word将以不同的方式处理这些字符!甚至是Ctrl+M序列。如果需要,请尝试它不起作用,请发布代码。答案是使用\v-这是一个段落分隔符。W

我正在运行时将一些包含“\r\n”的文本附加到word文档中。 但当我看到word文档时,它们被替换为小方形框:-(

我试着用
System.Environment.NewLine
替换它们,但仍然看到了这些小框


有什么想法吗?

你没有试过一种或另一种方法吗?例如,
\r
\n
,因为Word将分别解释回车符和换行符。唯一一次使用环境。换行符位于纯ASCII文本文件中。Word将以不同的方式处理这些字符!甚至是Ctrl+M序列。如果需要,请尝试它不起作用,请发布代码。

答案是使用
\v
-这是一个段落分隔符。

Word使用
XML元素进行换行。

经过多次尝试和错误后,下面是一个为Word XML节点设置文本并处理多行的函数:

//Sets the text for a Word XML <w:t> node
//If the text is multi-line, it replaces the single <w:t> node for multiple nodes
//Resulting in multiple Word XML lines
private static void SetWordXmlNodeText(XmlDocument xmlDocument, XmlNode node, string newText)
{

    //Is the text a single line or multiple lines?>
    if (newText.Contains(System.Environment.NewLine))
    {
        //The new text is a multi-line string, split it to individual lines
        var lines = newText.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);


        //And add XML nodes for each line so that Word XML will accept the new lines
        var xmlBuilder = new StringBuilder();
        for (int count = 0; count < lines.Length; count++)
        {
            //Ensure the "w" prefix is set correctly, otherwise docFrag.InnerXml will fail with exception
            xmlBuilder.Append("<w:t xmlns:w=\"http://schemas.microsoft.com/office/word/2003/wordml\">");
            xmlBuilder.Append(lines[count]);
            xmlBuilder.Append("</w:t>");

            //Not the last line? add line break
            if (count != lines.Length - 1)
            {
                xmlBuilder.Append("<w:br xmlns:w=\"http://schemas.microsoft.com/office/word/2003/wordml\" />");
            }
        }

        //Create the XML fragment with the new multiline structure
        var docFrag = xmlDocument.CreateDocumentFragment();
        docFrag.InnerXml = xmlBuilder.ToString();
        node.ParentNode.AppendChild(docFrag);

        //Remove the single line child node that was originally holding the single line text, only required if there was a node there to start with
        node.ParentNode.RemoveChild(node);
    }
    else
    {
        //Text is not multi-line, let the existing node have the text
        node.InnerText = newText;
    }
}
//设置Word XML节点的文本
//如果文字为多行文字,则会将单个节点替换为多个节点
//导致多个Word XML行
私有静态void SetWordXmlNodeText(XmlDocument XmlDocument,XmlNode节点,字符串newText)
{
//文本是单行还是多行?>
if(newText.Contains(System.Environment.NewLine))
{
//新文本是多行字符串,将其拆分为单独的行
var lines=newText.Split(“\n\r.ToCharArray(),StringSplitOptions.RemoveEmptyEntries”);
//并为每行添加XML节点,以便Word XML接受新行
var xmlBuilder=新的StringBuilder();
对于(int count=0;count
我们需要知道您是如何“添加一些文本”的。很难说问题出在哪里。您使用wingdings吗?谢谢大家,我使用了Aspose.Words进行字符串操作并复制到新文档中。strText.Replace(“\r\n”,Aspose.Words.ControlChar.LineBreak)解决了这个问题。谢谢!ps-我意识到这是一年前提出的。我只是想这会帮助其他人寻找答案。