我有一个包含一些标题的大型word文档。这些标题分别作为一个孩子的一张桌子。 (如屏幕截图所示)
因此我使用了Microsoft Interop.Word库。我的代码看起来像这样。如何获得标题段落的子项?也许有更好的方法来做到这一点。
Application word = new Application();
Document doc = new Document();
object missing = System.Type.Missing;
doc = word.Documents.Open(ref m_FileName,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
foreach (Paragraph paragraph in doc.Paragraphs)
{
Style style = paragraph.get_Style() as Style;
string text = paragraph.Range.Text;
paragraph.Range.Tables // does not get the table under the paragraph
}
答案 0 :(得分:0)
我是使用范围来做的。找到第一个标题,找到下一个标题(或任何你可以用作本章末尾的内容并获取其中的内容:
Range r1 = doc.Content;
Range r2 = doc.Content;
r1.Find.Execute("Heading 1");
r2.Find.Execute("Heading 2");
Range chapter = doc.Range(r1.Start, r2.Start);
//Console.WriteLine(chapter.Text);
foreach (Table t in chapter.Tables)
{
foreach(Row r in t.Rows)
{
foreach (Cell c in r.Cells)
{
//Console.WriteLine(c.Range.Text);
}
}
}