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

输出结果截图:

用Spire.doc来合并邮件的更多相关文章

  1. 在C#中使用Spire.doc对word的操作总结

    在C#中使用Spire.doc对word的操作总结 在最近的工程中我们要处理一些word文档.通过在网上的大量搜索,我发现大多数软件功能不是不完整就是有重复.极少数可以完全实现的word组件又要收费. ...

  2. 【产品对比】Word开发工具Aspose.Words和Spire.Doc性能和优劣对比一览

    转:evget.com/article/2018/4/3/27885.html 概述:Microsoft Office Word是微软公司的一个文字处理器应用程序,作为办公软件必不可少的神器之一,Wo ...

  3. Spire.DOC生成表格测试

    首先,很感谢Jack对我的信任,让我来写一个评测,在此对Jack说一声抱歉,由于本人愚钝,并且最近项目比较紧张,把评测这个事情脱了一个月之久,由于往后的日子可能更忙,所以今晚抽空只能只写了一个小程序来 ...

  4. SPIRE.DOC - .NET开发者的福利

    SPIRE.DOC - .NET开发者的福利 前面我们使用过Spire.XLS for .NET Component创建Excel文件.最近试用了下.DOC 方面的API.这次测试的产品是.通过使用S ...

  5. [.NET] 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc

    开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc [博主]反骨仔 [原文地址]http://www.cnblogs.com/li ...

  6. 使用Spire.Doc来转换文本

    使用Spire.Doc来转换文本 前段时间,我为不熟悉这个产品的读者们写了一篇关于我对 Spire.Doc的初识印象.Spire.Doc是一个专业的Word .NET库,它是专门为开发人员设计的用来快 ...

  7. Spire.Doc组件读取与写入Word

    之前写了一篇开源组件DocX读写word的文章,当时时间比较匆忙选了这个组件,使用过程中还是有些不便,不能提前定义好模版,插入Form表单域进行替换.最近无意中发现Spire.Doc组件功能很强大,目 ...

  8. Word转图片(使用Spire.doc)

    Spire.Doc for .NET是一款由E-iceblue公司开发的专业的Word .NET类库.支持.net,WPF,Silverlight, 下载地址:http://www.e-iceblue ...

  9. 利用Spire for .NET实现办公自动化——Spire.Doc

    今天研究了一下E-ICEBLUE公司的Spire for .NET系列产品.我们可以通过利用这个系列的dll库文件轻松的实现办公自动化的需求,而且不需要安装相应的办公软件.有关于Spire .NET系 ...

随机推荐

  1. linux 多线程基础

    参考出处:http://www.cnblogs.com/skynet/archive/2010/10/30/1865267.html 1.进程与线程 进程是程序代码在系统中的具体实现.进程是拥有所需资 ...

  2. Cocos2d 利用继承Draw方法制作可显示三维数据(宠物三维等)的三角形显示面板

    很久没有写博客了,这段时间比较忙,又是搬家又是做自己的项目,还有太多琐碎的事情缠身,好不容易抽出时间把最近自己做的一些简单例子记录一下. 在我的项目中,我需要一个显示面板来显示游戏中的一个三维数据,例 ...

  3. Java 清除数组相同元素

    定义一个函数清除该数组的重复元素,返回一个不能浪费长度的数组.代码如下: import java.util.*; public class demo1 { public static void mai ...

  4. MAC下使用Charles抓取安卓模拟器数据

    一.安装Charles,这个不多记录 二.Charles数据乱码问题(参照这篇文章 http://blog.csdn.net/huanghanqian/article/details/52973651 ...

  5. apache配置反向代理

    http.conf 去掉前面的#号 LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/ ...

  6. SQL 2008升级SQL 2008 R2完全教程或者10.00.1600升级10.50.1600

    http://blog.csdn.net/feng19821209/article/details/8571571 SQL 2008升级SQL 2008 R2完全教程或者10.00.1600升级10. ...

  7. 阿里巴巴分布式服务框架 Dubbo 团队成员梁飞专访

    项目主页:http://alibaba.github.io/dubbo-doc-static/Home-zh.htm 我的博客:http://javatar.iteye.com

  8. Mac上开启Web服务

    $ sudo apachectl start$ httpd -v /Library/WebServer/Documents

  9. 【转】Apache 配置虚拟主机三种方式

    Apache 配置虚拟主机三种方式  原文博客http://www.cnblogs.com/hi-bazinga/archive/2012/04/23/2466605.html 一.基于IP 1. 假 ...

  10. Visual Studio 2015 CTP6 发布

    微软发布ASP.NET 5 支持在Windows.Mac和Linux上构建程序,Visual Studio 2015 CTP6(社区预览版)现已发布了.感兴趣的朋友们可以登录官网下载[http://w ...