+-
如何使用C#和OpenXml访问Word文档中的大纲编号?
我正在尝试将Microsoft Word 2010中的大纲传输到Microsoft Excel 2010中的电子表格.我正在使用DocumentFormat.OpenXml.Packing和Documentformat.OpenXml.Wordprocessing

我得到文档的主体,并使用它来获取所有段落对象的列表:

var allParagraphs = new List<Paragraph>();
WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(wordDocPath.Text, false);
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
allParagraphs = body.OfType<Paragraph>().ToList();

但是我似乎找不到任何存储该段落旁边的大纲编号的东西.我是否需要抓住文档中段落以外的其他对象以获得每个段落的轮廓编号(如果有)?

我说的轮廓编号出现在以下屏幕快照中这些标题的左侧:

不幸的是,即使我知道ParagraphProperties.OutlineLevel是word文档中大纲的一部分,它也是null.

最佳答案
既然我已经确切地了解了您想要什么,那么这就是您应该如何解决问题的方法.

首先,我建议您从here下载Open XML Productivity工具.一旦知道文件的底层xml看起来是什么样,就很容易解决该问题.

<w:p w:rsidR="004265BF" w:rsidP="00AD13B6" w:rsidRDefault="00AD13B6">
 <w:pPr>
  <w:pStyle w:val="ListParagraph" />
  <w:numPr>
   <w:ilvl w:val="0" />
   <w:numId w:val="2" />
  </w:numPr>
 </w:pPr>
 <w:r>
  <w:t>Requirements</w:t>
 </w:r>
</w:p>
<w:p w:rsidR="00AD13B6" w:rsidP="00AD13B6" w:rsidRDefault="00AD13B6">
 <w:pPr>
  <w:pStyle w:val="ListParagraph" />
   <w:numPr>
    <w:ilvl w:val="1" />
    <w:numId w:val="2" />
   </w:numPr>
  </w:pPr>
  <w:r>
   <w:t>Performance</w:t>
  </w:r>
</w:p>

在上面可以看到仅几段的XML.每个段落都有对应的,其中包含.

每个Word文档都包含许多不同的XML文件,这些文件用作对整个文档正文中使用的样式和值的引用.有关轮廓,请参见Numbering.xml.

这里的每个numId都引用numbering.xml中的AbstractNumId,而后者又引用了同一文件中的abstractNum.您可以从那里获取您的提纲编号.

我知道这听起来可能很乏味,但这是唯一的方法.

.

祝一切顺利!

using (WordprocessingDocument doc = WordprocessingDocument.Open("word-wrP.docx", true))
        {
            Body body = doc.MainDocumentPart.Document.Body;

            //Documents' numbering definition
            Numbering num = doc.MainDocumentPart.NumberingDefinitionsPart.Numbering;

            //Get all paragraphs in the document
            IEnumerable<Paragraph> paragraphs = doc.MainDocumentPart.Document.Body.OfType<Paragraph>();
            foreach (Paragraph paragraph in paragraphs)
            {
                int tempLevel = 0; 

                //Each paragraph has a reference to a numbering definition that is defined by the numbering ID
                NumberingId numId = paragraph.ParagraphProperties.NumberingProperties.NumberingId;

                //NumberingLevelReference defines the outline level or the "indent" of Numbering, index starts at Zero.
                NumberingLevelReference iLevel =
                    paragraph.ParagraphProperties.NumberingProperties.NumberingLevelReference;

                //From the numbering reference we get the actual numbering definition to get start value of the outline etc etc.
                var firstOrDefault =
                    num.Descendants<NumberingInstance>().FirstOrDefault(tag => tag.NumberID == (int)numId.Val);
                if (firstOrDefault != null)
                {
                    var absNumId =
                        firstOrDefault.GetFirstChild<AbstractNumId>();
                    AbstractNum absNum =
                        num.OfType<AbstractNum>().FirstOrDefault(tag => tag.AbstractNumberId == (int)absNumId.Val);
                    if (absNum != null)
                    {
                        StartNumberingValue start = absNum.OfType<StartNumberingValue>().FirstOrDefault();
                        // once you have the start value its just a matter of counting the paragraphs that have the same numberingId and from the Number
                        //ingLevel you can calculate the actual values that correspond to each paragraph.
                        if (start != null) startValue = start.Val;
                    }
                }
                else
                {
                    Console.WriteLine("Failed!");
                }
            }
        }
点击查看更多相关文章

转载注明原文:如何使用C#和OpenXml访问Word文档中的大纲编号? - 乐贴网