简体   繁体   English

c# 从word文档中的特定表和多列读取数据

[英]c# read data from specific table and multiple columns in word document

I need to read data from a word document that has 3 tables with multiple columns.我需要从一个包含 3 个多列表格的 word 文档中读取数据。

I managed to read data for all my table and show them in datagridView but it only shows data from column1 and i dont know how to distinguish between the different tables in the document.我设法读取了我所有表的数据并将它们显示在 datagridView 中,但它只显示来自 column1 的数据,我不知道如何区分文档中的不同表。

I am using the assembly using Microsoft.Office.Interop.Word;我正在using Microsoft.Office.Interop.Word;

my code我的代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    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();
        textBox1.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);

        dataGridView1.Columns.Add("explaineText", "col1");
        dataGridView1.Columns.Add("CustomerText", "col2");


        foreach (Table tb in docs.Tables)
        //Is it possible to add a condition statement here like 
        //if(tb{number} = 1){ do something } 
        {
            for (int row = 1; row <= tb.Rows.Count; row++)
            {
                var cell = tb.Cell(row, 1);
                var text = cell.Range.Text;
                //here i try to assing col to column 2 

                var col = tb.Cell(row, 2);
                var custtext = cell.Range.Text;
                //here i try to add the text from column2 in dataGridView 
                dataGridView1.Rows.Add(text, custtext);
            }
            // text now contains the content of the cell.
        }

        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();
    }
}

Try using these loops inside your Tables loop:尝试在 Tables 循环中使用这些循环:

foreach(Microsoft.Office.Interop.Word.Row row in tb.Rows) 
{
    foreach(Microsoft.Office.Interop.Word.Cell cell in row.Cells) 
    {
        //do something with all the cells
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM