本篇文章给大家分享的是有关freemarker中怎么导出word,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
原图:
替换后 :
年龄、职位、项目、简述对应的值依次替换成 ${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(); } } } } /** * 创建 word 文档 * 必须先设置response导出配置,然后解析模版,否则会出问题 * @throws IOException */ public static void createFile(File templateDir,String templateName,String docFileName, Map<String,Object> rootMap,HttpServletRequest request, HttpServletResponse response) throws IOException { // response.resetBuffer(); //设置导出 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.测试接口
@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和上面模板内的${key}保持一致即可 map.put("name", "刘禹锡"); map.put("age", 26); map.put("position", "php开发"); map.put("project", "不动产登记项目"); map.put("note", "php是世界上最好的语言!!!"); String docFileName ="word-模板导出测试.doc"; String templateName = "ftl2doc.ftl"; FreemarkerUtil.createFile(file,templateName,docFileName, map, req, resp); } catch (IOException e) { e.printStackTrace(); } }
测试结果导出文件如下:
以上就是freemarker中怎么导出word,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。