微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用Spire.doc来合并邮件

                               用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;
 
     @R_881_4045@on 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.docviewer the result file.
     System.Diagnostics.Process.Start(");
  }
 
void MailMerge_MergeField(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
{
   string m_contactName;
   string ContactName
     {
        get
        {
          return m_contactName;
         }
        set
        {
           m_contactName = value;
         }
    }
 
string m_fax;
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;
     }
  }
}

 

输出结果截图:

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐