本文将介绍如何使用spire.doc组件创建word邮件合并模板文档,以及如何合并文本和图片到已有的模板文档。
创建邮件合并模板文档
c#
//创建document实例
document document = new document();
//添加节
section section = document.addsection();
//添加段落
paragraph paragraph = section.addparagraph();
//添加文本
paragraph.appendtext("\n姓 名 : ");
//添加合并域“name”
paragraph.appendfield("name", fieldtype.fieldmergefield);
//添加文本
paragraph.appendtext("\n电 话 : ");
//添加合并域"phone"
paragraph.appendfield("phone", fieldtype.fieldmergefield);
//添加文本
paragraph.appendtext("\n部 门 : ");
//添加合并域“department”
paragraph.appendfield("department", fieldtype.fieldmergefield);
//添加文本
paragraph.appendtext("\n照 片 : ");
//添加合并域”photo”
paragraph.appendfield("image:photo", fieldtype.fieldmergefield);
//保存并关闭文档
document.savetofile("模板.docx", fileformat.docx2013);
vb.net
'创建document实例
dim document as document = new document
'添加节
dim section as section = document.addsection
'添加段落
dim paragraph as paragraph = section.addparagraph
'添加文本
paragraph.appendtext("\n姓 名 :")
'添加合并域“name”
paragraph.appendfield("name", fieldtype.fieldmergefield)
'添加文本
paragraph.appendtext("\n电 话 :")
'添加合并域"phone"
paragraph.appendfield("phone", fieldtype.fieldmergefield)
'添加文本
paragraph.appendtext("\n部 门 :")
'添加合并域“department”
paragraph.appendfield("department", fieldtype.fieldmergefield)
'添加文本
paragraph.appendtext("\n照 片 :")
'添加合并域”photo”
paragraph.appendfield("image:photo", fieldtype.fieldmergefield)
'保存并关闭文档
document.savetofile("模板.docx ", fileformat.docx2013)
合并文本和图片到模板
c#
//载入模板文档
document doc = new document();
doc.loadfromfile(@"模板.docx");
var textfieldnames = new string[] { "name", "phone", "department" };
var textfieldvalues = new string[] { "李 晓 飞", "15581678910", "技 术 支 持" };
var imagefieldnames = new string[] { "photo" };
var imagefieldvalues = new string[] { "image.jpg" };
//合并文本到模板
doc.mailmerge.execute(textfieldnames, textfieldvalues);
//创建合并图片自定义事件
doc.mailmerge.mergeimagefield = new mergeimagefieldeventhandler(mailmerge_mergeimagefield);
//合并图片到模板
doc.mailmerge.execute(imagefieldnames, imagefieldvalues);
//保存文档
doc.savetofile("result.docx", fileformat.docx);
//载入图片
static void mailmerge_mergeimagefield(object sender, mergeimagefieldeventargs field)
{
string filepath = field.fieldvalue as string;
if (!string.isnullorempty(filepath))
{
field.image = image.fromfile(filepath);
}
}
vb.net
'载入模板文档
dim doc as document = new document
doc.loadfromfile("模板.docx")
dim textfieldnames as var = new string() {"name", "phone", "department"}
dim textfieldvalues as var = new string() {"李 晓 飞", "15581678910", "技 术 支 持"}
dim imagefieldnames as var = new string() {"photo"}
dim imagefieldvalues as var = new string() {"image.jpg"}
'合并文本到模板
doc.mailmerge.execute(textfieldnames, textfieldvalues)
'创建合并图片自定义事件
addhandler doc.mailmerge.mergeimagefield, addressof me.mailmerge_mergeimagefield
'合并图片到模板
doc.mailmerge.execute(imagefieldnames, imagefieldvalues)
'保存文档
doc.savetofile("result.docx", fileformat.docx)
'载入图片
private shared sub mailmerge_mergeimagefield(byval sender as object, byval field as mergeimagefieldeventargs)
dim filepath as string = ctype(field.fieldvalue,string)
if not string.isnullorempty(filepath) then
field.image = image.fromfile(filepath)
end if
end sub