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来做报表输出,可以实现多样化的报表设计及 ...
随机推荐
- Logstash导入数据到ElasticSearch
一:在Windows环境 1 下载解压Logstash的压缩包 2 在Logstash的压缩包中安装Logstash-jdbc-input插件: 在Bin命令行下运行命令: .\logstash-pl ...
- 小甲鱼Python3笔记
000-愉快的开始 入门容易,学习难,代码量少. 跨平台: Windows, Mac OS, Linux, UNIX. 应用领域: 操作系统 WEB 3D动画 企业应用 云计算等等. 001-我和Py ...
- 文件上传控件bootstrap-fileinput的使用
1.插件下载地址:https://github.com/kartik-v/bootstrap-fileinput 2.插件的引用 需要引用jquery 需要结合bootstrap使用,即页面需要引入b ...
- unittest测试套件
测试套件就是测试集,测试集是测试用例的集合. a.按用例顺序执行(addtest) 当addtest与unittest的测试规则冲突时,仍然按照ASCII码的顺序执行. import unittest ...
- boot+Xss防攻击的处理方案
以下是boot+Xss防攻击的(解决处理JSON入参)处理方案,第二个亲测有效 https://www.jianshu.com/p/3e4b00b8ff3ahttps://www.jianshu.co ...
- 深入解析Java反射-invoke方法
博客原文:http://www.sczyh30.com/posts/Java/java-reflection-2/ 上篇文章中回顾了一下Java反射相关的基础内容.这一节我们来深入研究Method类中 ...
- 如何在微信小程序定义全局变量、全局函数、如何实现 函数复用 模块化开发等问题详解
1.如何定义全局数据 在app.js的App({})中定义的数据或函数都是全局的,在页面中可以通过var app = getApp(); app.function/key的方式调用,不过我们没有必要 ...
- Web前端-Vue.js必备框架(三)
Web前端-Vue.js必备框架(三) vue是一款渐进式javascript框架,由evan you开发.vue成为前端开发的必备之一. vue的好处轻量级,渐进式框架,响应式更新机制. 开发环境, ...
- [Swift]LeetCode265.粉刷房子 II $ Paint House II
There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...
- android自动化必备之SDK
进入到SDK包中,通过打开SDK manager.exe即可看到SDK管理界面,可能部分童靴发现一直在加载出不来,我们需要设置代理来解决: 选择工具栏上的Tools->Options打开如下窗口 ...