Jak*_*ith 3 c# excel ms-word openxml
我正在尝试将大纲传输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();
Run Code Online (Sandbox Code Playgroud)
但我似乎找不到任何存储段落旁边的大纲编号的内容。除了文档中的段落之外,我是否需要抓取其他对象来获取每个段落的大纲编号(如果有的话)?
我所说的大纲编号出现在下面屏幕截图中这些标题的左侧:

不幸的ParagraphProperties.OutlineLevel是,即使我知道它是 word 文档中大纲的一部分,它也是空的。
现在我已经完全了解您想要什么,下面是您应该如何解决您的问题。
首先,我建议您从这里下载 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>
Run Code Online (Sandbox Code Playgroud)
您可以在上面看到仅几段的 XML。每个段落都有其对应的<w:numPr>和包含<w:numId>.
每个 word 文档都包含许多不同的 XML 文件,这些文件充当对整个文档正文中使用的样式和值的引用。对于大纲,有 Numbering.xml。
这里的每个 numId 指的是 numbering.xml 中的 AbstractNumId,而后者又指的是同一文件中的 abstractNum。您可以从那里获取您的 Outline 编号。
我知道这听起来可能很乏味,但这是唯一可以做到的方法。
.
祝一切顺利!
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!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3414 次 |
| 最近记录: |