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);
相关推荐
_码农121381 小时前
spring boot + mybatis + mysql 只有一个实体类的demo
spring boot·mysql·mybatis
_oP_i1 小时前
word中rtf格式介绍
word
c_zyer2 小时前
FreeSWITCH与Java交互实战:从EslEvent解析到Spring Boot生态整合的全指南
spring boot·netty·freeswitch·eslevent
郝学胜-神的一滴2 小时前
Spring Boot Actuator 保姆级教程
java·开发语言·spring boot·后端·程序人生
斜月3 小时前
Springboot 项目加解密的那些事儿
spring boot·后端
草莓爱芒果3 小时前
Spring Boot中使用Bouncy Castle实现SM2国密算法(与前端JS加密交互)
java·spring boot·算法
汤姆yu5 小时前
基于springboot的快递分拣管理系统
java·spring boot·后端
2301_787725655 小时前
Word 转 HTML API 接口
word
E_ICEBLUE5 小时前
Python 操作 Word 文档:主流库对比与选择指南
开发语言·经验分享·python·word·办公自动化