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郵件合并模板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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. 郵件合并文本和圖片數據
以上面創建模板為例,填充文本數據及圖片數據
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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" }; //合并文本到模板 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); } } } |
合并效果:
到此這篇關于Java 在Word中創建郵件合并模板并合并文本和圖片的文章就介紹到這了,更多相關java創建郵件合并模板內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
原文鏈接:https://www.cnblogs.com/Yesi/p/15014205.html