The Mail Merge functionality provided by the DevExpress ASP.NET MVC Spreadsheet enables you to automatically generate a set of documents based on a common template and fill each document with unique data values retrieved from a data source. This feature can be useful for a variety of business requirements, such as personalizing letters, and composing catalogs and reports.
To perform a mail merge, you need a template and a data source.
A template is a spreadsheet document that contains static information (which is the same for all resulting documents) and mail merge fields. A mail merge field is a placeholder in which a value from the specified data source field will appear in a merged document. To insert fields into template cells, use the following special functions in cell formulas: FIELD and FIELDPICTURE.
A data source (Model) contains data that will be merged into fields in a template to create merged documents.
Within a template, you can choose a mail merge mode specifying how the result of the mail merge should be generated. The mail merge mode is specified programmatically at the template workbook level, by setting a specific defined name ("MAILMERGEMODE") to a particular string constant ("Worksheets" or "OneWorksheet").
- "OneWorksheet" (Single Sheet) - Merged ranges for all data source records are inserted one after another (vertically or horizontally, depending on the document orientation) into a single worksheet. This mode is used by default.
- "Worksheets" (Multiple Sheets) - The merged range for each data source record is inserted into a separate worksheet in a single workbook.
Note that by default, the "MAILMERGEMODE" defined name does not exist in a template workbook and the "Single Sheet" mode is used.
To initialize the mail merge process, use the PerformMailMerge method of the Spreadsheet extension. This method accepts a set of parameters (which specify the path to a template, a data source object, the mail merge mode and etc.) and generates resulting documents containing the merged information (if the "Single Sheet" or "Multiple Sheets" mail merge mode is used, a single workbook is processed).
In this demo, we illustrate the merging of images along with personal information for contacts from the Northwind database's Employees table.