{{ figure align=center src=“images/Generate-Word-from-Template-C.png” alt=“Generate Word from template in C#” }}
Enterprises often generate Word reports automatically. Some reports are built from scratch, while others use predefined templates with placeholders. This article explains how to generate Word documents from templates dynamically in C# and how to fill them using various data sources.
The following scenarios and code samples are covered:
- C# Word Automation API
- Generate Word document from a template using C# object’s values
- Generate Word document using an XML data source
- Create Word document using a JSON data source
- Generate Word document using a CSV data source
C# Word Automation API
We’ll use Aspose.Words for .NET – a Word automation API that lets you create Word documents from scratch or by filling predefined templates. You can either download the API binaries or install it via NuGet.
Using NuGet Package Manager
{{ figure align=center src=“images/Aspose.Words-NuGet.png” alt="" }}
Using the Package Manager Console
PM> Install-Package Aspose.Words
Generate Word Document from Template using C# Objects
First, see how to populate a Word template with C# objects. Create a DOC/DOCX template containing these placeholders:
<<[sender.Name]>> says: "<<[sender.Message]>>."
The sender object is defined by the following class:
{{ gist aspose-com-gists 4f0e741d863333a8ea3874eadaf34372 “sender.cs” }}
Use Aspose.Words’ reporting engine to generate the document:
- Create a Document instance and load the template.
- Instantiate the Sender object.
- Create a ReportingEngine instance.
- Call ReportingEngine.BuildReport() with the document, data source, and data source name.
- Save the result with Document.Save().
The code sample below demonstrates this process:
{{ gist aspose-com-gists 4f0e741d863333a8ea3874eadaf34372 “generate-word-from-template-object.cs” }}
Output
{{ figure align=center src=“images/Create-word-document-in-Java.png” alt="" }}
Generate Word Document from an XML Data Source in C#
For XML data, use a more complex template with these 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:
{{ gist aspose-com-gists 4f0e741d863333a8ea3874eadaf34372 “datasource.xml” }}
Steps to generate the document:
- Load the template with a Document instance.
- Create an XmlDataSource pointing to the XML file.
- Instantiate ReportingEngine.
- Call ReportingEngine.BuildReport() as before.
- Save the output with Document.Save().
Code example:
{{ gist aspose-com-gists 4f0e741d863333a8ea3874eadaf34372 “generate-word-from-template-XML.cs” }}
Output
{{ figure align=center src=“images/Create-word-document-in-Java-using-XML.png” alt="" }}
Generate Word Document from a JSON Data Source in C#
Now generate a document using JSON. The template lists clients grouped by managers:
<<foreach [in managers]>>Manager: <<[Name]>>
Contracts:
<<foreach [in Contract]>>- <<[Client.Name]>> ($<<[Price]>>)
<</foreach>>
<</foreach>>
JSON data source:
{{ gist aspose-com-gists 4f0e741d863333a8ea3874eadaf34372 “datasource.json” }}
Load the JSON with JsonDataSource and follow the same steps as before. Code sample:
{{ gist aspose-com-gists 4f0e741d863333a8ea3874eadaf34372 “generate-word-from-template-JSON.cs” }}
Output
{{ figure align=center src=“images/Create-word-document-in-Java-using-JSON.png” alt="" }}
Generate Word Document from CSV Data Source in C#
For CSV data, use this template:
<<foreach [in persons]>>Name: <<[Column1]>>, Age: <<[Column2]>>, Date of Birth: <<[Column3]:"dd.MM.yyyy">>
<</foreach>>
Average age: <<[persons.Average(p => p.Column2)]>>
CSV data:
{{ gist aspose-com-gists 4f0e741d863333a8ea3874eadaf34372 “datasource.csv” }}
The process is identical, except you load the data with CsvDataSource. Code example:
{{ gist aspose-com-gists 4f0e741d863333a8ea3874eadaf34372 “generate-word-from-template-CSV.cs” }}
Output
{{ figure align=center src=“images/Create-word-document-in-Java-using-CSV.png” alt="" }}
Try Aspose.Words for .NET for Free
You can try Aspose.Words for .NET using a free temporary license.
Conclusion
In this article, you learned how to generate Word documents from templates in C#. You saw how to use objects, XML, JSON, and CSV data sources with Aspose.Words. Explore more about the C# Word automation API in the documentation and join the discussion on our forum.