Skip to content

Commit f0eaee5

Browse files
authored
Update Program.cs
1 parent e51b7a4 commit f0eaee5

File tree

1 file changed

+50
-47
lines changed

1 file changed

+50
-47
lines changed

CS/MailMergeExample/Program.cs

+50-47
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,60 @@ static class Program {
99
[STAThread]
1010
static void Main() {
1111
#region #main
12-
Workbook workbook = new DevExpress.Spreadsheet.Workbook();
13-
workbook.Unit = DevExpress.Office.DocumentUnit.Inch;
12+
using (Workbook workbook = new Workbook())
13+
{
14+
workbook.Unit = DevExpress.Office.DocumentUnit.Inch;
15+
16+
// Create a mail merge template.
17+
Worksheet template = workbook.Worksheets[0];
18+
template.Rows[1].RowHeight = 1.5;
19+
template.Columns[1].ColumnWidth = 1.0;
20+
template.Columns[1].Alignment.Vertical = SpreadsheetVerticalAlignment.Center;
21+
template.Columns[2].ColumnWidth = 2.5;
22+
template.Columns[2].Alignment.WrapText = true;
23+
workbook.BeginUpdate();
24+
template.Cells["C2"].Formula = "FIELDPICTURE(\"Photo\", \"range\", C2, FALSE, 50)";
25+
template.Cells["C3"].Formula = "=FIELD(\"FirstName\")&\" \"&FIELD(\"LastName\")";
26+
template.Cells["B4"].Value = "Position:";
27+
template.Cells["C4"].Formula = "FIELD(\"Title\")";
28+
template.Cells["B5"].Value = "Birth Date:";
29+
template.Cells["C5"].Formula = "FIELD(\"BirthDate\")";
30+
template.Cells["C5"].NumberFormat = "M/d/yyyy";
31+
template.Cells["B6"].Value = "Hire Date:";
32+
template.Cells["C6"].Formula = "FIELD(\"HireDate\")";
33+
template.Cells["C6"].NumberFormat = "dddd MMMM dd, yyyy";
34+
template.Cells["B7"].Value = "Home Phone:";
35+
template.Cells["C7"].Formula = "FIELD(\"HomePhone\")";
36+
template.Cells["B8"].Value = "Address:";
37+
template.Cells["C8"].Formula = "=FIELD(\"Address\")&\" \"&FIELD(\"City\")";
38+
template.Cells["B9"].Value = "About:";
39+
template.Cells["C9"].Formula = "FIELD(\"Notes\")";
40+
workbook.EndUpdate();
41+
// Set a detail range in the template.
42+
CellRange detail = template.Range["C1:C9"];
43+
detail.Name = "DETAILRANGE";
1444

15-
// Create a mail merge template.
16-
Worksheet template = workbook.Worksheets[0];
17-
template.Rows[1].RowHeight = 1.5;
18-
template.Columns[1].ColumnWidth = 1.0;
19-
template.Columns[1].Alignment.Vertical = SpreadsheetVerticalAlignment.Center;
20-
template.Columns[2].ColumnWidth = 2.5;
21-
template.Columns[2].Alignment.WrapText = true;
22-
template.Cells["C2"].Formula = "FIELDPICTURE(\"Photo\", \"range\", C2, FALSE, 50)";
23-
template.Cells["C3"].Formula = "=FIELD(\"FirstName\")&\" \"&FIELD(\"LastName\")";
24-
template.Cells["B4"].Value = "Position:";
25-
template.Cells["C4"].Formula = "FIELD(\"Title\")";
26-
template.Cells["B5"].Value = "Birth Date:";
27-
template.Cells["C5"].Formula = "FIELD(\"BirthDate\")";
28-
template.Cells["C5"].NumberFormat = "M/d/yyyy";
29-
template.Cells["B6"].Value = "Hire Date:";
30-
template.Cells["C6"].Formula = "FIELD(\"HireDate\")";
31-
template.Cells["C6"].NumberFormat = "dddd MMMM dd, yyyy";
32-
template.Cells["B7"].Value = "Home Phone:";
33-
template.Cells["C7"].Formula = "FIELD(\"HomePhone\")";
34-
template.Cells["B8"].Value = "Address:";
35-
template.Cells["C8"].Formula = "=FIELD(\"Address\")&\" \"&FIELD(\"City\")";
36-
template.Cells["B9"].Value = "About:";
37-
template.Cells["C9"].Formula = "FIELD(\"Notes\")";
45+
// Set a header range in the template.
46+
CellRange header = template.Range["B1:B9"];
47+
header.Name = "HEADERRANGE";
3848

39-
// Set a detail range in the template.
40-
CellRange detail = template.Range["C1:C9"];
41-
detail.Name = "DETAILRANGE";
49+
// Switch the mail merge mode to "Multiple Sheets".
50+
workbook.DefinedNames.Add("MAILMERGEMODE", "=\"Worksheets\"");
51+
// Switch the mail merge mode to "Multiple Documents".
52+
//workbook.DefinedNames.GetDefinedName("MAILMERGEMODE").RefersTo = "\"Documents\"";
53+
// Switch the mail merge mode to "Single Sheet".
54+
//workbook.DefinedNames.GetDefinedName("MAILMERGEMODE").RefersTo = "\"OneWorksheet\"";
4255

43-
// Set a header range in the template.
44-
CellRange header = template.Range["B1:B9"];
45-
header.Name = "HEADERRANGE";
56+
// Set vertical document orientation.
57+
workbook.DefinedNames.Add("HORIZONTALMODE", "=TRUE");
4658

47-
// Switch the mail merge mode to "Multiple Sheets".
48-
workbook.DefinedNames.Add("MAILMERGEMODE", "=\"Worksheets\"");
49-
// Switch the mail merge mode to "Multiple Documents".
50-
//workbook.DefinedNames.GetDefinedName("MAILMERGEMODE").RefersTo = "\"Documents\"";
51-
// Switch the mail merge mode to "Single Sheet".
52-
//workbook.DefinedNames.GetDefinedName("MAILMERGEMODE").RefersTo = "\"OneWorksheet\"";
53-
54-
// Set vertical document orientation.
55-
workbook.DefinedNames.Add("HORIZONTALMODE", "=TRUE");
56-
57-
// Perform mail merge.
58-
workbook.MailMergeDataSource = EmployeeInfo.EmployeesInfo.GetData();
59-
var result = workbook.GenerateMailMergeDocuments();
60-
result[0].SaveDocument("result.xlsx");
61-
System.Diagnostics.Process.Start("result.xlsx");
62-
#endregion #main
59+
// Perform mail merge.
60+
workbook.MailMergeDataSource = EmployeeInfo.EmployeesInfo.GetData();
61+
var result = workbook.GenerateMailMergeDocuments();
62+
result[0].SaveDocument("result.xlsx");
63+
System.Diagnostics.Process.Start("result.xlsx");
64+
#endregion #main
65+
}
6366
}
6467
}
6568
}

0 commit comments

Comments
 (0)