C# 使用Microsoft.Office.Interop.Word分析表,是否仅从第一列获取文本?

C# 使用Microsoft.Office.Interop.Word分析表,是否仅从第一列获取文本?,c#,ms-office,office-interop,C#,Ms Office,Office Interop,我正在编写一个程序,该程序将解析来自MicrosoftWord2010文档的文本数据。具体来说,我希望从文档中每个表的第一列中的每个单元格中获取文本 作为参考,该文档如下所示: 我只需要每页第一列单元格中的文本。我将把这个文本添加到一个内部数据表中 到目前为止,我的代码如下所示: private void button1_Click(object sender, EventArgs e) { // Create an instance of the Open File

我正在编写一个程序,该程序将解析来自MicrosoftWord2010文档的文本数据。具体来说,我希望从文档中每个表的第一列中的每个单元格中获取文本

作为参考,该文档如下所示:

我只需要每页第一列单元格中的文本。我将把这个文本添加到一个内部数据表中

到目前为止,我的代码如下所示:

private void button1_Click(object sender, EventArgs e)
    {
        // Create an instance of the Open File Dialog Box
        var openFileDialog1 = new OpenFileDialog();

        // Set filter options and filter index
        openFileDialog1.Filter = "Word Documents (.docx)|*.docx|All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 1;
        openFileDialog1.Multiselect = false;

        // Call the ShowDialog method to show the dialog box.
        openFileDialog1.ShowDialog();
        txtDocument.Text = openFileDialog1.FileName;

        var word = new Microsoft.Office.Interop.Word.Application();
        object miss = System.Reflection.Missing.Value;
        object path = openFileDialog1.FileName;
        object readOnly = true;
        var docs = word.Documents.Open(ref path, ref miss, ref readOnly, 
                                       ref miss, ref miss, ref miss, ref miss, 
                                       ref miss, ref miss, ref miss, ref miss, 
                                       ref miss, ref miss, ref miss, ref miss, 
                                       ref miss);

        // Datatable to store text from Word doc
        var dt = new System.Data.DataTable();
        dt.Columns.Add("Text");

        // 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)
        {
            // insert code here to get text from cells in first column
            // and insert into datatable.
        }

        ((_Document)docs).Close();
        ((_Application)word).Quit();
    }
我被困在从每个单元格抓取文本并将其添加到数据表的部分。有人能给我一些建议吗?我当然很感激


谢谢

我不知道您希望如何将其存储在数据库中,但要阅读文本,我认为您可以循环出行并选择每行中的第一列:

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;

        // text now contains the content of the cell.
    }
}
foreach(docs.Tables中的表tb){
对于(int row=1;row