从 Word 文档中获取列表编号?

Kev*_*vin 3 c# office-interop winforms

我正在使用 Microsoft.Office.Interop.Word 来解析 Word 2010 文档。我从每个页面上每个表格的第一列中的每个单元格中获取所有文本。但是,我遇到的问题是,当我收到文本时,它不包括列表编号。例如,我表中的文本如下所示: 在此处输入图片说明

我的程序循环遍历文档并从第一列中的每个单元格中获取文本。不过,我得到的不是“1. Introduction”,而是“Introduction”。这是我得到的数据:

在此处输入图片说明

如您所见,我没有得到列表编号,只有文本(即“简介”而不是“1.简介”)。

这是我用来获取数据的循环:

        // Loop through each table in the document, 
        // grab only text from cells in the first column
        // in each table.
        foreach (Table tb in docs.Tables)
        {
            for (int row = 1; row <= tb.Rows.Count; row++)
            {
                var cell = tb.Cell(row, 1);
                var text = cell.Range.Text;
                dt.Rows.Add(text);
            }
        }
Run Code Online (Sandbox Code Playgroud)

有人可以提供有关如何从每个单元格中获取列表编号以及文本的任何指示吗?我想它会是这样的:

var text = cell.Range.ListNumber + " " + cell.Range.Text;
Run Code Online (Sandbox Code Playgroud)

......但我无法弄清楚,确切地说。

Kev*_*vin 5

找到了答案。我必须获得 ListString 值:

        // Loop through each table in the document, 
        // grab only text from cells in the first column
        // in each table.
        foreach (Table tb in docs.Tables)
        {
            for (int row = 1; row <= tb.Rows.Count; row++)
            {
                var cell = tb.Cell(row, 1);
                var listNumber = cell.Range.ListFormat.ListString;
                var text = listNumber + " " + cell.Range.Text;

                dt.Rows.Add(text);
            }
        }
Run Code Online (Sandbox Code Playgroud)