通过OpenXML在Word 2007中创建两个项目符号列表

时间:2012-06-26 09:08:01

标签: ms-word openxml wordprocessingml

我希望输出两个项目符号列表,如图所示:

enter image description here

但我得到的是这两个列表都有第二个(黑色圆圈)皮肤。

我的代码:

private static void AddNumberingDefinition(WordprocessingDocument docx)
        {
            NumberingDefinitionsPart numberingPart =
  docx.MainDocumentPart.AddNewPart<NumberingDefinitionsPart>();

            Numbering element =
              new Numbering(
                //     new Nsid(){ Val="FFFFFF80"},
                new AbstractNum(new Nsid() { Val = "FFFFFF80" },
                    new MultiLevelType() { Val = MultiLevelValues.HybridMultilevel },
                new TemplateCode() { Val = "4970B4E6" },
                  new Level(
                    new NumberingFormat() { Val = NumberFormatValues.Bullet },
                    new LevelText() { Val = "°" },//char.ConvertFromUtf32(61616) }
                    new LevelJustification() { Val = LevelJustificationValues.Left }//,
                //  new RunProperties() { RunFonts = new RunFonts() { Hint = FontTypeHintValues.Default, Ascii = "Symbol", 
//HighAnsi = "Symbol" } }
                  ) { LevelIndex = 0 }
                ) { AbstractNumberId = 0 },
                new NumberingInstance(
                  new AbstractNumId() { Val = 0 }
                ) { NumberID = 1 },

           // element.Save(numberingPart);

         //   element = new Numbering(
            new AbstractNum(new Nsid() { Val = "FFFFFF89" },
                new MultiLevelType() { Val = MultiLevelValues.HybridMultilevel },
                new TemplateCode() { Val = "4970B4E6" },
                  new Level(
                    new NumberingFormat() { Val = NumberFormatValues.Bullet },
                    new LevelText() { Val = "•" },//char.ConvertFromUtf32(61616) }
                    new LevelJustification() { Val = LevelJustificationValues.Left }//,
                //    new RunProperties() { RunFonts = new RunFonts() { Hint = FontTypeHintValues.Default, Ascii = "Symbol",
// HighAnsi = "Symbol" } }
                  ) { LevelIndex = 0 }
                ) { AbstractNumberId = 1 },
                new NumberingInstance(
                  new AbstractNumId() { Val = 1 }
                ) { NumberID = 2 });

            element.AddNamespaceDeclaration("ve", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            element.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
            element.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            element.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
            element.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
            element.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
            element.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
            element.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
            element.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");

            element.Save(numberingPart);
        }

        private static Paragraph GenerateParagraph()
        {
            return new Paragraph(
           new ParagraphProperties(
             new NumberingProperties(
               new NumberingLevelReference() { Val = 0 },
               new NumberingId() { Val = 1 })),
           new Run(
             new RunProperties(),
             new Text("Hello, Wordl!"))
             );
        }

        private static Paragraph GenerateParagraph2()
        {
            return new Paragraph(
           new ParagraphProperties(
             new NumberingProperties(
               new NumberingLevelReference() { Val = 0 },
               new NumberingId() { Val = 2 })),
           new Run(
             new RunProperties(),
             new Text("Hello, Wordl!"))
             );
        }

我不知道,隐藏的问题在哪里?我使用Open XML生产力工具来比较我生成的文件和手动创建的文件,但我无法让它工作。提前感谢任何提示。

1 个答案:

答案 0 :(得分:7)

几个小时后我发现,插入AbstractNum和NumberingInstance对象的顺序很重要。

Open XML SDK 2.0 Productivity Tools有一个Validate方法,它向我显示了验证错误。

工作代码:

        private static void AddNumberingDefinition(WordprocessingDocument docx)
        {
            NumberingDefinitionsPart numberingPart =
  docx.MainDocumentPart.AddNewPart<NumberingDefinitionsPart>();

            var a = new AbstractNum(new Nsid() { Val = "FFFFFF80" },
                  new MultiLevelType() { Val = MultiLevelValues.HybridMultilevel },
              new TemplateCode() { Val = "4970B4E6" },
                new Level(
                  new NumberingFormat() { Val = NumberFormatValues.Bullet },
                  new LevelText() { Val = "°" },
                  new LevelJustification() { Val = LevelJustificationValues.Left }
                ) { LevelIndex = 0 }
              ) { AbstractNumberId = 0 };

            var a1 = new NumberingInstance(
                 new AbstractNumId() { Val = 0 }
               ) { NumberID = 1 };

            var b = new AbstractNum(new Nsid() { Val = "FFFFFF89" },
                  new MultiLevelType() { Val = MultiLevelValues.HybridMultilevel },
                  new TemplateCode() { Val = "4970B4E6" },
                    new Level(
                      new NumberingFormat() { Val = NumberFormatValues.Bullet },
                      new LevelText() { Val = "•" },
                      new LevelJustification() { Val = LevelJustificationValues.Left }
                    ) { LevelIndex = 0 }
                  ) { AbstractNumberId = 1 };
            var b1 = new NumberingInstance(
                 new AbstractNumId() { Val = 1 }
               ) { NumberID = 2 };

            Numbering element = new Numbering();

            element.AddNamespaceDeclaration("ve", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            element.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
            element.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            element.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
            element.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
            element.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
            element.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
            element.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
            element.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");

            element.Append(a);
            element.Append(b);
            element.Append(a1);
            element.Append(b1);
            element.Save(numberingPart);
        }