千家信息网

freemarker中怎么导出word

发表于:2025-12-04 作者:千家信息网编辑
千家信息网最后更新 2025年12月04日,本篇文章给大家分享的是有关freemarker中怎么导出word,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。1.新建word文档,导出
千家信息网最后更新 2025年12月04日freemarker中怎么导出word

本篇文章给大家分享的是有关freemarker中怎么导出word,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

1.新建word文档,导出预期如下

2.另存为xml文件

3.编辑xml,将文字替换成表达式 ${argname}

原图:

替换后 :

年龄、职位、项目、简述对应的值依次替换成 ${age} ${position} ${project} ${note}

4.修改文件名称为:ftl2doc.ftl,放到项目里

5.查看pom依赖是否添加若没有需添加freemarker依赖
                                      org.springframework.boot                        spring-boot-starter-freemarker                
6.编写工具类
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 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 map = new HashMap();            //这里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,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

文件 项目 UTF-8 测试 文档 更多 模板 模版 知识 篇文章 实用 一致 不动产 世界 原图 就是 工作会 工具 年龄 接口 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 盛行时代网络安全吗 软件开发公司广告语 银行软件开发中心技术岗 南京大学网络安全培训 无线传感网络技术有什么特点 哈利波特手游能换服务器吗 怎么从电脑拷出数据库安装包 服务器正在维护中维护时间请参考 非root安装es数据库 河北专接本网络技术专业 mysql导入数据库语句 大学生提升网络技术的途径 mc东方服务器 网络安全知识闯关竞答第1期 图片数据库api json 护苗网络安全进课堂为主题的绘画 互联网科技体验中心 地址 知识产权数据库网络安全防御 杨浦区市场软件开发咨询热线 服务器机柜电源线 开源软件开发编辑器 与医药相关的数据库系统 店铺管理系统数据库设计报告 excel单价数据库建立方法 托管服务器安全性 中国应急管理部网络安全 国度RF现在有哪些服务器 学校网络安全手抄报图片 签订网络安全责任书采取的措施 如何用电脑做服务器发布网页
0