How to Read Table Data in Word Document using C#

In this article, we are going to describe an example that will read the data from table available in Word document by using C# and then this data will be stored in C# data table. Here is the snap shot of a sample word document located at path D:\TEST\Employee Details.docx. This contains a table of employee details.

To read the details of each employee from this table by C# code program, you need to follow the below given steps.

Step 1: Drag a dataGridView component and a button to the form and set the text property of button to Read Word.

Step 2: In the Solution Explorer window, right-click the project then click Add Reference. In .NET tab, select Microsoft.Office.Interop.Word component and then click OK.

Step 3: Now check that Microsoft.Office.Interop.Word has been added under the references.

Step 4: Add the Word reference to your Class by placing this code in the list of namespaces.

using Microsoft.Office.Interop.Word;

Step 5: Declare a DataTable object dtgrid at the class level.

DataTable dtgrid = new DataTable();

Step 6: Define the readTableFromWord() function as shown below.

public void readTableFromWord()
        {
            Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
            Document document = application.Documents.Open(@"D:\TEST\Employee Details.docx");

            foreach (Microsoft.Office.Interop.Word.Row row in document.Tables[1].Rows)
            {
                // if it first row, it will be the header row
                if (row.IsFirst)
                {
                    // this foreach loop will iterate through every cell of first row and will create column of DataTable(dtgrid)
                    foreach (Microsoft.Office.Interop.Word.Cell cell in row.Cells)
                    {
                        dtgrid.Columns.Add(cell.Range.Text.Replace("\r\a", "").Replace("\a", "").ToString());
                    }

                }                
                else
                {
                    // in this code block, every cell of word table rows will be iterated and their values will be inserted in
                    // respective cells of DataTable(dtgrid)
                    DataRow dr = dtgrid.NewRow();
                    List<string> rows = new List<string>();
                    foreach (Microsoft.Office.Interop.Word.Cell cell in row.Cells)
                    {
                        rows.Add(cell.Range.Text.Replace("\r\a", "").Replace("\a", "").ToString());
                    }

                    for (int i = 0; i < dtgrid.Columns.Count; i++)
                    {
                        dr[i] = rows[i];
                    }
                    dtgrid.Rows.Add(dr);
                }


            }

            // data binding of dtgrid data to dataGridView1
            dataGridView1.DataSource = dtgrid;
            document.Close();
            application.Quit();
        }

Step 7: Call this function in Click event of button1.

readTableFromWord();

Step 8: Build the application and run.

Step 9: Click on the Read Word button.

Step 10: You will see the output as below.

Leave a Reply

Your email address will not be published.