Generate Word from template in C#

Automating document generation is crucial for businesses to create dynamic reports, invoices, and letters. With 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 demonstrates how to utilize Mail Merge in C# for streamlined and automated document workflows, specifically focusing on C# .NET dynamic Word document creation.


Topics Covered:


Overview of Mail Merge for .NET

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

  • Populate templates with data dynamically, supporting the .NET framework word document generation.
  • Perform bulk generation of documents efficiently, leveraging the .NET mail merge solution for large datasets.
  • Maintain consistent formatting across outputs, effectively enhancing the C# word document template best practices.

Key Features:

  1. Multi-Source Support: Use 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, making use of the .NET word mail merge automation capabilities.

Setup

Install Aspose.Words using NuGet:

PM> Install-Package Aspose.Words

Generate Word Document from Template Using C# Objects

First, let’s see how to populate a Word template using C# objects. To illustrate this, we will create a Word document (DOC/DOCX) with the following placeholders as the content of the document:

<<[sender.Name]>> says: "<<[sender.Message]>>."

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

Now, we can 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:

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

The following code sample shows 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 will utilize 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:

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

Here’s the code sample demonstrating how to generate a Word document from 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. The code sample below shows 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 discovered how to leverage various data sources such as objects, XML, JSON, and CSV for dynamic document generation .NET. Delve deeper into the capabilities of the .NET Word template processing by exploring the documentation, and feel free to reach out through our forum.

See Also