Aspose.Word邮件合并之自定义数据源
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) Word邮件合并2
第九周(1) Word邮件合并2 教学时间 2013-4-22 教学课时 2 教案序号 15 教学目标 1.进一步掌握邮件合并的技巧和方法.2.利用邮件合并制作准考证.3.掌握在同一页生成多个记录的方 ...
- 第八周(2) Word邮件合并1
源自:http://www.sxszjzx.com/~c20/12-2/office-gj/files/8-2/8-2.html 第八周(2) Word邮件合并1 教学时间 2013-4-16 教学课 ...
- Aspose.Words实现邮件合并功能和打印
前言 最近公司要做一个B/S架构的web打印系统,主要是可以上传.下载.邮件合并.打印等等,还有就是角色的分配.用户的创建.日志记录等等,跟一般的web系统一样.可能不一样的就是需求:想把excel的 ...
- Java 在Word中创建邮件合并模板并合并文本和图片
Word里面的邮件合并功能是一种可以快速批量操作同类型数据的方式,常见的如数据填充.打印等.其中必不可少的步骤包括用于填充的模板文档.填充的数据源以及实现邮件合并的功能.下面,通过Java程序展示如何 ...
- PyQt5实现邮件合并功能(GUI)
1. 实战Word批量 需要处理批量替换word的一些数据,数据源从Excel中来. Excel的百分数会变为数字,以及浮点数会多好多精度,为了原汁原味的数据,直接复制数据到文本文件.通过\t来分隔即 ...
- 利用Aspose.Word控件和Aspose.Cell控件,实现Word文档和Excel文档的模板化导出
我们知道,一般都导出的Word文档或者Excel文档,基本上分为两类,一类是动态生成全部文档的内容方式,一种是基于固定模板化的内容输出,后者在很多场合用的比较多,这也是企业报表规范化的一个体现. 我的 ...
- Aspose Word模板使用总结
Aspose Word模板使用总结 1.创建word模版,使用MergeFeild绑定数据 新建一个Word文档,命名为Template.doc 注意:这里并不是输入"< ...
- 利用Aspose.Word控件实现Word文档的操作
Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及输出,由于一般输出的内容比较正规化或者多数是表格居多,所以一般 ...
- 黄聪:利用Aspose.Word控件实现Word文档的操作(转)
撰写人:伍华聪 http://www.iqidi.com Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及 ...
随机推荐
- conda 查看已有环境
conda info -e # conda environments: # dlcv /Users/enzhao/suanec/libs/anaconda2/envs/dlcv py36 /Users ...
- idea maven cannot resolve symbol http报错问题解决
学习SpringMVC的过程中,在idea中使用maven管理依赖.在class中使用 javax.servlet.http.HttpServletRequest的时候,报错:cannot resol ...
- S-CMS企建v3二次SQL注入
S-CMS企建v3二次SQL注入 0x01 前言 继上一篇的S-CMS漏洞再来一波!首发T00ls 0x2 目录 Sql注入二次SQL注入 0x03 Sql注入 漏洞文件:\scms\bbs\bbs. ...
- #Java学习之路——基础阶段(第四篇)
我的学习阶段是跟着CZBK黑马的双源课程,学习目标以及博客是为了审查自己的学习情况,毕竟看一遍,敲一遍,和自己归纳总结一遍有着很大的区别,在此期间我会参杂Java疯狂讲义(第四版)里面的内容. 前言: ...
- [Swift]LeetCode41. 缺失的第一个正数 | First Missing Positive
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- [Swift]LeetCode122. 买卖股票的最佳时机 II | Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [Swift]LeetCode532. 数组中的K-diff数对 | K-diff Pairs in an Array
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...
- [SQL]LeetCode627. 交换工资 | Swap Salary
SQL架构 create table ), sex ), salary int) Truncate table salary insert into salary (id, name, sex, sa ...
- [Swift]LeetCode729. 我的日程安排表 I | My Calendar I
Implement a MyCalendar class to store your events. A new event can be added if adding the event will ...
- ubuntu16.04安装lnmp环境
1.安装mysql sudo apt install mysql-server 2.安装nginx和php #添加nginx和php的ppa源 sudo apt-add-repository ppa ...