一、添加maven依賴,導入FreeMarker所需要的jar包
1 <dependency> 2 <groupId>org.freemarker</groupId> 3 <artifactId>freemarker</artifactId> 4 <version>2.3.20</version> 5 </dependency>
二、定義word模板文件
三、修改模板xml文件
把剛剛制作的word文件另存為xml文件
修改這個xml文件,開發工具我用的idea,我是把這個xml文件放在項目中,Ctrl+Alt+L進行格式化,這樣文件內容看起來結構比較清晰,方便修改,你也可以使用其他xml編輯器等等
把文檔內容中的動態數據,換成freemarker的標識。其實就是Map<String, Object>中key,如把段然濤換成${name};
教育經歷是需要循環遍歷的,在這里可以循環遍歷list,list中存儲的都是Map數據,然后再一一取出
折疊起來看就是這樣,一定不要忘記了結束的list標簽
接下來就是圖片了,在加入了圖片占位的地方,會看到一片base64編碼后的代碼,把base64刪除替換成${image},圖片不確定有多少張,也是需要循環遍歷的
這些全部弄好之后,模板就制作完了,修改文件為.ftl即可,然后把模板放入到項目中
四、代碼編寫
接下來就是代碼部分了,編寫FreeMarkerUtil工具類,並測試
1 package com.cccuu.project.utils; 2 3 import freemarker.template.Configuration; 4 import freemarker.template.DefaultObjectWrapper; 5 import freemarker.template.Template; 6 import freemarker.template.TemplateExceptionHandler; 7 import org.apache.log4j.Logger; 8 import sun.misc.BASE64Encoder; 9 10 import java.io.*; 11 import java.util.*; 12 13 /******************************************* 14 * 15 * @Package com.cccuu.project.utils 16 * @Author duan 17 * @Date 2018/7/27 11:51 18 * @Version V1.0 19 *******************************************/ 20 public class FreeMarkerUtil { 21 22 private static Logger log = Logger.getLogger(FreeMarkerUtil.class); 23 private static final String ENCODING = "UTF-8"; 24 private static Configuration cfg = new Configuration(); 25 26 //初始化cfg 27 static { 28 //設置模板所在文件夾 29 cfg.setClassForTemplateLoading(FreeMarkerUtil.class, "/templates/word"); 30 // setEncoding這個方法一定要設置國家及其編碼,不然在ftl中的中文在生成html后會變成亂碼 31 cfg.setEncoding(Locale.getDefault(), ENCODING); 32 // 設置對象的包裝器 33 cfg.setObjectWrapper(new DefaultObjectWrapper()); 34 // 設置異常處理器,這樣的話就可以${a.b.c.d}即使沒有屬性也不會出錯 35 cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER); 36 37 } 38 39 //獲取模板對象 40 public static Template getTemplate(String templateFileName) throws IOException { 41 return cfg.getTemplate(templateFileName, ENCODING); 42 } 43 44 /** 45 * 據數據及模板生成文件 46 * @param data Map的數據結果集 47 * @param templateFileName ftl模版文件名 48 * @param outFilePath 生成文件名稱(可帶路徑) 49 */ 50 public static File crateFile(Map<String, Object> data, String templateFileName, String outFilePath) { 51 Writer out = null; 52 File outFile = new File(outFilePath); 53 try { 54 // 獲取模板,並設置編碼方式,這個編碼必須要與頁面中的編碼格式一致 55 Template template = getTemplate(templateFileName); 56 if (!outFile.getParentFile().exists()) { 57 outFile.getParentFile().mkdirs(); 58 } 59 out = new OutputStreamWriter(new FileOutputStream(outFile), ENCODING); 60 // 處理模版 61 template.process(data, out); 62 out.flush(); 63 log.info("由模板文件" + templateFileName + "生成" + outFilePath + "成功."); 64 } catch (Exception e) { 65 log.error("由模板文件" + templateFileName + "生成" + outFilePath + "出錯"); 66 e.printStackTrace(); 67 } finally { 68 try { 69 if (out != null) { 70 out.close(); 71 } 72 } catch (IOException e) { 73 log.error("關閉Write對象出錯", e); 74 e.printStackTrace(); 75 } 76 } 77 return outFile; 78 } 79 80 //獲得圖片的base64碼 81 public static String getImageBase(String src) throws Exception { 82 if (src == null || src == "") { 83 return ""; 84 } 85 File file = new File(src); 86 if (!file.exists()) { 87 return ""; 88 } 89 InputStream in = null; 90 byte[] data = null; 91 try { 92 in = new FileInputStream(file); 93 data = new byte[in.available()]; 94 in.read(data); 95 in.close(); 96 } catch (IOException e) { 97 e.printStackTrace(); 98 } 99 BASE64Encoder encoder = new BASE64Encoder(); 100 return encoder.encode(data); 101 } 102 103 public static void main(String[] args) { 104 try { 105 Map<String, Object> data = new HashMap<String, Object>(); 106 data.put("name", "段然濤"); 107 data.put("sex", "男"); 108 data.put("birthday", "1994-03-14"); 109 data.put("phone", "17737138812"); 110 data.put("address", "河南省許昌市"); 111 data.put("school", "江西科技師范大學"); 112 List<Map<String, String>> educations = new ArrayList<Map<String, String>>(); 113 Map<String, String> paramsMap = new HashMap<String, String>(); 114 paramsMap.put("school", "禹州一高"); 115 paramsMap.put("startDate", "2008-09"); 116 paramsMap.put("endDate", "2012-06"); 117 paramsMap.put("person", "李磊"); 118 Map<String, String> paramsMap2 = new HashMap<String, String>(); 119 paramsMap2.put("school", "江西科技師范大學"); 120 paramsMap2.put("startDate", "2012-09"); 121 paramsMap2.put("endDate", "2016-07"); 122 paramsMap2.put("person", "李傑"); 123 educations.add(paramsMap); 124 educations.add(paramsMap2); 125 data.put("educations", educations); 126 List<String> images = new ArrayList<String>(); 127 images.add(getImageBase("C:/Users/Administrator/Desktop/圖片/timg.jpg")); 128 images.add(getImageBase("C:/Users/Administrator/Desktop/圖片/timg11.jpg")); 129 data.put("images", images); 130 crateFile(data, "文檔1.ftl", "C:/Users/Administrator/Desktop/文檔/簡歷.doc"); 131 } catch (Exception e) { 132 e.printStackTrace(); 133 } 134 } 138 }