簡單介紹aspose-words-18.10-jdk16做導出word


今天在搞那個用aspose words for java做導出word的功能,順便簡單介紹這個怎么用,我有兩個版本的破解版,就都做簡單介紹怎么用

警告:請勿用於商業用途,僅供學習研究使用,如有任何版權糾紛,本人概不負責!

由於aspose比較吃內存,操作大一點的文件就會堆溢出,所以請先設置好java虛擬機參數:-Xms1024m -Xmx1024m(參考值)

首先你可以去官網下載jar包也可以,我的是用jar包引進的

 

順帶一提,這兩個版本都是pojieban的,15.8.0需要加那個aspechweaver-1.9.1的jar,18.10就不用,然后都需要license.xml這個配置文件用於注冊的,主要是這個文件

 

jar包放到webapp下面的WEB_INF里面的lib文件夾下面,記得將包引進來(project Structrue——Modeules——右邊的Dependecies)看圖吧

這樣這個jar就引進來了,我們可以做測試了

 1 import com.aspose.words.Document;
 2 import com.aspose.words.License;
 3 import com.bns.modules.pr.utils.MapMailMergeDataSource;
 4 import org.aspectj.weaver.ast.Test;
 5 
 6 import java.io.FileInputStream;
 7 import java.io.InputStream;
 8 import java.util.ArrayList;
 9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 
13 public class WordExportUtilTest {
14 public static boolean getLicense() {
15 boolean result = false;
16 try {
17 InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml");
18 License aposeLic = new License();
19 aposeLic.setLicense(is);
20 result = true;
21 } catch (Exception e) {
22 e.printStackTrace();
23 }
24 return result;
25 }
26 
27 
28 public void savedocx() throws Exception {
29 //這里需要注冊一下,不注冊的話,到時候word就會出現水印了
30 if (!getLicense()) {
31 return;
32 }
33 //載入模板
34 Document doc = new Document("D:\\Template.docx");
35 List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
36 
37 String imagePath = "D:\\employees.jpg";
38 //讀取一個二進制圖片 
39 FileInputStream fis = new FileInputStream(imagePath);
40 byte[] image = new byte[fis.available()];
41 fis.read(image);
42 fis.close();
43 
44 for (int i = 0; i < 5; i++) {
45 Map<String, Object> record = new HashMap<String, Object>();
46 //這里的key要與模板中的<<xxxxx>>對應
47 record.put("FirstName", "歐陽");
48 record.put("LastName", "夏丹");
49 record.put("Time", "2018/12/20測試");
50 record.put("Title", "個人簡歷導出Word PDF");
51 record.put("Address", "中國 北京市 東城區");
52 record.put("City", "北京");
53 record.put("Country", "遼寧沈陽");
54 record.put("PhotoBLOB", image);
55 dataList.add(record);
56 }
57 //填充數據源
58 doc.getMailMerge().executeWithRegions(new MapMailMergeDataSource(dataList, "UserList"));
59 //保存合並后的文檔
60 doc.save("D:\\Template1.docx");
61 }
62 
63 }

上面的測試的代碼,不過因為Aspose.Words for Java不支持HashMap的數據格式,需要我們自己實現 
好在它提供了IMailMergeDataSource接口 

 1 import com.aspose.words.IMailMergeDataSource;
 2 import com.aspose.words.ref.Ref;
 3 
 4 
 5 import java.util.ArrayList;
 6 import java.util.List;
 7 import java.util.Map;
 8 
 9 /**
10 * 因為Aspose.Words for Java不支持HashMap的數據格式,所以這個類是為了實現對HashMap的支持
11 * */
12 public class MapMailMergeDataSource implements IMailMergeDataSource {
13 private List<Map<String, Object>> dataList;
14 private int index;
15 
16 //word模板中的«TableStart:tableName»«TableEnd:tableName»對應
17 private String tableName = null;
18 
19 /**
20 * @param dataList 數據集
21 * @param tableName 與模板中的Name對應
22 */
23 public MapMailMergeDataSource(List<Map<String, Object>> dataList, String tableName) {
24 this.dataList = dataList;
25 this.tableName = tableName;
26 index = -1;
27 }
28 
29 /**
30 * @param data 單個數據集
31 * @param tableName 與模板中的Name對應
32 */
33 public MapMailMergeDataSource(Map<String, Object> data, String tableName) {
34 if (this.dataList == null) {
35 this.dataList = new ArrayList<Map<String, Object>>();
36 this.dataList.add(data);
37 }
38 this.tableName = tableName;
39 index = -1;
40 }
41 
42 /**
43 * 獲取結果集總數
44 *
45 * @return
46 */
47 private int getCount() {
48 return this.dataList.size();
49 }
50 
51 @Override
52 public IMailMergeDataSource getChildDataSource(String arg0)
53 throws Exception {
54 return null;
55 }
56 
57 @Override
58 public String getTableName() throws Exception {
59 return this.tableName;
60 }
61 
62 /**
63 * 實現接口
64 * 判斷是否還有下一條記錄
65 */
66 @Override
67 public boolean moveNext() throws Exception {
68 index += 1;
69 if (index >= this.getCount()) {
70 return false;
71 }
72 return true;
73 }
74 
75 /**
76 * 實現接口
77 * 獲取當前index指向數據行的數據
78 * 將數據存入args數組中即可
79 *
80 * @return ***返回false則不綁定數據***
81 */
82 @Override
83 public boolean getValue(String key, Ref<Object> args) throws Exception {
84 if (index < 0 || index >= this.getCount()) {
85 return false;
86 }
87 if (args != null) {
88 args.set(this.dataList.get(index).get(key));
89 return true;
90 } else {
91 return false;
92 }
93 }
94 }

這里大家要開始注意了

 

我上面用的是18.10版本的是可以支持這個Ref<object> 的寫法的,而且Ref里面實現了set的方法

我們才可以調用set的方法,好像18.6以后的版本都可以,之前的版本我沒試過15.8的版本是不行的,所以15.8以下的版本都是不行的

如果使用15.8版本的話,需要改成這樣

 1 public boolean getValue(String key, Object[] args ) throws Exception {
 2 if (index < 0 || index >= this.getCount()) {
 3 return false;
 4 }
 5 if (args != null && args.length > 0) {
 6 //args.set(this.dataList.get(index).get(key));
 7 args[0] = this.dataList.get(index).get(key);
 8 return true;
 9 } else {
10 return false;
11 }
12 }


上面的代碼和圖是aspose words-15.8才需要的改的

還要一個重要的事,就是模板,這個工具是需要模板來導出的,

上面需要注意的是《》里面不是手寫的而是插入的,看圖吧

而且還需要《TableStart:UserList》***《TableEnd:UserList》這兩個對應的域,相當於循環***,就是要循環的內容

要跟代碼對應哦,如果你那個寫成abc,你就要寫成《TableStart:abc》***《TableEnd:abc》

第一個是模板,第二個是圖片的,第三個才是生成的word

這樣就是結果了

上面說的jdk1.8的,我覺得用aspose words18.10的吧,比較版本比較新,兼容1.8,而且支持ref<object>的使用,而15.8的只能用Object[] ,這就是我上面提到的區別

我已經把打包好了,15.8一個包,18.10一個包

15.8的鏈接:https://download.csdn.net/download/qq_32003379/10868410

18.10的鏈接:https://download.csdn.net/download/qq_32003379/10868413

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM