How to perform mail merge using Dictionary as data source
Mail merge is a process of merging data from a data source to a Word template document. Syncfusion® Essential® DocIO is a .NET Word library used to generate reports like invoices, payrolls, letters, etc., by performing mail merge faster in a batch process without Microsoft Word or interop dependencies. Using this library, you can execute mail merge using a Dictionary as an IEnumerable collection.
Mail merge using Dictionary as data source
Essential® DocIO supports executing mail merge using an IEnumerable collection through the Execute API. You can pass a Dictionary as an IEnumerable collection by adding it into a List collection of object type.
The following code example shows how to execute mail merge using a Dictionary as an IEnumerable collection.
C#
// Loads input document
WordDocument document = new WordDocument("Template.docx");
// Gets data as Dictionary collection to perform mail merge
Dictionary<string, object> keyValuePairs = GetDetails();
// Creates "IEnumerable" collection using Dictionary
List<object> enumerableCollection = new List<object>();
enumerableCollection.Add(keyValuePairs);
// Performs mail merge
document.MailMerge.Execute(enumerableCollection);
// Saves and closes the document
document.Save("Sample.docx");
document.Close();
VB
' Loads input document
Dim document As WordDocument = New WordDocument("Template.docx")
' Gets data using Dictionary
Dim keyValuePairs As Dictionary(Of String, Object) = GetDetails
' Initialize the list of object
Dim enumerableCollection As List(Of Object) = New List(Of Object)
' Adds the Dictionary to the list
enumerableCollection.Add(keyValuePairs)
' Execute mail merge function with IEnumerable collection
document.MailMerge.Execute(enumerableCollection)
' Saves and closes the document
document.Save("Sample.docx")
document.Close
The following code example shows the GetDetails method which is used to get the data for mail merge.
C#
public Dictionary<string, object> GetDetails()
{
// Creates a Dictionary collection to maintain field names as keys and field values as values
Dictionary<string, object> stringList = new Dictionary<string, object>();
// Adds the keys and values to the Dictionary
stringList.Add("Cycle", "Cycle");
stringList.Add("Vehicle", System.Drawing.Image.FromFile("Mountain-200.jpg"));
stringList.Add("Purchase Date", new DateTime(2019, 8, 1));
stringList.Add("Purchase Price", "3500");
stringList.Add("Purchase Mileage", "50");
stringList.Add("Sold Date", new DateTime(2019, 8, 9));
stringList.Add("Sold Price", "5000");
stringList.Add("Sold Mileage", "50");
stringList.Add("Color", "Blue");
stringList.Add("Product Number", "BK-M68B-38");
stringList.Add("VIN Number", "1234");
return stringList;
}
VB
Public Function GetDetails() As Dictionary(Of String, Object)
' Creates a Dictionary collection to maintain field names as keys and field values as values
Dim stringList As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
' Adds the keys and values to the Dictionary
stringList.Add("Cycle", "Cycle")
stringList.Add("Vehicle", System.Drawing.Image.FromFile("Mountain-200.jpg"))
stringList.Add("Purchase Date", New DateTime(2019, 8, 1))
stringList.Add("Purchase Price", "3500")
stringList.Add("Purchase Mileage", "50")
stringList.Add("Sold Date", New DateTime(2019, 8, 9))
stringList.Add("Sold Price", "5000")
stringList.Add("Sold Mileage", "50")
stringList.Add("Color", "Blue")
stringList.Add("Product Number", "BK-M68B-38")
stringList.Add("VIN Number", "1234")
Return stringList
End Function
A complete working example of how to perform Mail merge using Dictionary as data source in Word document in C# can be downloaded from Mail merge using Dictionary.zip.
Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, PDF and image conversions with code examples.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.