gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:44:24 24 4
gpt4 key购买 nike

我正在使用 Microsoft.Office.Interop.Word 来解析 Word 2010 文档。我正在抓取每一页上每个表格第一列中每个单元格中的所有文本。不过,我遇到的问题是,当我收到文本时,它不包括列表编号。例如,我的表格中的文本如下所示: enter image description here

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

enter image description here

如您所见,我没有得到列表编号,只有文本(即“介绍”而不是“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;

...但我无法准确地弄明白。

最佳答案

找到了答案。我必须获得 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);
}
}

关于c# - 从 Word 文档中获取列表编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17810974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com