无法使用OpenXml为单词添加项目符号

时间:2011-11-28 10:04:56

标签: openxml docx openxml-sdk

我的预期结果是:

  • 你好
  • 世界!

但是当我使用以下代码时:

        MainDocumentPart mainDocumentPart =
          package.AddMainDocumentPart();

        DocumentFormat.OpenXml.Wordprocessing.Document elementW =
          new DocumentFormat.OpenXml.Wordprocessing.Document(
            new Body(
              new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
                    new NumberingProperties(
                      new NumberingLevelReference() { Val = 0 },
                      new NumberingId() { Val = 1 })
                    ),
                new Run(
                  new RunProperties(),
                  new Text("Hello, ") { Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues> { InnerText = "preserve" } })),
              new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
                new ParagraphProperties(
                  new NumberingProperties(
                    new NumberingLevelReference() { Val = 0 },
                    new NumberingId() { Val = 1 })),
                new Run(
                  new RunProperties(),
                  new Text("world!")
                  {
                      Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues> { InnerText = "preserve" }
                  })));

        elementW.Save(mainDocumentPart);

结果是:

  1. 你好
  2. 世界!
  3. 我如何得到预期的结果?

2 个答案:

答案 0 :(得分:7)

我意识到这已经太晚了,但也许它可以帮助其他人提出同样的问题。明确的答案(由amurra)实际上并没有达到预期的结果。它只是创建一个文档,列表作为内容,比你更完整。您添加到主文档部分的内容很好。

在XML格式中,列表项被定义为具有缩进级别和编号ID的段落。此ID引用文档的NumberingDefinitionsPart中定义的编号规则。

在您的情况下,由于您已将编号ID设置为1,因此以下代码会将该ID映射为1,以根据需要反映项目符号列表。请注意Level对象中的NumberingFormat和LevelText对象。这些是格式化的关键组件。

NumberingDefinitionsPart numberingPart =
    mainDocumentPart.AddNewPart<NumberingDefinitionsPart>("myCustomNumbering");

Numbering numElement = new Numbering(
    new AbstractNum(
        new Level(
            new NumberingFormat() { Val = NumberFormatValues.Bullet },
            new LevelText() { Val = "·" }
        ) { LevelIndex = 0 }
    ) { AbstractNumberId = 0 },
    new NumberingInstance(
        new AbstractNumId(){ Val = 0 }
    ){ NumberID = 1 }
);

numElement.Save(numberingPart);

有关详细信息,请查看Wordprocessing Namespace on MSDNWorking With Numbering标记文章中所有相关课程的文档。

答案 1 :(得分:2)

这应该会为您创建一个包含预期输出的空白文档:

        // Creates an Document instance and adds its children.
        public Document GenerateDocument()
        {
            Document document1 = new Document();
            document1.AddNamespaceDeclaration("ve", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            document1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
            document1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            document1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
            document1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
            document1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
            document1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
            document1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
            document1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");

            Body body1 = new Body();

            Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "00AF4948", RsidParagraphProperties = "00625634", RsidRunAdditionDefault = "00625634" };

            ParagraphProperties paragraphProperties1 = new ParagraphProperties();
            ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "ListParagraph" };

            NumberingProperties numberingProperties1 = new NumberingProperties();
            NumberingLevelReference numberingLevelReference1 = new NumberingLevelReference(){ Val = 0 };
            NumberingId numberingId1 = new NumberingId(){ Val = 1 };

            numberingProperties1.Append(numberingLevelReference1);
            numberingProperties1.Append(numberingId1);

            paragraphProperties1.Append(paragraphStyleId1);
            paragraphProperties1.Append(numberingProperties1);

            Run run1 = new Run();
            Text text1 = new Text();
            text1.Text = "Hello";

            run1.Append(text1);

            paragraph1.Append(paragraphProperties1);
            paragraph1.Append(run1);

            Paragraph paragraph2 = new Paragraph(){ RsidParagraphAddition = "00625634", RsidParagraphProperties = "00625634", RsidRunAdditionDefault = "00625634" };

            ParagraphProperties paragraphProperties2 = new ParagraphProperties();
            ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId(){ Val = "ListParagraph" };

            NumberingProperties numberingProperties2 = new NumberingProperties();
            NumberingLevelReference numberingLevelReference2 = new NumberingLevelReference(){ Val = 0 };
            NumberingId numberingId2 = new NumberingId(){ Val = 1 };

            numberingProperties2.Append(numberingLevelReference2);
            numberingProperties2.Append(numberingId2);

            paragraphProperties2.Append(paragraphStyleId2);
            paragraphProperties2.Append(numberingProperties2);

            Run run2 = new Run();
            Text text2 = new Text();
            text2.Text = "world!";

            run2.Append(text2);

            paragraph2.Append(paragraphProperties2);
            paragraph2.Append(run2);

            SectionProperties sectionProperties1 = new SectionProperties(){ RsidR = "00625634", RsidSect = "00AF4948" };
            HeaderReference headerReference1 = new HeaderReference(){ Type = HeaderFooterValues.Even, Id = "rId7" };
            HeaderReference headerReference2 = new HeaderReference(){ Type = HeaderFooterValues.Default, Id = "rId8" };
            FooterReference footerReference1 = new FooterReference(){ Type = HeaderFooterValues.Even, Id = "rId9" };
            FooterReference footerReference2 = new FooterReference(){ Type = HeaderFooterValues.Default, Id = "rId10" };
            HeaderReference headerReference3 = new HeaderReference(){ Type = HeaderFooterValues.First, Id = "rId11" };
            FooterReference footerReference3 = new FooterReference(){ Type = HeaderFooterValues.First, Id = "rId12" };
            PageSize pageSize1 = new PageSize(){ Width = (UInt32Value)12240U, Height = (UInt32Value)15840U };
            PageMargin pageMargin1 = new PageMargin(){ Top = 1440, Right = (UInt32Value)1440U, Bottom = 1440, Left = (UInt32Value)1440U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U };
            Columns columns1 = new Columns(){ Space = "720" };
            DocGrid docGrid1 = new DocGrid(){ LinePitch = 360 };

            sectionProperties1.Append(headerReference1);
            sectionProperties1.Append(headerReference2);
            sectionProperties1.Append(footerReference1);
            sectionProperties1.Append(footerReference2);
            sectionProperties1.Append(headerReference3);
            sectionProperties1.Append(footerReference3);
            sectionProperties1.Append(pageSize1);
            sectionProperties1.Append(pageMargin1);
            sectionProperties1.Append(columns1);
            sectionProperties1.Append(docGrid1);

            body1.Append(paragraph1);
            body1.Append(paragraph2);
            body1.Append(sectionProperties1);

            document1.Append(body1);
            return document1;
        }