Mail merge is often used to print reports in bulk, such as financial statement, payroll or transcript. And the merged documents can be sent by E-mail.
In this article, I will show one way to generate mail merged reports via a .NET Word component, Spire.Doc.
This report includes multiple invoices, and each invoice starts in a new page. Invoice logo and supplier information will present in the header of every page.
Order, shipment, customer, order details and total price make up a completed invoice.
The following pictures show the appearance of invoice:
Content details in each invoice are shown as following:
Order Data Overview
All data in this example is from NorthWind database, which is a sample database provided by Microsoft Access 2003.
We will export data from table Orders, Shippers, Customers, Employees, [Order Details] and Products to generate our report. The following picture shows the relationship between the 6 tables.
We need to finish the following 3 steps to generate our report.
- Create a mail merge template.
- Load data from database.
- Merge data into template and save.
Every step includes several sub-steps and in #2 and #3 we need to write some code.
Create mail merge template
How to Create the Template
A template is a reusable document. It renders the pattern of our report. We could modify it to change our report without any modification of code.
Note: in this section, all tables mean DataTable instance, not physical table in database.
1. We can create the template in MS Word or by other program. Please see the following picture. It is the template we need to create. Data will be filled in the red party.
2. Insert mail-merge-field as placeholder into the red-block. There are three types of mail-merge-field which will be used in this example:
a). GeneralField is a general Word mail-merge-field. It is real data field and our data will be filled in it during merge process. We need to insert a GeneralField to every red-block and name these fields with the corresponding data name. After inserting GeneralFields, our template will looks like:
b). TableField is assistant mail-merge-field and used as a container of multiple related GeneralFields and other TableFields. So it is not data placeholder and no data will be filled in. It is composed of two special mail-merge-fields: TableStart:TableName and TableEnd:TableName. During merge process, the data of related GeneralFields contained by one same TableField will be from one same data table. For example, fields in Customer information block will be filled with data from data table Customer, so we need to put them in TableField Customer. Insert a mail-merge-field with field name TableStart:Customer immediately before the first CompanyName field and insert another mail-merge-field with field name TableEnd:Customer immediately after the field Country. And then our fields in Customer information block looks like:
During the merge process, data in column CompanyName of table Customer will be filled in the field CompanyName, Customer.Address to field Address, Customer.City to field City and so on.
Data of fields in column Salesperson in Order information table is from table Employee
Data of fields in column Ship Via in Order information table is from table Shipper
Data of fields in Order details table is from table Detail, except field ProductName. Data of field ProductName is from table Product. Dataof field InvoiceSubtotal and InvoiceTotal in Invoice total information is from table Total (virtual table)
c). GroupField is assistant mail-merge-field too. It can contain multiple related GeneralFields and TableFields. It is composed of two special mail-merge-fields: GroupStart:GroupName and GroupEnd:GroupName. During merge process, all Word document elements included in a GroupField will be copied. One row in data table has one copy and data in the row will be filled into the fields in the copy. If the row has sub data table, the data in sub data table will be filled into the fields included in the corresponding TableField. If the sub data table has multiple data rows, the corresponding TableField will be copied and filled too. We need to insert a mail-merge-field named GroupStart:Order in the top of the template body and insert a mail-merge-field named GroupEnd:Order in the bottom of the template body. After this, our template looks like:
You could find the complete template named InvoiceTemplate.doc in the attached source package.
Load Data from Database
Spire.Doc provides merge data from DataSet. So we will use DataAdapter to fill data table from NorthWind database to a DataSet and merge it into our template. Difference from DataRelation of DataSet, Spire.Doc has owned table relation functionality. So we don’t need to create DataRelation instance for the DataSet object. The code below just shows load Order data. Please see the attached source package for other code.
Merge data into template and save
In this section, we need to write some code to call Spire.Doc to merge our data table and template.
1. Create Spire.Doc.Document object and load template.
2. Establish relationship between data tables.
In order to start each invoice in a new page, we insert a page-break-symbol immediately before the first paragraph when a new order row will be merged. To do this, we need to handle the event MergeField which is fired before a field merged.
Code of method InsertPageBreak
This article focuses on how to generate report by using mail merge with C#. In this method, we need to prepare data information and template firstly. Then, merge data in customized template. It helps us to generate report in bulk quickly and easily.
For the component used in this example, please download from here.