2012-06-26 101 views
1

在Word 2007中創建兩個項目符號列表我想輸出的兩個符號像圖片上的列表:通過OpenXML的

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的高效工具來比較我生成的文件和手動創建的文件,但我無法使其工作。預先感謝任何提示。

回答

7

了幾個小時後,我發現,插入AbstractNum和NumberingInstance對象的順序是非常重要的..

的Open XML SDK 2.0生產力工具有一個驗證方法,它給我的驗證錯誤。

工作代碼:

 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); 
     }