News Category

How to Use Mail Merge in Silverlight

2011-07-26 07:05:50 Written by  support iceblue
Rate this item
(0 votes)

Introduction

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 Spire.Doc.

Steps

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

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.

First, We can create the template in MS Word or by other program. It is the template we need to create. Data will be filled in the red party.

Mail Merge Fax

Second, Insert mail-merge-field as placeholder into the red-block. There are three types of mail-merge-field:

  • GeneralField is a general Word mail-merge-field. It is real data field and our data will be filled in it during merge process.
  • 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.
  • 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:OrderM in the bottom of the template body.

In this Silverlight application we only use GeneralField. As the image shows below. Insert mail-merge-field as placeholder into the red-block.

Mail Merge Fax

Load Data

Spire.Doc provides merge data from various kind of datasources. This program merge data from a string array which is filled by user.

Mail Merge Fax

using Spire.Doc;
using System;
using System.IO;
namespace MailMerge
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] fieldNames
                = new String[] { "Contact Name", "Fax", "From", "Date", "Subject", "Content" };
            DateTime faxDate
                = this.datePickerFaxDate.SelectedDate.HasValue ?
                    this.datePickerFaxDate.SelectedDate.Value : DateTime.Now;
            String[] fieldValues
                = new String[] 
                {
                    this.textBoxTo.Text,
                    this.textBoxFax.Text,
                    this.textBoxFrom.Text,
                    faxDate.ToShortDateString(),
                    this.textBoxSubject.Text,
                    this.textBoxContent.Text
                };
            this.documentTemplate.MailMerge.Execute(fieldNames, fieldValues);

            bool? result = this.saveFileDialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = this.saveFileDialog.OpenFile())
                {
                    //this.documentTemplate.SaveToStream(stream, FileFormat.Doc);
                    this.documentTemplate.SaveToFile(stream, FileFormat.Doc);
                }


            }
        }
    }
}

Merge data into template and save

In this section, we need to write some code to call Spire.Doc to merge our data and template.

this.documentTemplate.MailMerge.Execute(fieldNames, fieldValues);

            bool? result = this.saveFileDialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = this.saveFileDialog.OpenFile())
                {
                    //this.documentTemplate.SaveToStream(stream, FileFormat.Doc);
                    this.documentTemplate.SaveToFile(stream, FileFormat.Doc);
                }
            }

The Result

Mail Merge Fax

Additional Info

  • tutorial_title: Mail Merge for Silverlight
Last modified on Friday, 03 September 2021 03:27