use spire.doc to merge messages
< Span style= "font-size:10.0pt;font-family: Song body; color: #222222" > Let's imagine this scenario: you're in a it company to work. One day the company's products have been greatly upgraded. Then you need to notify all the customers. It's a really long list. It's a bit silly to notice them because it takes a lot of time and effort. Why not find a better way to get the job done quickly and efficiently? I'm here to give you a way to work with components. The link to the component is here. This is Spire.doc
This is a notification message and the content is the same. First, let's create a template that is used to create notification messages. Take a look at the template below.
What we want to do next is to replace the text in square brackets with the customer information. Here's the code for the implementation:
PrivateintLastIndex =0;
PrivatevoidButton1_Click (Objectsender, EventArgs e)
{
//Create Word Document
Document document =NewDocument ();
Document. LoadFromFile ("Template.doc");
LastIndex =0;
//Informaton of Customers
List<customerrecord> customerrecords =NewList<customerrecord> ();
CustomerRecord C1 =NewCustomerRecord ();
C1. ContactName ="Lucy";
C1. 传真 ="786-324-10";
C1. Date = DateTime.Now;
Customerrecords.add (C1);
CustomerRecord C2 =NewCustomerRecord ();
C2. ContactName ="Lily";
C2. 传真 ="779-138-13";
C2. Date = DateTime.Now;
Customerrecords.add (C2);
CustomerRecord C3 =NewCustomerRecord ();
C3. ContactName ="James";
C3. 传真 ="363-287-02";
C3. Date = DateTime.Now;
Customerrecords.add (C3);
//Execute MailMerge
Document. Mailmerge.mergefield + = Newmergefieldeventhandler (Mailmerge_mergefield);
Document. Mailmerge.executegroup (NewMailmergedatatable ("Customer", customerrecords));
//save doc file.
Document. SaveToFile ("Result.doc", Fileformat.doc);
//Viewer The result file.
System.Diagnostics.Process.Start ("Result.doc");
}
voidMailmerge_mergefield (Objectsender, Mergefieldeventargs args)
{
//Next Row
if(args. RowIndex > LastIndex)
{
LastIndex = args. RowIndex;
Addpagebreakformergefield (args. Currentmergefield);
}
}
voidAddpagebreakformergefield (Imergefield MergeField)
{
//find position of needing to add page break
BOOLFoundgroupstart =false;
Paragraph paramgraph = MergeField.PreviousSibling.Owner asParagraph;
MergeField Meragefield =NULL;
while(!foundgroupstart)
{
Paramgraph = Paramgraph. PreviousSibling asParagraph;
for(inti =0; I < Paramgraph. Items.Count; i++)
{
Meragefield = Paramgraph. Items[i] asMergeField;
if((Meragefield! =NULL) && (Meragefield.prefix = ="GroupStart"))
{
Foundgroupstart =true;
Break;
}
}
}
Paramgraph. Appendbreak (Breaktype.pagebreak);
}
//class to represent customers
PublicclassCustomerRecord
{
PrivatestringM_contactname;
PublicstringContactName
{
Get
{
returnM_contactname;
}
Set
{
M_contactname = value;
}
}
PrivatestringM_fax;
Public stringFax
{
Get
{
returnM_fax;
}
Set
{
M_fax = value;
}
}
PrivateDateTime m_date;
PublicDateTime Date
{
Get
{
returnM_date;
}
Set
{
M_date = value;
}
}
}
Output Result:
Use Spire.doc to merge messages