1. 程式人生 > >使用Aspose.Words for Java完成複雜Word與PDF的匯出

使用Aspose.Words for Java完成複雜Word與PDF的匯出

使用Aspose.Words for Java 可以匯出複雜WORD PDF HTML 多種資料格式 
官方下載地址:http://www.aspose.com/java/word-component.aspx

我所用的版本是Aspose.Words.jdk16.jar 

先看效果圖

1-對資料行的匯出,分別是PDF與WORD格式







[size=large] 
使用該元件一共分為4個步驟 
1-定義模板 
2-載入模板 
3-填充資料 
4-設定匯出格式並匯出 
接下來我們按照以上4個步驟進行報表的匯出 

首先定義模板(可以再附件中下載)這裡只介紹最後一個個人簡歷的模板 
一個普通的自定義word就可以 

«TableStart:Employees» 


«TableEnd:Employees» 
這一對標記代表一個數據單元 Employees 是可以自定義的 填充資料來源時要對應上 

其他的就好理解了 比如«FirstName» 就是資料來源中的屬性 



接下來開始我們的匯出之旅吧!!!! 

第一步定義一個匯出的抽象類 

Java程式碼  收藏程式碼
  1. package com.epkj.words;  
  2. import org.springframework.stereotype.Component;  
  3. import com.aspose.words.Document;  
  4. @Component("ProcessWord")  
  5. public abstract
     class ProcessWord {  
  6.     public abstract Document execute(String templatePath) throws Exception;  
  7. }  

第二部寫一個具體的實現類 
Java程式碼  收藏程式碼
  1. package com.epkj.words;  
  2. import java.io.FileInputStream;  
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8. import org.springframework.stereotype.Component;  
  9. import com.aspose.words.Document;  
  10. /** 
  11.  * 帶圖片的匯出 
  12.  */  
  13. @Component("EmployeesReportDemo")  
  14. public class EmployeesReportDemo extends ProcessWord {  
  15.     @Override  
  16.     public Document execute(String templatePath) throws Exception {  
  17. //1 讀取模板  
  18.         Document doc = new Document(templatePath + "/" + "EmployeesReportDemo.doc");  
  19.         String imagePath = templatePath + "/" + "employees.jpg";  
  20. //2 填充資料來源  
  21.         doc.getMailMerge().executeWithRegions(new MapMailMergeDataSource(getMapList(imagePath), "Employees"));  
  22.         return doc;  
  23.     }  
  24.     private List<Map<String, Object>> getMapList(String imagePath) throws IOException {  
  25.         List<Map<String, Object>> dataList = new ArrayList<Map<String,Object>>();  
  26.         //讀取一個二進位制圖片  
  27.         FileInputStream fis = new FileInputStream(imagePath);  
  28.         byte[] image = new byte[fis.available()];  
  29.         fis.read(image);  
  30.         fis.close();  
  31.         for (int i = 0; i < 20; i++) {  
  32.             Map<String, Object> record = new HashMap<String, Object>();  
  33. //這裡的key要與模板中的<<xxxxx>>對應  
  34.             record.put("FirstName""歐陽");  
  35.             record.put("LastName""夏丹");  
  36.             record.put("Title""個人簡歷匯出Word PDF");  
  37.             record.put("Address""中國 北京市 東城區");  
  38.             record.put("City""北京");  
  39.             record.put("Country""遼寧瀋陽");  
  40.             //二進位制資料  
  41.             record.put("PhotoBLOB", image);  
  42.             dataList.add(record);  
  43.         }  
  44.         return dataList;  
  45.     }  
  46. }  



!!!因為Aspose.Words for Java不支援HashMap的資料格式,需要我們自己實現 
好在它提供了IMailMergeDataSource介面 
Java程式碼  收藏程式碼
  1. package com.epkj.words;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5. import com.aspose.words.IMailMergeDataSource;  
  6. /** 
  7.  * 實現對HashMap的支援 
  8.  */  
  9. public class MapMailMergeDataSource implements IMailMergeDataSource {  
  10.     private List<Map<String, Object>> dataList;  
  11.     private int index;  
  12.     //word模板中的«TableStart:tableName»«TableEnd:tableName»對應  
  13.     private String tableName = null;  
  14.     /** 
  15.      * @param dataList 資料集 
  16.      * @param tableName 與模板中的Name對應 
  17.      */  
  18.     public MapMailMergeDataSource(List<Map<String, Object>> dataList, String tableName) {  
  19.         this.dataList = dataList;  
  20.         this.tableName = tableName;  
  21.         index = -1;  
  22.     }  
  23.     /** 
  24.      * @param data 單個數據集 
  25.      * @param tableName 與模板中的Name對應 
  26.      */  
  27.     public MapMailMergeDataSource(Map<String, Object> data, String tableName) {  
  28.         if(this.dataList == null) {  
  29.             this.dataList = new ArrayList<Map<String,Object>>();  
  30.             this.dataList.add(data);  
  31.         }  
  32.         this.tableName = tableName;  
  33.         index = -1;  
  34.     }  
  35.     /** 
  36.      * 獲取結果集總數 
  37.      * @return 
  38.      */  
  39.     private int getCount() {  
  40.         return this.dataList.size();  
  41.     }  
  42.     @Override  
  43.     public IMailMergeDataSource getChildDataSource(String arg0)  
  44.             throws Exception {  
  45.         return null;  
  46.     }  
  47.     @Override  
  48.     public String getTableName() throws Exception {  
  49.         return this.tableName;  
  50.     }  
  51.     /** 
  52.      * 實現介面 
  53.      * 獲取當前index指向資料行的資料 
  54.      * 將資料存入args陣列中即可 
  55.      * @return ***返回false則不繫結資料*** 
  56.      */  
  57.     @Override  
  58.     public boolean getValue(String key, Object[] args) throws Exception {  
  59.         if(index < 0 || index >= this.getCount()) {  
  60.             return false;  
  61.         }  
  62.         if(args != null && args.length > 0) {  
  63.             args[0] = this.dataList.get(index).get(key);  
  64.             return true;  
  65.         } else {  
  66.             return false;  
  67.         }  
  68.     }  
  69.     /** 
  70.      * 實現介面 
  71.      * 判斷是否還有下一條記錄 
  72.      */  
  73.     @Override  
  74.     public boolean moveNext() throws Exception {  
  75.         index += 1;  
  76.         if(index >= this.getCount())  
  77.         {  
  78.             return false;  
  79.         }  
  80.         return true;  
  81.     }  
  82. }  



這樣我們就把資料填充好了。接下來就是匯出了 
Java程式碼  收藏程式碼
  1. package com.epkj.words;  
  2. import javax.servlet.ServletContext;  
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.context.WebApplicationContext;  
  8. import org.springframework.web.context.support.WebApplicationContextUtils;  
  9. import com.aspose.words.Document;  
  10. import com.aspose.words.SaveFormat;  
  11. /** 
  12.  * 所有匯出功能由該類完成 
  13.  */  
  14. @Controller  
  15. @RequestMapping("/DemoBaseController.do")  
  16. public class DemoBaseController {  
  17.     @RequestMapping(params =