聲明:轉載需要注明出處 https://www.cnblogs.com/sun-flower1314/p/10126111.html
本篇是關於利用FreeMarker導出Word的實現步驟。
優點:采用FreeMarker是導出Word的最佳實現,非常的靈活,能夠按照自己指定的樣式設置並輸出內容,操作簡單方便,代碼實現也容易。代碼量少,樣式、內容容易控制,打印不變形,完全符合office標准
缺點:需要提前設計好word模板,把需要替換的地方用特殊標記標出來
關於使用POI的導出方案在另一篇博客:https://www.cnblogs.com/sun-flower1314/p/10128796.html
下面是實現的效果圖:
下面是實現步驟:
1.添加FreeMarker需要的jar包(這里用的是2.3.28版本,從網上的maven倉庫中獲取的)
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> </dependency>
2.然后制作需要導出的Word模板。先利用office工具生成導出怎樣的word樣式,如圖是我繪制的模板:
3.制作好了基本的樣式之后,然后另存為.xml格式文檔,如:
4.打開這個text.xml文件,在相應的地方填入${xx}表達式:
5.填好后,使用其Notepad++或Sublime工具打開文件,能夠看到xml的內容如下:
填入后,如果有可能${}與 telephone 分離,則刪除分離后${},然后在telephone上添加${}后保存。
另一種最安全的方式是:不刪除分離的${},先在telephone上添加${},保存后,用word工具打開test.xml,將原來分離的${}刪除即可。
6.成功修改后,將文件重命名為.ftl格式的文件。然后將文件放置在項目中或其他路徑。這里我是將其拷貝至包中
7. 接下來是代碼層的實現
package com.myHelloWorld; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Map; import freemarker.core.ParseException; import freemarker.log.Logger; import freemarker.template.Configuration; import freemarker.template.MalformedTemplateNameException; import freemarker.template.Template; import freemarker.template.TemplateException; import freemarker.template.TemplateExceptionHandler; import freemarker.template.TemplateNotFoundException; import sun.misc.BASE64Encoder; /** * @Description 利用FreeMarker導出Word * 2018年12月15日 下午10:23:40 * @Author Huang Xiaocong */ public class ExportMyWord { private Logger log = Logger.getLogger(ExportMyWord.class.toString()); private Configuration config = null; public ExportMyWord() { config = new Configuration(Configuration.VERSION_2_3_28); config.setDefaultEncoding("utf-8"); } /** * FreeMarker生成Word * @param dataMap 數據 * @param templateName 目標名 * @param saveFilePath 保存文件路徑的全路徑名(路徑+文件名) * @Author Huang Xiaocong 2018年12月15日 下午10:19:03 */ public void createWord(Map<String, Object> dataMap, String templateName, String saveFilePath) { //加載模板(路徑)數據 config.setClassForTemplateLoading(this.getClass(), ""); //設置異常處理器 這樣的話 即使沒有屬性也不會出錯 如:${list.name}...不會報錯 config.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER); Template template = null; if(templateName.endsWith(".ftl")) { templateName = templateName.substring(0, templateName.indexOf(".ftl")); } try { template = config.getTemplate(templateName + ".ftl"); } catch (TemplateNotFoundException e) { log.error("模板文件未找到", e); e.printStackTrace(); } catch (MalformedTemplateNameException e) { log.error("模板類型不正確", e); e.printStackTrace(); } catch (ParseException e) { log.error("解析模板出錯,請檢查模板格式", e); e.printStackTrace(); } catch (IOException e) { log.error("IO讀取失敗", e); e.printStackTrace(); } File outFile = new File(saveFilePath); if(!outFile.getParentFile().exists()) { outFile.getParentFile().mkdirs(); } Writer out = null; FileOutputStream fos = null; try { fos = new FileOutputStream(outFile); } catch (FileNotFoundException e) { log.error("輸出文件時未找到文件", e); e.printStackTrace(); } out = new BufferedWriter(new OutputStreamWriter(fos)); //將模板中的預先的代碼替換為數據 try { template.process(dataMap, out); } catch (TemplateException e) { log.error("填充模板時異常", e); e.printStackTrace(); } catch (IOException e) { log.error("IO讀取時異常", e); e.printStackTrace(); } log.info("由模板文件:" + templateName + ".ftl" + " 生成文件 :" + saveFilePath + " 成功!!"); try { out.close();//web項目不可關閉 } catch (IOException e) { log.error("關閉Write對象出錯", e); e.printStackTrace(); } } /** * 獲得圖片的Base64編碼 * @param imgFile * @return * @Author Huang Xiaocong 2018年12月15日 下午10:15:10 */ public String getImageStr(String imgFile) { InputStream in = null; byte[] data = null; try { in = new FileInputStream(imgFile); } catch (FileNotFoundException e) { log.error("加載圖片未找到", e); e.printStackTrace(); } try { data = new byte[in.available()]; //注:FileInputStream.available()方法可以從輸入流中阻斷由下一個方法調用這個輸入流中讀取的剩余字節數 in.read(data); in.close(); } catch (IOException e) { log.error("IO操作圖片錯誤", e); e.printStackTrace(); } BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data); } }
下面是測試類:
public static void main(String[] args) { ExportMyWord emw = new ExportMyWord(); Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("name", "黃xx"); dataMap.put("age", 26); dataMap.put("blog", "sun_flower火柴客"); dataMap.put("email", "sun_flower@xxxx.com"); dataMap.put("gender", "男"); dataMap.put("imgheader", emw.getImageStr("D:\\picture\\23.jpg")); dataMap.put("telephone", "123456789101"); dataMap.put("address", "深圳"); dataMap.put("naturework", "全職"); dataMap.put("industry", "IT"); dataMap.put("aplication", "Java開發"); dataMap.put("time", "2013年-2017年"); dataMap.put("schoolname", "南昌大學"); dataMap.put("education", "本科"); dataMap.put("projectname", "電子證照xxxx"); dataMap.put("projecttime", "2017年3月"); dataMap.put("projectcontent", "我們除了有視、聽、味、嗅、觸這些外感系統之外,人類還有一個非常重要的內感系統,就是我們情緒和情感的世界。" + "這種感受是那樣地細膩、微妙、強烈、深沉;看不見、摸不着,說不清、道不明。..."); emw.createWord(dataMap, "test.ftl", "E:/簡歷.doc"); }
7.效果圖:
整個過程就是這樣。
對於需要多條記錄或循環的部分,只要在模板層的代碼中添加標簽:
<#list project as Item> <w:t>${Item.projectname}</w:t><w:br/> </#list>
這里說下需要注意的點:
1)很多項目中采用的是Log4j或 Commons Logging日志形式。而Freemarker自帶日志類型,即:
若導入的FreeMarker 2.3.x版本以下,可能回拋出Freemarker模版緩存問題:
Compiling FreeMarker template test.ftl[zh_CN,UTF-8,parsed] .... Could not find template in cache
看官方解釋:
2)插入圖片的時候格外小心,因為可能導出后是一堆圖片代碼,那是因為模板未能識別這個圖片。說明導出沒有問題,而是模板有問題。解決方案就是在原來的地方隨便插入一張圖片,然后在ftl中刪除圖片代碼就可以了。
同時 希望各位能提出寶貴的意見方便改進 不甚感激!!