您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)freemarker中怎么導(dǎo)出word,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
原圖:
替換后 :
年齡、職位、項(xiàng)目、簡(jiǎn)述對(duì)應(yīng)的值依次替換成 ${age} ${position} ${project} ${note}
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
package org.jeecg.modules.system.util; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder; import java.util.Locale; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import freemarker.template.*; public class FreemarkerUtil { private static final Object LOCK = new Object(); private static Configuration cfg; private static FreemarkerUtil ftl ; private FreemarkerUtil(File file) throws IOException { cfg = new Configuration(); cfg.setDirectoryForTemplateLoading(file); cfg.setEncoding(Locale.getDefault(), "UTF-8"); cfg.setObjectWrapper(new DefaultObjectWrapper()); } private static void check(File file) { if (ftl == null) { synchronized (LOCK) { try { ftl = new FreemarkerUtil(file); } catch (IOException e) { e.printStackTrace(); } } } } /** * 創(chuàng)建 word 文檔 * 必須先設(shè)置response導(dǎo)出配置,然后解析模版,否則會(huì)出問(wèn)題 * @throws IOException */ public static void createFile(File templateDir,String templateName,String docFileName, Map<String,Object> rootMap,HttpServletRequest request, HttpServletResponse response) throws IOException { // response.resetBuffer(); //設(shè)置導(dǎo)出 response.addHeader("Cache-Control","no-cache"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/vnd.ms-word;charset=UTF-8"); String ua = request.getHeader("user-agent"); ua = ua == null ? null : ua.toLowerCase(); if(ua != null && (ua.indexOf("firefox") > 0 || ua.indexOf("safari")>0)){ try { docFileName = new String(docFileName.getBytes(),"ISO8859-1"); response.addHeader("Content-Disposition","attachment;filename=" + docFileName); } catch (Exception e) { } }else{ try { docFileName = URLEncoder.encode(docFileName, "utf-8"); response.addHeader("Content-Disposition","attachment;filename=" + docFileName); } catch (Exception e) { } } check(templateDir); //解析模版 Template temp = cfg.getTemplate(templateName, "UTF-8"); PrintWriter write = response.getWriter(); try { temp.process(rootMap, write); } catch (TemplateException e) { e.printStackTrace(); }finally { if(write != null){ write.flush(); write.close(); } } } }
7.測(cè)試接口
@Resource private ResourceLoader resourceLoader; @GetMapping(value = "/wordTest") public void wordTest(HttpServletRequest req,HttpServletResponse resp){ String path = "templates/word"; org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:"+path); try { File file = resource.getFile(); Map<String,Object> map = new HashMap<String,Object>(); //這里map的key和上面模板內(nèi)的${key}保持一致即可 map.put("name", "劉禹錫"); map.put("age", 26); map.put("position", "php開(kāi)發(fā)"); map.put("project", "不動(dòng)產(chǎn)登記項(xiàng)目"); map.put("note", "php是世界上最好的語(yǔ)言?。?!"); String docFileName ="word-模板導(dǎo)出測(cè)試.doc"; String templateName = "ftl2doc.ftl"; FreemarkerUtil.createFile(file,templateName,docFileName, map, req, resp); } catch (IOException e) { e.printStackTrace(); } }
測(cè)試結(jié)果導(dǎo)出文件如下:
以上就是freemarker中怎么導(dǎo)出word,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。