-
用Spire.doc来合并邮件
让我们想象一下这样的场景:你在一家IT公司上班。某天公司的某一产品大幅度升级了。然后你需要通知所有的客户。这真是很长的名单。一个个的通知他们是有点蠢的,因为这要花费太多的时间和人力了。为什么不找个更好的方法来快速高效地完成这项工作呢?我这里给大家一个用组件来解决的方法。组件的链接在这里。这是Spire.doc的另一个小功能,就是用它来合并邮件。
这是一个通知邮件并且所发的内容都是相同的。首先我们先创建一个模板,这个模板是用来创建通知邮件。请看下面的模板。
接下来我们要做的是将方括号内的文本替换成客户信息。下面是实现的代码:
private int lastIndex = 0;
private void button1_Click(object sender, EventArgs e)
{
//create word document
Document document = new Document();
document.LoadFromFile("template.doc");
lastIndex = 0;
//informaton of customers
List<CustomerRecord> customerRecords = new List<CustomerRecord>();
CustomerRecord c1 = new CustomerRecord();
c1.ContactName = "Lucy";
c1.Fax = "786-324-10";
c1.Date = DateTime.Now;
customerRecords.Add(c1);
CustomerRecord c2 = new CustomerRecord();
c2.ContactName = "Lily";
c2.Fax = "779-138-13";
c2.Date = DateTime.Now;
customerRecords.Add(c2);
CustomerRecord c3 = new CustomerRecord();
c3.ContactName = "James";
c3.Fax = "363-287-02";
c3.Date = DateTime.Now;
customerRecords.Add(c3);
//execute mailmerge
document.MailMerge.MergeField += newMergeFieldEventHandler(MailMerge_MergeField);
document.MailMerge.ExecuteGroup(new MailMergeDataTable("Customer", customerRecords));
//save doc file.
document.SaveToFile("result.doc", FileFormat.Doc);
//viewer the result file.
System.Diagnostics.Process.Start("result.doc");
}
void MailMerge_MergeField(object sender, MergeFieldEventArgs args)
{
//next row
if (args.RowIndex > lastIndex)
{
lastIndex = args.RowIndex;
AddPageBreakForMergeField(args.CurrentMergeField);
}
}
void AddPageBreakForMergeField(IMergeField mergeField)
{
//find position of needing to add page break
bool foundGroupStart = false;
Paragraph paramgraph = mergeField.PreviousSibling.Owner as Paragraph;
MergeField merageField = null;
while (!foundGroupStart)
{
paramgraph = paramgraph.PreviousSibling as Paragraph;
for (int i = 0; i < paramgraph.Items.Count; i++)
{
merageField = paramgraph.Items[i] as MergeField;
if ((merageField != null) && (merageField.Prefix == "GroupStart"))
{
foundGroupStart = true;
break;
}
}
}
paramgraph.AppendBreak(BreakType.PageBreak);
}
//class to represent customers
public class CustomerRecord
{
private string m_contactName;
public string ContactName
{
get
{
return m_contactName;
}
set
{
m_contactName = value;
}
}
private string m_fax;
public string Fax
{
get
{
return m_fax;
}
set
{
m_fax = value;
}
}
private DateTime m_date;
public DateTime Date
{
get
{
return m_date;
}
set
{
m_date = value;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
//create word document
Document document = new Document();
document.LoadFromFile("template.doc");
lastIndex = 0;
//informaton of customers
List<CustomerRecord> customerRecords = new List<CustomerRecord>();
CustomerRecord c1 = new CustomerRecord();
c1.ContactName = "Lucy";
c1.Fax = "786-324-10";
c1.Date = DateTime.Now;
customerRecords.Add(c1);
CustomerRecord c2 = new CustomerRecord();
c2.ContactName = "Lily";
c2.Fax = "779-138-13";
c2.Date = DateTime.Now;
customerRecords.Add(c2);
CustomerRecord c3 = new CustomerRecord();
c3.ContactName = "James";
c3.Fax = "363-287-02";
c3.Date = DateTime.Now;
customerRecords.Add(c3);
//execute mailmerge
document.MailMerge.MergeField += newMergeFieldEventHandler(MailMerge_MergeField);
document.MailMerge.ExecuteGroup(new MailMergeDataTable("Customer", customerRecords));
//save doc file.
document.SaveToFile("result.doc", FileFormat.Doc);
//viewer the result file.
System.Diagnostics.Process.Start("result.doc");
}
void MailMerge_MergeField(object sender, MergeFieldEventArgs args)
{
//next row
if (args.RowIndex > lastIndex)
{
lastIndex = args.RowIndex;
AddPageBreakForMergeField(args.CurrentMergeField);
}
}
void AddPageBreakForMergeField(IMergeField mergeField)
{
//find position of needing to add page break
bool foundGroupStart = false;
Paragraph paramgraph = mergeField.PreviousSibling.Owner as Paragraph;
MergeField merageField = null;
while (!foundGroupStart)
{
paramgraph = paramgraph.PreviousSibling as Paragraph;
for (int i = 0; i < paramgraph.Items.Count; i++)
{
merageField = paramgraph.Items[i] as MergeField;
if ((merageField != null) && (merageField.Prefix == "GroupStart"))
{
foundGroupStart = true;
break;
}
}
}
paramgraph.AppendBreak(BreakType.PageBreak);
}
//class to represent customers
public class CustomerRecord
{
private string m_contactName;
public string ContactName
{
get
{
return m_contactName;
}
set
{
m_contactName = value;
}
}
private string m_fax;
public string Fax
{
get
{
return m_fax;
}
set
{
m_fax = value;
}
}
private DateTime m_date;
public DateTime Date
{
get
{
return m_date;
}
set
{
m_date = value;
}
}
}
输出结果截图:
原文链接:https://www.cnblogs.com/Yesi/p/4218254.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数