When performing a
mail merge with the
SpreadsheetControl, you can sort data to be merged and split it into groups based on identical values in the specified data field.
This example contains the
mail merge template to generate invoices using data from the
Invoices view 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 template is bound to the data source via the
IWorkbook.MailMergeDataSource and
IWorkbook.MailMergeDataMember properties in code.
Run the application and review the prepared template (the
MailMergeTemplate_GroupData.xlsx file) that is automatically loaded into the SpreadsheetControl.
On the
Mail Merge tab, enable the
Mail Merge Design View to highlight the template structure. The template consists of the following ranges.
-
HeaderThe
Header range contains the current date to be displayed above each invoice.
-
Detail
The
Detail range contains fields for merging order data (general order information, products ordered and order totals). In this range, the
OrderID data field is set as a criterion for sorting data. To make sure of this, check that the
OrderID field is displayed in the
Sort Fields dialog (to invoke this dialog, select any cell within the
Detail range and click the
Sort Fields button on the
Mail Merge tab), and the SORTFIELD0
defined name for the
OrderID field is added to the worksheet's collection of defined names (you can see this defined name in the
Name Manager dialog when you click the corresponding button on the
Formulas tab).
-
GroupHeader
All general information for a group of records is located within the
GroupHeader0 range. In this example, each group is an order. Thus, the group header contains information about the order, customer, salesperson, dates, delivery, etc. The existing
OrderID(Ascending) sort field is used as a criterion for creating groups in the merged document (this sort field was selected when the
GroupHeader range was created). In the
Name Manager dialog, you can see the GROUPHEADER0 defined name that corresponds to this group header range.
-
GroupFooterThe
GroupFooter0 range displays totals at the end of a group of records (in other words, at the end of an order). It is also created based on the
OrderID(Ascending) sort field and saved in the template document as a defined name (GROUPFOOTER0).
In this template, the mail merge mode is set to
Multiple Sheets. Thus, each order will be inserted in a separate worksheet. To review the result, click the
Mail Merge Preview button on the
Mail Merge tab.