读取 Word table 列中的单元格

Read cells in a Word table column

我想遍历 table 中的行并同时从一列中读取数据。需要能够按 table 中的列分隔数据,这样我就可以 select ex。 table 1 来自第 1、2 和 4 列的数据

我的代码

{
    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");
                //dataGridView1.Columns.Add("CustomerText", "col5");
                int ntab = 5;
                Table t = docs.Tables[ntab];
                Range r = t.Range;
                int y = 1;
                int i = 1;
                Cells cells = r.Cells;
                while (i <= cells.Count)
                {
                    string txt = "";
                    string txt2 = "";


                        if (ntab != 5)
                        {
                        i = i + 2;
                        y = i + 1;
                        }
                        if(ntab == 5)
                        {
                        i = i + 6;
                        y = i + 4;
                        }
                   //      Debug.WriteLine("iteration" + i + " i= " + i + "y= " + y);


                        try
                        {
                            Cell cell = cells[i];
                            Cell cell2 = cells[y];
                            Range r2 = cell.Range;
                            Range r3 = cell2.Range;
                            txt = r2.Text;
                            txt2 = r3.Text;
                        }


                        dataGridView1.Rows.Add(txt, txt2);

                }


                                              ((_Document)docs).Close();
                ((_Application)word).Quit();
            }

        }
    }
}

如您所见,我尝试通过根据哪一列递增我的计数器来按列读取行数据 我想读书。

我知道有一个方法cell.split,但我没能实现它。

我确定一定有更好的方法来遍历行和列?

注意:我只是阅读文档,没有编辑。

我自己找到了答案,在我的实验中我只是将 table 数据附加到 用“|”分隔每一列的文本文件 在这里我可以迭代每一行中的每一列。

代码:

int ntab = 5;
        Table t = docs.Tables[ntab];
        Rows rows = t.Rows;
        Columns cols = t.Columns;

        for (int i = 1; i < rows.Count; i++) {
            string ftxt = "";
                for (int j = 1; j <= cols.Count; j++) {
                  Cell cell = rows[i].Cells[j];
                  Range r = cell.Range;
                string txt = r.Text;
                ftxt = ftxt + " | " + txt; 

            }
            File.AppendAllText(@"c:\temp\test.txt", ftxt.ToString() + Environment.NewLine);
            Console.WriteLine(ftxt);

        }
        Console.WriteLine("\n");


        ((_Document)docs).Close();
        ((_Application)word).Quit();



    }