【Java】用Freemarker完美導出word文檔(帶圖片)或POI導出、在線預覽--作爲基線查看

原文地址:https://www.cnblogs.com/zwqh/p/7182953.html

建議看如下文章,做過修改:兩種方式建議使用第二種

可以先看下這篇文章:通過FreeMarker生成word文檔及導出PDF文件

0、在線預覽:利用微軟在線預覽鏈接展示office相關文檔內容

當我們需要將文檔展示到頁面的時候,往往會想到將office相關文件轉換成pdf,然後再通過h5標籤等方式展示到頁面,無論是使用libreoffice或者openoffice,轉換後的pdf效果都會有一定的差別,但不影響使用,如果只是想做簡單的文檔展示功能,而又不想做得那麼複雜,可以借用微軟的在線展示office文件的鏈接,只需要在頁面加個iframe即可:

https://view.officeapps.live.com/op/view.aspx?src=http://xxx.abc.cn/zzz.docx

src後面跟的是你文檔所在服務器的具體地址。
--------------------- 
作者:ljihe 
來源:CSDN 
原文:https://blog.csdn.net/ljihe/article/details/77451117 
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

1、使用poi生成word文檔(最全例子)

1、poi:https://blog.csdn.net/owen_william/article/details/81290024

2、poi-tl:https://github.com/Sayi/poi-tl

2、Java  用Freemarker完美導出word文檔(帶圖片)

導出pdf的方法如下:

1、https://blog.csdn.net/mynamepg/article/details/85789213

2、百度網盤源碼(包含導出到word、導出到pdf、pdf導出到圖片的項目):

鏈接:https://pan.baidu.com/s/1acnpMUt3anm251oYktdVJw z95i

前言

最近在項目中,因客戶要求,將頁面內容(如合同協議)導出成word,在網上翻了好多,感覺太亂了,不過最後還是較好解決了這個問題。

準備材料

1.word原件 2.編輯器(推薦Firstobject free XML editor)或者使用sublime(更推薦)

sublime使用方法:打開xml代碼,修改關鍵位置的數據,這時候需要有格式化代碼的工具,才能看到規整的代碼,推薦使用sublime的格式化工具,需要安裝,參照sublime格式化。選擇selection->format->indent XML,就可以一鍵格式化當前XML文件

實現步驟

1.用Microsoft Office Word打開word原件;

2.把需要動態修改的內容替換成xxxxxxxx,如果有圖片,儘量選擇較小的圖片幾十K左右,並調整好位置;

3.另存爲,選擇保存類型Word 2003 XML 文檔(*.xml)【這裏說一下爲什麼用Microsoft Office Word打開且要保存爲Word 2003XML,本人親測,用WPS找不到Word 2003XML選項,如果保存爲Word XML,會有兼容問題,避免出現導出的word文檔不能用Word 2003打開的問題】,保存的文件名不要是中文;

4.用Firstobject free XML editor打開文件,選擇Tools下的Indent【或者按快捷鍵F8】格式化文件內容。左邊是文檔結構,右邊是文檔內容;

5. 將文檔內容中需要動態修改內容的地方,換成freemarker的標識。其實就是Map<String, Object>中key,如${landName};

6.在加入了圖片佔位的地方,會看到一片base64編碼後的代碼,把base64替換成${image},也就是Map<String, Object>中key,值必須要處理成base64;

  代碼如:<w:binData w:name="wordml://自定義.png" xml:space="preserve">${image}</w:binData>

  注意:“>${image}<”這尖括號中間不能加任何其他的諸如空格,tab,換行等符號。

  如果需要循環,則使用:<#list maps as map></#list>  maps是Map<String, Object>中key,值爲數組,map爲自定義;

7. 標識替換完之後,模板就弄完了,另存爲.ftl後綴文件即可。注意:一定不要用word打開ftl模板文件,否則xml內容會發生變化,導致前面的工作白做了。

另存爲.ftl後綴文件的方法如圖:

百度網盤源碼:

代碼實現

工具類WordUtils.Java

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 import java.io.OutputStreamWriter;
 7 import java.io.Writer;
 8 import java.net.URLEncoder;
 9 import java.util.Date;
10 import java.util.Map;
11 
12 import javax.servlet.ServletOutputStream;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 
16 import freemarker.template.Configuration;
17 import freemarker.template.Template;
18 
19 public class WordUtils {
20      //配置信息,代碼本身寫的還是很可讀的,就不過多註解了  
21     private static Configuration configuration = null;  
22     //這裏注意的是利用WordUtils的類加載器動態獲得模板文件的位置  
23    // private static final String templateFolder = WordUtils.class.getClassLoader().getResource("../../").getPath() + "WEB-INF/templetes/";  
24     private static final String templateFolder = "H:/我的項目/lm/lm/web/src/main/webapp/WEB-INF/templates";  
25     static {  
26         configuration = new Configuration();  
27         configuration.setDefaultEncoding("utf-8");  
28         try {  
29             configuration.setDirectoryForTemplateLoading(new File(templateFolder));  
30         } catch (IOException e) {  
31             e.printStackTrace();  
32         }  
33    }  
34   
35     private WordUtils() {  
36         throw new AssertionError();  
37     }  
38   
39     public static void exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response, Map map,String title,String ftlFile) throws IOException {  
40         Template freemarkerTemplate = configuration.getTemplate(ftlFile);  
41         File file = null;  
42         InputStream fin = null;  
43         ServletOutputStream out = null;  
44         try {  
45             // 調用工具類的createDoc方法生成Word文檔  
46             file = createDoc(map,freemarkerTemplate);  
47             fin = new FileInputStream(file);  
48   
49             response.setCharacterEncoding("utf-8");  
50             response.setContentType("application/msword");  
51             // 設置瀏覽器以下載的方式處理該文件名  
52             String fileName = title+DateUtil.formatDateDetailTime(new Date()) + ".doc";  
53             response.setHeader("Content-Disposition", "attachment;filename="  
54                     .concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));  
55   
56             out = response.getOutputStream();  
57             byte[] buffer = new byte[512];  // 緩衝區  
58             int bytesToRead = -1;  
59             // 通過循環將讀入的Word文件的內容輸出到瀏覽器中  
60             while((bytesToRead = fin.read(buffer)) != -1) {  
61                 out.write(buffer, 0, bytesToRead);  
62             }  
63         } finally {  
64             if(fin != null) fin.close();  
65             if(out != null) out.close();  
66             if(file != null) file.delete(); // 刪除臨時文件  
67         }  
68     }  
69   
70     private static File createDoc(Map<?, ?> dataMap, Template template) {  
71         String name =  "sellPlan.doc";  
72         File f = new File(name);  
73         Template t = template;  
74         try {  
75             // 這個地方不能使用FileWriter因爲需要指定編碼類型否則生成的Word文檔會因爲有無法識別的編碼而無法打開  
76             Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");  
77             t.process(dataMap, w);  
78             w.close();  
79         } catch (Exception ex) {  
80             ex.printStackTrace();  
81             throw new RuntimeException(ex);  
82         }  
83         return f;  
84     }  
85 }Action 1 @RequestMapping("/exportSellPlan")
 2     public @ResponseBody void exportSellPlan(Long id){
 3         Calendar calendar = Calendar.getInstance();// 取當前日期。
 4         if(id!=null){
 5             SellPlan plan=sellService.getSellPlanInfo(id);
 6              //獲得數據  
 7             Map<String, Object> map = new HashMap<String, Object>(); 
 8             map.put("bYear", plan.getBusinessYear()!=null?plan.getBusinessYear():"");
 9             map.put("lYear", plan.getLiveYear()!=null?plan.getLiveYear():"");
10             map.put("leader",plan.getLeader()!=null?plan.getLeader():""); 
11             map.put("phone", plan.getPhone()!=null?plan.getPhone():"");
12             map.put("curYear", calendar.get(Calendar.YEAR)+"");
13             map.put("image", getImageBase(plan.getPositionImage()));
14             try {
15                 WordUtils.exportMillCertificateWord(getRequest(),getResponse(),map,"方案","sellPlan.ftl");
16             } catch (IOException e) {
17                 // TODO Auto-generated catch block
18                 e.printStackTrace();
19             }    
20         }
21     }Base64處理 1  //獲得圖片的base64碼
 2     @SuppressWarnings("deprecation")
 3     public String getImageBase(String src) {
 4         if(src==null||src==""){
 5             return "";
 6         }
 7         File file = new File(getRequest().getRealPath("/")+src.replace(getRequest().getContextPath(), ""));
 8         if(!file.exists()) {
 9             return "";
10         }
11         InputStream in = null;
12         byte[] data = null;  
13         try {
14             in = new FileInputStream(file);
15         } catch (FileNotFoundException e1) {
16             e1.printStackTrace();
17         }
18         try {  
19             data = new byte[in.available()];  
20             in.read(data);  
21             in.close();  
22         } catch (IOException e) {  
23           e.printStackTrace();  
24         } 
25         BASE64Encoder encoder = new BASE64Encoder();
26         return encoder.encode(data);
27     }Javascript1 window.location.href="<%=path%>/exportSellPlan?id=" + id;pom.xml    <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.20</version>
        </dependency>結束語
如果對Freemarker標籤不熟的,可以在網上先學習下,瞭解文檔結構。
相關鏈接
Firstobject free XML editor下載地址:http://www.firstobject.com/dn_editor.htm
freemarker 官網:http://freemarker.org/

 

 

 

 

 

持之以恆

分類: Java

標籤: JavaWord導出

好文要頂 關注我 收藏該文  

朝霧輕寒
關注 - 0
粉絲 - 7

+加關注

4

1

« 上一篇:【windows】遠程桌面 把遠程服務器的explorer.exe進程關掉了,咋辦?
» 下一篇:【百度地圖】獲取某個覆蓋物的總面積

posted @ 2017-07-15 15:49 朝霧輕寒 閱讀(22918) 評論(7) 編輯 收藏

 

評論列表

  

#1樓 2017-11-28 17:04 zymqqc  

get !

支持(0)反對(0)

  

#2樓 2018-02-06 10:43 uuuuY  

樓主問下如果導出的word帶目錄樹,怎麼處理??

支持(0)反對(0)

  

#3樓[樓主] 2018-02-06 11:03 朝霧輕寒  

@ uuuuY
什麼樣的目錄樹?

支持(0)反對(0)

  

#4樓 2018-02-06 11:07 uuuuY  

@朝霧輕寒 
導出word文檔的時候生成一個目錄結構

像這樣的!!!

支持(0)反對(0)

  

#5樓[樓主] 2018-02-06 11:12 朝霧輕寒  

@ uuuuY
這個保存成xml時應該有相應的格式,可以循環出來,不過頁碼暫時不知道怎麼處理。

支持(0)反對(0)

  

#6樓 2018-02-06 11:16 uuuuY  

@朝霧輕寒 
好吧,謝謝樓主!!!本來導出word功能已經實現的了(就是按照你的帖子的教程去做的),現在就是加了需求,導出的word要生成目錄結構!!

支持(0)反對(0)

  

#7樓 2018-02-26 19:15 閃閃發光的代碼  

有得,支持一下,順便補充兩點
1)WPS也可以,另存爲Word XML文檔(最下面那個)
2)ftl文件的註釋方式是 <#--註釋內容-->
(舊版的<#comment> 註釋 </#comment>也還能用)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章