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

Aspose.Word邮件合并之自定义数据源的更多相关文章

  1. 第九周(1) Word邮件合并2

    第九周(1) Word邮件合并2 教学时间 2013-4-22 教学课时 2 教案序号 15 教学目标 1.进一步掌握邮件合并的技巧和方法.2.利用邮件合并制作准考证.3.掌握在同一页生成多个记录的方 ...

  2. 第八周(2) Word邮件合并1

    源自:http://www.sxszjzx.com/~c20/12-2/office-gj/files/8-2/8-2.html 第八周(2) Word邮件合并1 教学时间 2013-4-16 教学课 ...

  3. Aspose.Words实现邮件合并功能和打印

    前言 最近公司要做一个B/S架构的web打印系统,主要是可以上传.下载.邮件合并.打印等等,还有就是角色的分配.用户的创建.日志记录等等,跟一般的web系统一样.可能不一样的就是需求:想把excel的 ...

  4. Java 在Word中创建邮件合并模板并合并文本和图片

    Word里面的邮件合并功能是一种可以快速批量操作同类型数据的方式,常见的如数据填充.打印等.其中必不可少的步骤包括用于填充的模板文档.填充的数据源以及实现邮件合并的功能.下面,通过Java程序展示如何 ...

  5. PyQt5实现邮件合并功能(GUI)

    1. 实战Word批量 需要处理批量替换word的一些数据,数据源从Excel中来. Excel的百分数会变为数字,以及浮点数会多好多精度,为了原汁原味的数据,直接复制数据到文本文件.通过\t来分隔即 ...

  6. 利用Aspose.Word控件和Aspose.Cell控件,实现Word文档和Excel文档的模板化导出

    我们知道,一般都导出的Word文档或者Excel文档,基本上分为两类,一类是动态生成全部文档的内容方式,一种是基于固定模板化的内容输出,后者在很多场合用的比较多,这也是企业报表规范化的一个体现. 我的 ...

  7. Aspose Word模板使用总结

    Aspose Word模板使用总结 1.创建word模版,使用MergeFeild绑定数据     新建一个Word文档,命名为Template.doc     注意:这里并不是输入"< ...

  8. 利用Aspose.Word控件实现Word文档的操作

    Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及输出,由于一般输出的内容比较正规化或者多数是表格居多,所以一般 ...

  9. 黄聪:利用Aspose.Word控件实现Word文档的操作(转)

    撰写人:伍华聪  http://www.iqidi.com  Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及 ...

随机推荐

  1. for循环:用turtle画一颗五角星

    import turtle # 设置初始位置 turtle.penup() turtle.left(90) turtle.fd(200) turtle.pendown() turtle.right(9 ...

  2. win10 vscode使用 智能提示

    1.没有第三方库的智能提示 参考:https://code.visualstudio.com/docs/python/editing 1.点开Settings 2.搜索加添加 3.添加后的内容 然后就 ...

  3. jstl使用中的错误----基于idea

    第一:首先正确将jstl.jar和standard.jar导入项目的lib目录下,注意两者的版本信息 第二: <%@ taglib prefix="c" uri=" ...

  4. Shadow Copying导致ASP.NET应用启动很慢的解决办法

    What's Shadow Copying? 我们安装一个应用程序并启动后,我们是无法更新应用程序安装目录中程序集文件的.如果强制替换会提示文件正在使用,如下图所示. 那你可能会问,为什么会无法更新呢 ...

  5. [Swift]LeetCode688. “马”在棋盘上的概率 | Knight Probability in Chessboard

    On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K ...

  6. [Swift]LeetCode717. 1比特与2比特字符 | 1-bit and 2-bit Characters

    We have two special characters. The first character can be represented by one bit 0. The second char ...

  7. VSCode与Deepin资源管理器冲突

    解决方式: xdg-mime default dde-file-manager.desktop inode/directory 此外,网上有较多推荐(在deepin 15.8版本上测试无效): gvf ...

  8. Elasticsearch Document

    1.  基本概念回顾 1.1.  Node 节点是一个服务器,它是集群的一部分,存储数据,并参与集群的索引和搜索功能 节点有一个名称标识,该名称在缺省情况下是在启动时分配给节点的随机全局惟一标识符(U ...

  9. 第五章 服务容错保护:Spring Cloud Hystrix

    在微服务架构中,我们将系统拆分为很多个服务,各个服务之间通过注册与订阅的方式相互依赖,由于各个服务都是在各自的进程中运行,就有可能由于网络原因或者服务自身的问题导致调用故障或延迟,随着服务的积压,可能 ...

  10. redis 系列18 事件

    一.概述 Redis服务器是一个事件驱动程序,服务器需要处理两类事件:1文件事件,2时间事件.文件事件是关于客户端与服务器之间的通信操作.时间事件是关于服务器内部的一些定时操作.本篇还是参照" ...