java用freemarker导出word

freemarker导出word

第一步、将word转换为xml格式

第二步、将转换后的xml文件修改后缀为ftl后复制到项目 resources 目录下(可以自己新建一个文件夹放在文件夹中)

第三步、格式化xml代码(如果问价太大可能会无法格式化)这时候需要在idea中进行设置

idea.max.intellisense.filesize=10000

idea.max.content.load.filesize=10000

第四步、ftl文件值的替换

java 复制代码
${monthLowerALL!} 表示替换单个值

遍历数组
<#list userList as user>
    编号:${user.id} 姓名:${user.username} ${user.age}<br/>
</#list>

第五步、工具类

java 复制代码
@Slf4j
public class FreemarkerUtil {

    /**
     * 使用 Freemarker 生成 Word 文件
     *
     * @param templateName 模板文件路径名称
     * @param fileName     生成的文件路径以及名称
     * @param dataModel    填充的数据对象
     */
    public static void exportWord(String templateName, String fileName, Map<String, Object> dataModel) {
        generateFile(templateName, fileName, dataModel);
    }

    /**
     * 使用 Freemarker 生成指定文件返回流
     *
     * @param templateName 模板文件路径名称
     * @param fileName     生成的文件名称
     * @param dataModel    填充的数据对象
     */
    private static void generateFile(String templateName, String fileName, Map<String, Object> dataModel) {
        try {
            // 1、创建配置对象
            Configuration config = new Configuration(Configuration.VERSION_2_3_30);
            config.setDefaultEncoding("utf-8");
            //这里 templates 为你项目中resources目录下templates文件夹,相当于会拼接在你的 templateName参数前面
            config.setClassForTemplateLoading(FreemarkerUtil.class, "/templates");
            // 2、获取模板文件
            Template template = config.getTemplate(templateName, "utf-8");
            HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            response.setContentType("application/msword;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            try {
                response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".docx");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            template.process(dataModel, response.getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

###第六步、调用

java 复制代码
 Map<String, Object> dataMap = Maps.newConcurrentMap();
        dataMap.put("totalIncomeAll", "123456789++");
        dataMap.put("monthLowerALL", "1960");
        // 执行导出
        FreemarkerUtil.exportWord("templates路径下对应的模板名称.ftl", "导出的模板名称", dataMap);

POM

java 复制代码
 <!-- 文档模板操作依赖 -->
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.core</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.document</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.template</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
            <version>2.0.2</version>
        </dependency>
相关推荐
步行cgn17 小时前
Spring 基于 XML 的自动装配:byName 详解
java·后端·spring
大帅点兵17 小时前
Apache Ant 1.9.9 完整讲解
java
LuTshoes18 小时前
spring ai 实战 手搓 PlaneExecuteAgent
java·人工智能·spring·ai
这料鬼有毒18 小时前
二刷hot100-73.矩阵置零
java
caoerzhong18 小时前
JeeWMS 开源仓库管理系统二次开发与接口对接实战:Java WMS 如何与 ERP、MES 和自动化设备打通
java·开源
长谷深风11118 小时前
评测AI Agent:三种裁判各司其职
java·大数据·开发语言·人工智能·ai agent
边境悍匪18 小时前
蜗牛学苑 Java 智能体学习 Day42|项目周开发技术汇总 1 思维导图复盘
java·开发语言·spring boot·学习·spring
槑有烦恼19 小时前
Java 字符串 & List集合 高频易错真题复盘(含全部错题+修正)
java
语戚19 小时前
力扣 1621. 大小为K的不重叠线段的数目:动态规划(Java 实现)
java·算法·leetcode·动态规划·力扣·dp
君顾119 小时前
本地托管机构管理源码实战:需求拆解、数据建模与多端部署
java·开发语言·托管