This example demonstrates how to use the Spreadsheet Mail Merge functionality to automatically generate a document based on data retrieved from a specified data source (the Categories table of the Northwind database). The nwind.mdb database file is included in the project. This file ships with the XtraSpreadsheet Suite installation, and found in the C:\Users\Public\Documents\DevExpress Demos 14.1\Components\Data directory by default.
The application includes the following controls.
1. SpreadsheetControl
A ready-to-use mail merge template (the MailMergeTemplate.xlsx file) is automatically loaded into the SpreadsheetControl when invoking the application. This template is bound to the Categories table of the Northwind database via the IWorkbook.MailMergeDataSource and IWorkbook.MailMergeDataMember properties in code.
2. Field List
The Field List panel shows the structure of the mail merge data source at runtime. You can add mail merge fields to template cells by drag-and-drop or by double-clicking the corresponding items in this panel.
3. RibbonControl with the Mail Merge tab
The Mail Merge page contains various command buttons that you can use to modify the mail merge template. For example, you can do the following.
- Enable the Mail Merge Design View. This is a specific mode to display worksheets intended for preparing mail merge templates. It is recommended that you activate this view before creating or modifying a template.
- Select one of the available modes to specify whether data records should be merged into separate workbooks, separate worksheets in a single workbook, or a single worksheet. By default, the Single Sheet mode is used.
- Set the vertical or horizontal orientation for the resulting worksheet.
- Specify the Detail, Header and Footer ranges in the template.
- Sort, group and filter the data to appear in the resulting document.
- Preview the merged document.
In this example, the Result button is added to the Mail Merge page. It calls the IWorkbook.GenerateMailMergeDocuments method, which returns the resulting workbook and saves it to a file. If the mail merge mode is set to Multiple Documents, each workbook returned by the GenerateMailMergeDocuments method is saved to a separate file.