Read Table In Word Document Using C#

Office Open XML

 
Office Open XML is introduced by Microsoft to work with documents. For e.g.: - read/write MS word documents.
 
Prerequisites
Execute below command to Install DocumentFormat.OpenXml SDK in your project
  1. Install-Package DocumentFormat.OpenXml Install-Package DocumentFormat.OpenXml  
Note
In this example, I am using a windows form application to interact with word document and display the result on the screen.
 

How to find first table from word document?

  1. Table table =  doc.MainDocumentPart.Document.Body.Elements<Table>().First();  
Here .First() is extension method to find first table from word document.
 

The WordprocessingDocument Methods

 
EXTENSION METHOD
DESCRIPTION
.Elements<Table>().First()
To get the first table from the word document
.Elements<Table>().Last()
To get the last table from the word document
.Elements<Table>().FisrtOrDefault()
To get the first table from the word document or return the default value if the document does not contain table.
.Elements<Table>().LastOrDefault()
To get the Last table from word document or return the default value if the document does not contain table.
.Elements<Table>().ElementAt(Index)
To get the exact table from the word document by index number. Default index number is 0.
Table.Elements<TableRow>().ElementAt(index)
To get the exact table row from selected table by index number. Default index number is 0.
Row.Elements<TableCell>().ElementAt(Index)
To get the exact row cell from selected row by index number. Default index number is 0.
 
Execute the following code to read the first table from the word document,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Windows.Forms;  
  6. using DocumentFormat.OpenXml.Packaging;  
  7. using DocumentFormat.OpenXml.Wordprocessing;  
  8.   
  9. namespace ReadTable  
  10. {  
  11.     public partial class Form1 : Form  
  12.     {  
  13.         public Form1()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.   
  18.         // To browse and select files on a computer in an application  
  19.         private void btnBrowse_Click(object sender, EventArgs e)  
  20.         {  
  21.             DialogResult result = this.openDB.ShowDialog();  
  22.             if (result == DialogResult.OK)  
  23.             {  
  24.                 txtBrowse.Text = openDB.FileName;  
  25.             }  
  26.         }  
  27.   
  28.         private void btnReadTable_Click(object sender, EventArgs e)  
  29.         {  
  30.             // Open word document for read  
  31.             using (var doc = WordprocessingDocument.Open(txtBrowse.Text.Trim(), false))  
  32.             {  
  33.                 // To create a temporary table   
  34.                 DataTable dt = new DataTable();  
  35.                 int rowCount = 0;  
  36.   
  37.                 // Find the first table in the document.   
  38.                 Table table = doc.MainDocumentPart.Document.Body.Elements<Table>().First();  
  39.   
  40.                 // To get all rows from table  
  41.                 IEnumerable<TableRow> rows = table.Elements<TableRow>();  
  42.   
  43.                 // To read data from rows and to add records to the temporary table  
  44.                 foreach (TableRow row in rows)  
  45.                 {  
  46.                     if (rowCount == 0)  
  47.                     {  
  48.                         foreach (TableCell cell in row.Descendants<TableCell>())  
  49.                         {  
  50.                             dt.Columns.Add(cell.InnerText);  
  51.                         }  
  52.                         rowCount += 1;  
  53.                     }  
  54.                     else  
  55.                     {  
  56.                         dt.Rows.Add();  
  57.                         int i = 0;  
  58.                         foreach (TableCell cell in row.Descendants<TableCell>())  
  59.                         {  
  60.                             dt.Rows[dt.Rows.Count - 1][i] = cell.InnerText;  
  61.                             i++;  
  62.                         }  
  63.                     }  
  64.                 }  
  65.   
  66.                 // To display the result   
  67.                 // Bind datatable(temporary table) to the datagridview   
  68.                 dgvTable.DataSource = dt;  
  69.             }  
  70.         }  
  71.     }  
  72. }  
Output
 
 
In the above example, I have used .First() extension method to get the first table from word document. You can use other extension methods to find exact table or you can use for/foreach loop to find the exact table from the word document.
 

Summary

 
In this session, I discussed how to read the table from the word document using c#. I hope the above session will help you to read the table from the word document using C#.