Aspose.Word在进行邮件合并时,默认的几个重载方法对Database支持比较友好,但是也可以通过自定义数据源来实现从集合或者对象中返回数据进行邮件合并。

自定义数据源主要是通过实现IMailMergeDataSource接口来实现的。官方的例子如下:

[C#]

public void MailMergeCustomDataSource()
{
// Create some data that we will use in the mail merge.
CustomerList customers = new CustomerList();
customers.Add(new Customer("Thomas Hardy", "120 Hanover Sq., London"));
customers.Add(new Customer("Paolo Accorti", "Via Monte Bianco 34, Torino")); // Open the template document.
Document doc = new Document(MyDir + "MailMerge.CustomDataSource.doc"); // To be able to mail merge from your own data source, it must be wrapped
// into an object that implements the IMailMergeDataSource interface.
CustomerMailMergeDataSource customersDataSource = new CustomerMailMergeDataSource(customers); // Now you can pass your data source into Aspose.Words.
doc.MailMerge.Execute(customersDataSource); doc.Save(MyDir + "MailMerge.CustomDataSource Out.doc");
} /// <summary>
/// An example of a "data entity" class in your application.
/// </summary>
public class Customer
{
public Customer(string aFullName, string anAddress)
{
mFullName = aFullName;
mAddress = anAddress;
} public string FullName
{
get { return mFullName; }
set { mFullName = value; }
} public string Address
{
get { return mAddress; }
set { mAddress = value; }
} private string mFullName;
private string mAddress;
} /// <summary>
/// An example of a typed collection that contains your "data" objects.
/// </summary>
public class CustomerList : ArrayList
{
public new Customer this[int index]
{
get { return (Customer)base[index]; }
set { base[index] = value; }
}
} /// <summary>
/// A custom mail merge data source that you implement to allow Aspose.Words
/// to mail merge data from your Customer objects into Microsoft Word documents.
/// </summary>
public class CustomerMailMergeDataSource : IMailMergeDataSource
{
public CustomerMailMergeDataSource(CustomerList customers)
{
mCustomers = customers; // When the data source is initialized, it must be positioned before the first record.
mRecordIndex= -;
} /// <summary>
/// The name of the data source. Used by Aspose.Words only when executing mail merge with repeatable regions.
/// </summary>
public string TableName
{
get { return "Customer"; }
} /// <summary>
/// Aspose.Words calls this method to get a value for every data field.
/// </summary>
public bool GetValue(string fieldName, out object fieldValue)
{
switch (fieldName)
{
case "FullName":
fieldValue = mCustomers[mRecordIndex].FullName;
return true;
case "Address":
fieldValue = mCustomers[mRecordIndex].Address;
return true;
default:
// A field with this name was not found,
// return false to the Aspose.Words mail merge engine.
fieldValue = null;
return false;
}
} /// <summary>
/// A standard implementation for moving to a next record in a collection.
/// </summary>
public bool MoveNext()
{
if (!IsEof)
mRecordIndex++; return (!IsEof);
} public IMailMergeDataSource GetChildDataSource(string tableName)
{
return null;
} private bool IsEof
{
get { return (mRecordIndex >= mCustomers.Count); }
} private readonly CustomerList mCustomers;
private int mRecordIndex;
}

参考文档:

https://apireference.aspose.com/net/words/aspose.words.mailmerging/imailmergedatasource

最新文章

  1. 关于英语PETS5备考的一些事
  2. Mac&amp;nbsp;常用快捷键
  3. Sphinx和coreseek检索引擎
  4. Hibernate的各种关联关系
  5. poj2253 最短路 floyd Frogger
  6. BNUOJ-29357 Bread Sorting 模拟
  7. iOS开发——网络编程OC篇&amp;(二)XMPP实现用户登录与注销
  8. 基于管道通知的百万并发长连接server模型
  9. javascript DOM艺术
  10. sql -实验二
  11. app开发历程——android手机显示服务器端图片思路
  12. java中的“包”与C#中的“命名空间
  13. BEGINNING SHAREPOINT&amp;#174; 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 站点设置
  14. getResources提取资源文件
  15. MSIL实用指南-装箱拆箱
  16. SQLite 创建表(http://www.w3cschool.cc/sqlite/sqlite-create-table.html)
  17. Java 读书笔记 (十三) for each 循环
  18. [Maven]Maven构建可执行的jar包(包含依赖jar包)
  19. 面试必备:ArrayList源码解析(JDK8)
  20. [Go] defer 语句

热门文章

  1. Expedition---POJ - 2431
  2. 微信小程序60秒倒计时
  3. unittest测试用例的执行顺序
  4. C盘突然爆满
  5. LeetCode编程训练 - 滑动窗口(Sliding Window)
  6. Vue 学习笔记 — css属性计算的问题
  7. Trie树详解及其应用
  8. [Swift]LeetCode118. 杨辉三角 | Pascal&#39;s Triangle
  9. [Swift]LeetCode136. 只出现一次的数字 | Single Number
  10. [Swift]LeetCode161. 一次编辑距离 $ One Edit Distance