none
如何利用OpenXML SDK 2.0 编程向docx写入含有强制换行符 的字符 RRS feed

  • 问题

  • 如:C# : string  stringIncludeWrap = "第一行的字\n第二行的字"; 
    其中 \n 代表换行 写入到word 2007 文件中 就是这样的结果:
    第一行的字
    第二行的字
    问题:如何利用OpenXML SDK 2.0 编程达到这样的效果呢,  WordWrap可以么
    下面代码是生成 元素 供参考
    public static Paragraph GenerateParagraph(string stringIncludeWrap)
            {
                var element =
                    new Paragraph(
                        new Run(
                            new Text(stringIncludeWrap )
                        ) { RsidRunAddition = "008E3205" });
                return element;
            }

    IT Professional
    2009年11月4日 14:08

答案

  • 你好

    Markup

    <w:p w:rsidRPr="00CF4A05" w:rsidR="00FD48A2" w:rsidP="00CF4A05" w:rsidRDefault="00A03D24" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
      <w:r w:rsidRPr="00CF4A05">
        <w:t>This is the first line</w:t>
      </w:r>
      <w:r w:rsidRPr="00CF4A05">
        <w:br />
        <w:t>This is the second line</w:t>
      </w:r>
    </w:p>

    Equivalent code


            public static Paragraph GenerateParagraph()
            {
                var element =
                    new Paragraph(
                        new Run(
                            new Text("This is the first line")
                        ){ RsidRunProperties = "00CF4A05" },
                        new Run(
                            new Break(),
                            new Text("This is the second line")
                        ){ RsidRunProperties = "00CF4A05" }
                    ){ RsidParagraphMarkRevision = "00CF4A05", RsidParagraphAddition = "00FD48A2", RsidParagraphProperties = "00CF4A05", RsidRunAdditionDefault = "00A03D24" };
                return element;
            }

    OR  split these in two separate paragraphs. Effect is same , but the difference comes when we start applying styles to these.

    Like this
      public static Paragraph GenerateParagraph()
            {
                var element =
                    new Paragraph(
                        new Run(
                            new Text("This is the first line")
                        ){ RsidRunProperties = "00CF4A05" }
                    ){ RsidParagraphMarkRevision = "00CF4A05", RsidParagraphAddition = "00A03D24", RsidParagraphProperties = "00CF4A05", RsidRunAdditionDefault = "00A03D24" };
                return element;
            }


            public static Paragraph GenerateParagraph()
            {
                var element =
                    new Paragraph(
                        new Run(
                            new Text("This is the second line")
                        ){ RsidRunProperties = "00CF4A05" }
                    ){ RsidParagraphMarkRevision = "00CF4A05", RsidParagraphAddition = "00A03D24", RsidParagraphProperties = "00CF4A05", RsidRunAdditionDefault = "00A03D24" };
                return element;
            }

    谢谢


    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    2009年11月6日 11:34