用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 = ;
 
     //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 = ; 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;
     }
  }
}

输出结果截图:

最新文章

  1. 应用程序框架实战三十八:项目示例VS解决方案的创建(一)
  2. Java class file format specfication
  3. diff详解,读懂diff结果
  4. 【算法和数据结构】_11_小算法_itoa、ftoa及字符串倒置
  5. py零散知识点
  6. 使用Zabbix监控RabbitMQ
  7. 【MVC】 文件及URL 的整理
  8. Play Modules Morphia 1.2.9a 之 Aggregation and Group aggregation
  9. Nginx负载均衡和Keepalived的安装设置
  10. 【基础】C#异常处理的总结
  11. python基础===Python 代码优化常见技巧
  12. Motion control encoder extrapolation
  13. 【2017集美大学1412软工实践_助教博客】团队作业3——需求改进&amp;系统设计 成绩公示
  14. C++中构造函数的初始化列表(const、引用&amp;变量初始化)
  15. 20175324 2018-2019-2 《Java程序设计》第8周学习总结
  16. nginx在代理转发地图瓦片数据中的应用
  17. Machine Learning, Homework 9, Neural Nets
  18. jquery $与jQuery
  19. 如何设置SOLR的高亮 (highlight)?
  20. 69.Spartan-6的SelectIO资源

热门文章

  1. [LintCode] Trailing Zeroes 末尾零的个数
  2. mysql不能插入中文
  3. Sort with Swap(0, i)
  4. 安装OS X虚拟机错误vcpu-0:VERIFY vmcore/vmm/main/physMem_monitor.c:1123
  5. Spring MVC 框架的架包分析,功能作用,优点
  6. 使用Spring JdbcTemplate实现数据库操作
  7. numpy数组的操作
  8. windows或mac上对iOS设备截图
  9. 基于AngularJS的企业软件前端架构[转载]
  10. Windows安装和使用zookeeper