Mail Merging

The Mail Merge functionality provided by the Bootstrap 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.

  1. 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.
  2. A data source contains data that will be merged into fields in a template to create merged documents.

To bind a template to a data source, use the IWorkbook.MailMergeDataSource and IWorkbook.MailMergeDataMember properties of the template workbook.

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) - Used in this demo. Merged ranges for all data source records are inserted one after another (vertically or horizontally, depending on the document orientation) into a single worksheet.
  • "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 finalize the mail merge process, use the IWorkbook.GenerateMailMergeDocuments method of the template workbook. This method returns a collection of resulting workbooks containing the merged documents (if the "Single Sheet" or "Multiple Sheets" mail merge mode is used, the collection contains a single workbook).

In this demo, we illustrate the merging of images along with personal information for contacts from the Northwind database’s Employees table.

A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
AA
AB
AC
AD
AE
AF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Steven Buchanan
Laura Callahan
Nancy Davolio
Anne Dodsworth
Andrew Fuller
Robert King
Janet Leverling
Margaret Peacock
Michael Suyama
Position:
Sales Manager
Inside Sales Coordinator
Sales Representative
Sales Representative
Vice President, Sales
Sales Representative
Sales Representative
Sales Representative
Sales Representative
Birth Date:
3/4/1955
1/9/1985
12/8/1978
1/27/1980
2/19/1965
5/29/1960
8/30/1985
9/19/1973
7/2/1981
Hire Date:
Sunday, October 17, 1993
Friday, March 5, 2004
Sunday, May 1, 2005
Monday, November 15, 2004
Friday, August 14, 1992
Sunday, January 2, 1994
Monday, April 1, 2002
Monday, May 3, 1993
Sunday, October 17, 1999
Homephone:
(71) 555-4848
(206) 555-1189
(206) 555-9857
(71) 555-4444
(206) 555-9482
(71) 555-5598
(206) 555-3412
(206) 555-8122
(71) 555-7773
Address:
14 Garrett Hill London SW1 8JR UK
4726 - 11th Ave. N.E. Seattle 98105 USA
507 - 20th Ave. E. Apt. 2A Seattle 98122 USA
7 Houndstooth Rd. London WG2 7LT UK
908 W. Capital Way Tacoma 98401 USA
Edgeham Hollow Winchester Way London RG1 9SP UK
722 Moss Bay Blvd. Kirkland 98033 USA
4110 Old Redmond Rd. Redmond 98052 USA
Coventry House Miner Rd. London EC2 7JR UK
About:
Steven Buchanan graduated from St. Andrews University, Scotland, with a BSC degree in 1976. Upon joining the company as a sales representative in 1992, he spent 6 months in an orientation program at the Seattle office and then returned to his permanent post in London. He was promoted to sales manager in March 1993. Mr. Buchanan completed the courses: "Successful Telemarketing" and "International Sales Management." He is fluent in French.
Laura received a BA degree in psychology from the University of Washington. She also completed a course in business French. She reads and writes French.
Nancy received a BA degree in psychology from Colorado State University in 2000. She also completed "The Art of the Cold Call" course. She is a member of Toastmasters International.
Anne has a BA degree in English from St. Lawrence College. She is fluent in French and German.
Andrew received his BTS commercial in 1987 and a Ph.D. in international marketing at the University of Dallas in 1994. He speaks French and Italian fluently, and reads German. He joined the company as a sales representative. After that, he was promoted to sales manager in January 2005 and vice president of sales in March 2006. Andrew is a member of the Sales Management Round table, Seattle Chamber of Commerce, and Pacific Rim Importers Association.
Robert King served in the Peace Corps and traveled extensively before completing his degree in English at the University of Michigan in 1992, the year he joined the company. After completing the "Selling in Europe" course, he was transferred to the London office in March 1993.
Janet received a BS degree in chemistry at Boston College in 2006. She also completed a certificate program in food retailing management. Janet was hired as a sales associate in 2013 and promoted to sales representative in February 2014.
Margaret received a BA degree in English literature at Concordia College (1994) and an MA at the American Institute of Culinary Arts (2002). She was temporarily assigned to the London office from July until November 2008.
Michael is a graduate of Sussex University (MA, economics, 2001) and the University of California at Los Angeles (MBA, marketing, 2004). He also took the courses "Multi-Cultural Selling" and "Time Management for the Sales Professional." He is fluent in Japanese and can read and write French, Portuguese, and Spanish.
<dx:BootstrapSpreadsheet ID="SpreadsheetMailMerging" runat="server" ActiveTabIndex="0" ShowConfirmOnLosingChanges="false"
    OnLoad="SpreadsheetMailMerging_Load">
</dx:BootstrapSpreadsheet>
protected void SpreadsheetMailMerging_Load(object sender, EventArgs e) {
    if(!Page.IsPostBack) {
        var resultingWorkbook = GenerateMailMergeDocument();
        if(resultingWorkbook != null)
            OpenResultingDocument(resultingWorkbook);
    }
}
DevExpress.Spreadsheet.IWorkbook GenerateMailMergeDocument() {
    var workbook = SpreadsheetMailMerging.Document;
    LoadMailMergeTemplate(workbook);
    workbook.MailMergeDataSource = GetMailMergeDataSource();
    return workbook.GenerateMailMergeDocuments()[0];
}
void LoadMailMergeTemplate(DevExpress.Spreadsheet.IWorkbook workbook) {
    workbook.LoadDocument(Path.Combine(DirectoryManagmentUtils.CurrentDataDirectory, @"Spreadsheet\\EmployeesHorizontalTemplate.xlsx"));
}
List<Employee> GetMailMergeDataSource() {
    var context = new NorthwindContextSL();
    return context.Employees.ToList();
}
void OpenResultingDocument(DevExpress.Spreadsheet.IWorkbook resultingWorkbook) {
    using(MemoryStream result = new MemoryStream()) {
        for(int i = 0; i < resultingWorkbook.Worksheets.Count; i++) {
            var worksheet = resultingWorkbook.Worksheets[i];
            worksheet.Shapes[0].Left = (float)(worksheet.Columns[0].Width + (worksheet.Columns[1].Width - worksheet.Shapes[0].Width) / 2);
        }
        resultingWorkbook.SaveDocument(result, DevExpress.Spreadsheet.DocumentFormat.OpenXml);
        result.Seek(0, SeekOrigin.Begin);
        SpreadsheetMailMerging.Open(Guid.NewGuid().ToString(), DevExpress.Spreadsheet.DocumentFormat.OpenXml, () => { return result; });
    }
}
Screen Size
Color Themes
Demo QR Code
Calibri