How to mail merge Word document in ASP.NET MVC?
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 invoice, payroll, letter, etc., by performing mail merge faster in a batch process without Microsoft Word or interop dependencies. Using this library, you can mail merge Word document in ASP.NET MVC.
Steps to Mail merge Word document programmatically:
- Create a new ASP.NET MVC application project.
- Install the Syncfusion.DocIO.AspNet.Mvc5 NuGet package as a reference to your project from NuGet.org.
- Include the following namespace in that HomeController.cs file.
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS;
VB
Imports Syncfusion.DocIO Imports Syncfusion.DocIO.DLS
- A default action method named Index will be present in HomeController.cs. Right click on this action method and select Go To View where you will be directed to its associated view page Index.cshtml.
- Add a new button in the Index.cshtml as shown below.
CSHTML
@{Html.BeginForm("CreateDocument", "Home", FormMethod.Get); { <div> <input type="submit" value="Create Document" style="width:150px;height:27px" /> </div> } Html.EndForm(); }
- Add a new action method CreateDocument in HomeController.cs and include the below code to mail merge Word document and download it.
C#
//Opens the Word template document using (WordDocument document = new WordDocument(ResolveApplicationDataPath("/") + "/Letter Formatting.docx")) { string[] fieldNames = { "ContactName", "CompanyName", "Address", "City", "Country", "Phone"}; string[] fieldValues = { "Nancy Davolio", "Syncfusion", "507 - 20th Ave. E.Apt. 2A", "Seattle, WA", "USA", "(206) 555-9857-x5467" }; //Performs the mail merge document.MailMerge.Execute(fieldNames, fieldValues); //Saves the Word document to disk in DOCX format document.Save("Result.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment); } //Get the path for Word template document protected string ResolveApplicationDataPath(string fileName) { //Data folder path is resolved from requested page physical path. string dataPath = new System.IO.DirectoryInfo(Request.PhysicalPath + "..\\..\\Data\\").FullName; return string.Format("{0}\\{1}", dataPath, fileName); }
VB
'Opens the Word template document Using document As WordDocument = New WordDocument(ResolveApplicationDataPath("/") + "/Letter Formatting.docx") Dim fieldNames As String() = {"ContactName", "CompanyName", "Address", "City", "Country", "Phone"} Dim fieldValues As String() = {"Nancy Davolio", "Syncfusion", "507 - 20th Ave. E.Apt. 2A", "Seattle, WA", "USA", "(206) 555-9857-x5467" } 'Performs the mail merge document.MailMerge.Execute(fieldNames, fieldValues) 'Saves the Word document to disk in DOCX format document.Save("Result.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment) End Using 'Get the path for Word template document Protected Function ResolveApplicationDataPath(fileName As String) As String Dim dataPath As String = New System.IO.DirectoryInfo(Request.PhysicalPath + "..\Data\").FullName Return String.Format("{0}\{1}", dataPath, fileName) End Function
A complete working example of mail merge Word document in ASP.NET MVC can be downloaded from mail merge Word document.zip
By executing the program, you will get the Word document as follows.
See Also:
Mail merge Word document in ASP.NET
Mail merge Word document in ASP.NET Core
Mail merge Word document in WPF
Mail merge Word document in Windows Forms
Mail merge Word document in UWP
Mail merge Word document in Xamarin
Mail merge Word document in Xamarin.Android
Mail merge Word document in Xamarin.iOS
Mail merge Word document in C#
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to mail merge Word document in ASP.NET MVC.
You can refer to our ASP.NET MVC Word feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC Word example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!