SpringBoot-Freemarker导出word

使用word制作模版文件

使用Freemarker插值语法在需要替换的地方设置变量

把模版文件转换为xml文件保存
把模版文件复制到项目中并修改后缀.ftl
pom引入freemarker
xml 复制代码
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
编写一个工具类 WordUtils
java 复制代码
package com.gsafety.bg.emis.event.service.utils;

import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.experimental.UtilityClass;

/**
 * word生成工具类
 *
 * @author :王建
 * @since :2023-05-12 15:01
 */
@UtilityClass
public class WordUtils {

    /**
     * 生成 word 文档方法
     *
     * @param dataMap      要填充的数据
     * @param templateName 模版名称
     * @param fileName     要输出的文件路径
     * @throws Exception 抛出的异常
     */
    public static void generateWord(Map<String, Object> dataMap, String templateName, String fileName, HttpServletResponse response) throws Exception {
        // 设置FreeMarker的版本和编码格式
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
        configuration.setDefaultEncoding("UTF-8");
        configuration.setURLEscapingCharset("UTF-8");
        configuration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);

        // 此处把模版文件都放在 resources 下的 templates 中
        configuration.setClassForTemplateLoading(WordUtils.class, "/templates");

        // 设置FreeMarker生成Word文档所需要的模板
        Template tem = configuration.getTemplate(templateName, "UTF-8");

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Writer out = new OutputStreamWriter(baos);
        tem.process(dataMap, out);
        byte[] bytes = baos.toByteArray();
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/x-msdownload");
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
        response.setHeader("FileName", fileName);

        response.setContentLength(bytes.length);

        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    }
}
调用生成word文件
java 复制代码
 AiExportReportTemplateReq templateParams = new AiExportReportTemplateReq()
              .setCurrentTime(DateUtil.format(new Date(), "yyyy年MM月dd日"))
              .setName("xxxxxx");

WordUtils.generateWord(BeanUtil.beanToMap(templateParams),"你好.ftl", "你好-01.docx", response);
相关推荐
仙俊红4 小时前
Spring Boot `@Configuration` 与 `@Component` 笔记
java·spring boot·笔记
计算机学姐7 小时前
基于SpringBoot的社团管理系统【2026最新】
java·vue.js·spring boot·后端·mysql·spring·mybatis
CodeLongBear8 小时前
Spring Boot 与 Spring MVC 的区别与联系:从本质到实践
spring boot·spring·mvc
THMAIL10 小时前
深度剖析Spring AI源码(七):化繁为简,Spring Boot自动配置的实现之秘
人工智能·spring boot·spring
一枚小小程序员哈13 小时前
基于Android的车位预售预租APP/基于Android的车位租赁系统APP/基于Android的车位管理系统APP
android·spring boot·后端·struts·spring·java-ee·maven
用户307429716715813 小时前
Spring AI实战:基于ElevenLabs 实现文本转语音的实时音频流
java·spring boot·ai编程
Java水解15 小时前
Spring Boot 事务详解
spring boot·后端
lssjzmn15 小时前
MyBatis Plus 与 MyBatis的PK:Spring Boot 下的详解、选型与实战
spring boot·mybatis
Tom·Ge15 小时前
Spring AI 入门指南:三步将AI集成到Spring Boot应用
人工智能·spring boot·spring
tingting011916 小时前
Spring Boot 外部配置指定不生效的原因与解决
java·spring boot·后端