Insert DataTable into Word Document in C#

As a .NET developer, you have to deal with DataTable objects very often. Especially, when you need to import or export data from databases or XML/JSON files. While generating documents programmatically, you might need to insert data from the DataTable objects directly into the table in Word documents. For such cases, let me give you the perfect and easiest solution for inserting data from DataTable into a Word document in C#.

C# Library to Insert DataTable in Word DOC - Free Download

To import data from DataTable objects into Word documents, we will use Aspose.Words for .NET. It is a feature-rich library that lets you create and process MS Word documents. You can either download its DLL or install it from NuGet.

Download DLL

Install via NuGet

PM> Install-Package Aspose.Words

C# Insert DataTable into Word Document - Steps

The following are the steps that we will follow to retrieve data from a DataTable and insert that into the Word document.

  • Load data from a database or a data file into the DataTable object.
  • Create a new Word document and add a new table to it.
  • Loop through the rows in the DataTable and insert each row in Word’s table.

And that’s it.

Let’s now have a look at how to transform these steps into the C# code and import a DataTable into a Word document.

Insert Data from DataTable into Word DOC in C#

First, we will write a method that accepts the DocumentBuilder to build the document, DataTable having data, and an additional parameter to enable/disable importing column headings. Moreover, it returns the Table object.

The following is the workflow of this method.

  • Create a new table and get its reference into a Table object.
  • Insert columns into the table according to the columns in DataTable.
  • Set the font weight if the first row is for headings.
  • Loop through each DataRow in DataTable.Rows collection.
  • Insert a new cell for each value and set the data type accordingly.
  • Return the Table object.

The following is the implementation of the method for importing data from a DataTable into a table in Word document in C#.

Now, it’s time to call this method and insert a DataTable into a Word document.

Import DataTable into Word Document in C#

The following are the steps to import data from the DataTable into a Word document in C#.

  • Create a new Word document (or load an existing one) using Document class.
  • Read data from database/file into the DataTable object.
  • Call the method that we have written in the previous section and get the returned Table.
  • Set formatting of the table.
  • Save Word file using Document.Save() method.

The following code sample shows how to insert a DataTable into a Word document in C#.

C# DataTable to Word Importer Library - Get a Free License

You can get a free temporary license to use Aspose.Words for .NET without evaluation limitations.

Conclusion

In this article, you have learned how to insert data from a DataTable into a Word document in C#. You can easily integrate the code sample into your applications and import data directly from database/XML/JSON into Word documents using DataTable.

.NET Word Library - Read More

You can visit the documentation of Aspose.Words for .NET to explore more about the library. In case you would have any questions, feel free to let us know via our forum.

See Also