Word里面的邮件合并功能是一种可以快速批量操作同类型数据的方式,常见的如数据填充、打印等。其中必不可少的步骤包括用于填充的模板文档、填充的数据源以及实现邮件合并的功能。下面,通过Java程序展示如何来实现创建模板,并通过邮件合并功能来合并文本数据和图片数据的方法,分别以2个示例来展示,即:
1. 创建Word填充模板
2. 邮件合并文本和图片
本次程序运行环境如下:
代码编译工具:IDEA Jdk版本:1.8.0 Word测试文档:.docx 2013 Word jar包工具:Free Spire.Doc for Java关于jar导入:
下载Free Spire.Doc for Java 到本地后,解压。然后执行如下步骤手动导入jar到Java程序:Project Structure(Shift+Ctrl+Alt+S)打开的界面中选择【Modules】—【Dependencies】,点击“+”,【JARs or directories…】,选择本地路径中的jar文件,添加后,勾选,点击“OK”。完成导入。

Java代码示例
1. 创建Word邮件合并模板
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
public class CreateTemplate {
    public static void main(String[] args) {
        //创建Document实例
        Document document = new Document();
        document.loadFromFile("test.docx");
        //获取第一节
        Section section = document.getSections().get(0);
        //添加4个段落
        Paragraph para1 = section.addParagraph();
        Paragraph para2 = section.addParagraph();
        Paragraph para3 = section.addParagraph();
        Paragraph para4 = section.addParagraph();
        //添加合并域,包括文字域、图片域
        para1.setText("姓名 : ");
        para1.appendField("Name", FieldType.Field_Merge_Field);
        para2.setText("邮件地址: ");
        para2.appendField("Email Address", FieldType.Field_Merge_Field);
        para3.setText("日期 : ");
        para3.appendField("Date", FieldType.Field_Merge_Field);
        para4.setText("图片:");
        para4.appendField("Image:image",FieldType.Field_Merge_Field);
        //保存模板文档
        document.saveToFile("template.docx", FileFormat.Docx);
        document.dispose();
    }
}
模板效果:

2. 邮件合并文本和图片数据
以上面创建模板为例,填充文本数据及图片数据
import com.spire.doc.*;
import com.spire.doc.reporting.MergeImageFieldEventArgs;
import com.spire.doc.reporting.MergeImageFieldEventHandler;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MailMerge {
    public static void main(String[] args) throws Exception{
        //创建Document实例,并加载邮件合并模板文档
        Document document = new Document();
        document.loadFromFile("template.docx");
        //按文本合并域名称,设置合并域的文本值
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        String[] textFieldNames  = new String[]{"Name", "Email Address", "Date"};
        String[] textFieldValues  = new String[]{"亚当斯密", "123456@163.com", dateString};
        //合并文本到模板
        document.getMailMerge().execute(textFieldNames,textFieldValues  );
        //按图片合并域名称,设置图片路径值
        String[] imageFieldNames  = new String[]{"image"};
        String[] imageFieldValues  = new String[]{"logo.jpg"};
        //调用邮件合并事件加载图片
        document.getMailMerge().MergeImageField = new MergeImageFieldEventHandler()
        {
            public void invoke(Object sender, MergeImageFieldEventArgs args)
            {
                mailMerge_MergeImageField(sender, args);
            }
        };
        //执行邮件合并
        document.getMailMerge().execute(imageFieldNames, imageFieldValues);
        //保存文档
        document.saveToFile("result.docx", FileFormat.Docx);
    }
    //创建邮件合并事件用于加载图片
    private static void mailMerge_MergeImageField(Object sender, MergeImageFieldEventArgs field) {
        String filePath = (String) field.getFieldValue();
        if (!filePath.isEmpty())
        {
            field.setImage(filePath);
        }
    }
}
合并效果:


