如何使用C#和OpenXml访问Word文档中的大纲编号?

我正在尝试将Microsoft Word 2010的大纲转换为Microsoft Excel 2010的电子表格。 我正在使用DocumentFormat.OpenXml.PackingDocumentformat.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(); 

但我似乎无法find存储该段落旁边的大纲编号的任何内容。 除了文档中的段落外,是否还需要抓取其他对象以获取每个段落的大纲数字(如果有的话)?

我在说的大纲编号在下面的截图中出现在这些标题的左边:

轮廓的大纲编号

不幸的是, ParagraphProperties.OutlineLevel为空,尽pipe我知道它是word文档中大纲的一部分。

现在我已经明白你想要什么了,下面是你应该如何去解决你的问题。

首先,我build议你从这里下载Open XML Productivity工具。 一旦你知道了底层XML文件的样子,解决这个问题就变得非常简单了。

 <w:pw: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:pw: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。 每个段落都有其相应的内容。

每个单词文档都包含许多不同的XML文件,这些文件充当文档正文中使用的样式和值的引用。 对于大纲,有Numbering.xml。

这里的每个numId引用numbering.xml中的AbstractNumId,并依次引用同一文件中的abstractNum。 你可以从那里得到你的纲要号码。

我知道这听起来很乏味,但这是唯一可以做到的。

打开Xml生产力工具快照

祝一切顺利!

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