3

我正在使用 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);
            }
        }

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

var text = cell.Range.ListNumber + " " + cell.Range.Text;

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

4

1 回答 1

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);
            }
        }
于 2013-07-23T14:06:26.353 回答