用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. jsp页面 如何通过el表达式获取request属性值

    1. 我在一个超连接后加个参数如:      http://localhost:8080/test/testjstl.jsp?pid=001    此时在jsp页面中,获取jsp传过来的pid的参数值 ...

  2. WEB前端工程师(实践)制作天气预报难度:简单

    需要准备:jQuery Bootstrap 天气预报API(本文中使用API可能会失效请灵活运用) CSS样式可以你自己去写这里只提出jQuery 请求数据和解析JSON数据 { "resu ...

  3. js数组操作总结

    1.数组的检测 ECMAScript3    if(value instanceof Array){ //执行操作 }    假定单一全局环境,如果网页存在多个框架,多个window,Array具有不 ...

  4. jasmine入门

    本文来自http://blog.fens.me/nodejs-jasmine-bdd 粉丝日志 张丹   前言TDD(Test Driven Development)测试驱动开发,是敏捷开发中提出的最 ...

  5. 配置Spark on YARN集群内存

    参考原文:http://blog.javachen.com/2015/06/09/memory-in-spark-on-yarn.html?utm_source=tuicool 运行文件有几个G大,默 ...

  6. Smart3D系列教程3之 《论照片三维重建中Smart3D几个工作模块的功能意义》

    [摘要] 近年来,倾斜摄影测量技术是国际测绘遥感领域近年发展起来的一项高新技术,利用照片进行三维重建成为一项关键性的技术.Smart3D软件,是照片三维重建主流软件之一,本文将就Smart3D建模软件 ...

  7. CentOS 安装 gcc-c++

    由于网络环境的问题,很难在线安装,可以 直接挂载安装盘文件,然后在Pacakage 中进行本地安装

  8. asp.net 使用DroDownList来实现二级联动

    今天做新闻发布系统的时候,用到了二级联动,我把使用方法记录下来,以便日后查阅以及帮助新手朋友们.下面是效果图: 下面来讲解一下实现的方法: 1.在.aspx页面中,拖入两个DroDownList控件. ...

  9. SQL Server复制出错文章集锦

    SQL Server复制出错文章集锦 为了方便大家对数据库复制过程中出错的时候更好地解决问题 本人收集了SQL Server相关复制出错解决的文章   The process could not ex ...

  10. 源文件移动后gdb不显示代码的原因

    源文件移动后gdb不显示代码的原因   问题 我们从一个最简单的C语言程序开始.源文件main.c在 用户目录gdb文件夹下. florian@florian-pc:~/gdb$ cat main.c ...