【问题标题】:nvarchar(max) from SQL Server DB to XML Word Doc not preserving carriage return从 SQL Server DB 到 XML Word Doc 的 nvarchar(max) 不保留回车
【发布时间】:2015-10-23 02:59:18
【问题描述】:

我编写了一个程序,它可以从 SQL Server 表中提取信息并将信息放入 MS WORD 中。

foreach (String note in doc_object.alJobNotes)
{
    String formattednote = "";
    newTextElement = xdoc.CreateElement("w:t", wordmlNamespace);
    formattednote = note.Replace(Environment.NewLine, "
");
    XmlText newText = xdoc.CreateTextNode(formattednote);
    newTextElement.AppendChild(newText);
    XmlAttribute xmlSpace = xdoc.CreateAttribute(
                        "xml", "space",
                        "http://www.w3.org/XML/1998/namespace");
    xmlSpace.Value = "preserve";
    newTextElement.Attributes.Append(xmlSpace);
    ChildNode.ParentNode.InsertAfter(newTextElement, ChildNode);
    EmptyElement = xdoc.CreateElement("w:br", wordmlNamespace);

    EmptyElement.Attributes.Append(xmlSpace);

    ChildNode.ParentNode.InsertAfter(EmptyElement, ChildNode);
    ChildNode.ParentNode.InsertAfter(EmptyElement, ChildNode);
    ChildNode.ParentNode.InsertAfter(EmptyElement, ChildNode);
    ChildNode.ParentNode.InsertAfter(EmptyElement, ChildNode);
}

但是,当我打开 Ms Word 时,回车的地方只显示

我尝试将其替换为
(char)13(char)10<br/><w:cr>,但我找不到如何操作。

有什么建议吗?

【问题讨论】:

    标签: c# sql-server xml


    【解决方案1】:

    好吧,如果我正确解释了您的代码和问题描述,我相信问题在于您没有生成有效的 WordOpenXML。 (我假设这是 docx 文件格式!)

    查看您的代码发出的原始 XML,并将其与 ZIP 包中的示例 Word 文档的 document.xml 文件进行比较。

    您的代码似乎将新行添加为 w:t 元素的一部分。但这不是 WordOpenXML 的设计方式。 WordOpenXML 更像这样,其中 w:p 是段落(“回车”/ANSI 13):

    <w:p>
      <w:r>
         <w:t>text here</w:t>
      </w:r>
    </w:p>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多