Generate Word from template in C#

Automating document generation is essential for businesses aiming to create dynamic reports, invoices, and letters. With the Aspose.Words Mail Merge for .NET, you can efficiently populate Word templates with data from various sources, including objects, CSV, JSON, and XML. This guide will walk you through utilizing Mail Merge in C# for streamlined and automated document workflows, focusing on dynamic Word document creation with C# .NET.


Table of Contents


Overview of Mail Merge for .NET

The Aspose.Words Mail Merge for .NET plugin allows developers to generate personalized documents from predefined templates containing merge fields. This integration is designed for seamless use within .NET environments, offering capabilities to:

  • Dynamically populate templates with data, supporting .NET framework Word document generation.
  • Efficiently perform bulk document generation, leveraging the .NET mail merge solution for Word documents.
  • Ensure consistent formatting across outputs, enhancing C# Word document template best practices.

Key Features:

  1. Multi-Source Support: Utilize data from objects, databases, XML, JSON, and CSV to create customized Word documents with C#.
  2. High Performance: Efficiently handle large datasets and bulk operations for dynamic Word document generation with .NET Core.
  3. Customizable Templates: Create templates with placeholders for data insertion, utilizing the .NET Word mail merge automation capabilities.

Setup

To get started, install Aspose.Words using NuGet:

PM> Install-Package Aspose.Words

Generate Word Document from Template Using C# Objects

Let’s begin by populating a Word template using C# objects. We will create a Word document (DOC/DOCX) with the following placeholders:

«[sender.Name]» says: “«[sender.Message]».”

In this example, sender is an instance of a class that we will use to populate the template using the C# generate Word document from database approach.

Next, we will use the reporting engine of Aspose.Words to generate the Word document from the template and the instance of the Sender class by following these steps:

  1. Create an instance of the Document class and initialize it with the Word template’s path.
  2. Create and initialize an object of the Sender class.
  3. Instantiate the ReportingEngine class.
  4. Populate the template using ReportingEngine.BuildReport(), which takes the Document’s instance, data source, and the name of the data source as parameters.
  5. Save the generated Word document using Document.Save().

Here’s the code sample demonstrating how to generate a Word document from a template in C#.

Output

Generate Word Document from an XML Data Source in C#

To generate a Word document from an XML data source, we’ll use a more complex Word template with the following placeholders:

«foreach [in persons]»Name: «[Name]», Age: «[Age]», Date of Birth: «[Birth]:“dd.MM.yyyy”» «/foreach» Average age: «[persons.Average(p => p.Age)]»

The XML data source used in this example is shown below.

To create a Word document from an XML data source, follow these steps:

  1. Create an instance of the Document class and initialize it with the Word template’s path.
  2. Create an instance of the XmlDataSource class and initialize it with the XML file’s path.
  3. Instantiate the ReportingEngine class.
  4. Use ReportingEngine.BuildReport() method to populate the Word template.
  5. Save the generated Word document using Document.Save() method.

Here’s the code sample demonstrating how to generate a Word document from an XML data source in C#.

Output

Generate Word Document from a JSON Data Source in C#

Next, let’s explore how to generate a Word document using a JSON data source. In this example, we will create a list of clients segmented by their managers. The Word template will look like this:

«foreach [in managers]»Manager: «[Name]» Contracts: «foreach [in Contract]»- «[Client.Name]» ($«[Price]») «/foreach» «/foreach»

The corresponding JSON data source we will use to populate the template is below:

To generate the Word document from JSON, we will use the JsonDataSource class to load the data source, and the remaining steps will follow the previous structure. Here’s the code sample demonstrating how to generate a Word document from the JSON template in C#.

Output

Generate Word Document from CSV Data Source in C#

To generate the Word document from a CSV source, we will utilize the following Word template:

«foreach [in persons]»Name: «[Column1]», Age: «[Column2]», Date of Birth: «[Column3]:“dd.MM.yyyy”» «/foreach» Average age: «[persons.Average(p => p.Column2)]»

The template will be populated using the following CSV data:

Now, let’s review the C# code. The steps remain the same except for one key difference: we will use the CsvDataSource class to load the CSV data. The following code sample illustrates how to generate the Word document from a CSV data source.

Output

Try Aspose.Words for .NET for Free

You can try Aspose.Words for .NET using a free temporary license.

Conclusion

In this article, you have learned how to generate Word documents from templates using C#. You have explored how to leverage various data sources such as objects, XML, JSON, and CSV for dynamic document generation in .NET. For further insights into the capabilities of .NET Word template processing, dive into the documentation, and feel free to reach out through our forum.