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. namespace ReadTable
  9. {
  10. public partial class Form1 : Form
  11. {
  12. public Form1()
  13. {
  14. InitializeComponent();
  15. }
  16. // To browse and select files on a computer in an application
  17. private void btnBrowse_Click(object sender, EventArgs e)
  18. {
  19. DialogResult result = this.openDB.ShowDialog();
  20. if (result == DialogResult.OK)
  21. {
  22. txtBrowse.Text = openDB.FileName;
  23. }
  24. }
  25. private void btnReadTable_Click(object sender, EventArgs e)
  26. {
  27. // Open word document for read
  28. using (var doc = WordprocessingDocument.Open(txtBrowse.Text.Trim(), false))
  29. {
  30. // To create a temporary table
  31. DataTable dt = new DataTable();
  32. int rowCount = 0;
  33. // Find the first table in the document.
  34. Table table = doc.MainDocumentPart.Document.Body.Elements<Table>().First();
  35. // To get all rows from table
  36. IEnumerable<TableRow> rows = table.Elements<TableRow>();
  37. // To read data from rows and to add records to the temporary table
  38. foreach (TableRow row in rows)
  39. {
  40. if (rowCount == 0)
  41. {
  42. foreach (TableCell cell in row.Descendants<TableCell>())
  43. {
  44. dt.Columns.Add(cell.InnerText);
  45. }
  46. rowCount += 1;
  47. }
  48. else
  49. {
  50. dt.Rows.Add();
  51. int i = 0;
  52. foreach (TableCell cell in row.Descendants<TableCell>())
  53. {
  54. dt.Rows[dt.Rows.Count - 1][i] = cell.InnerText;
  55. i++;
  56. }
  57. }
  58. }
  59. // To display the result
  60. // Bind datatable(temporary table) to the datagridview
  61. dgvTable.DataSource = dt;
  62. }
  63. }
  64. }
  65. }
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#.