word模版导出(占位符方式)

java 复制代码
import freemarker.template.Configuration;
import freemarker.template.Template; 
import freemarker.template.TemplateException; 
import freemarker.template.Version; 
import jakarta.servlet.ServletOutputStream; 
import jakarta.servlet.http.HttpServletRequest; 
import jakarta.servlet.http.HttpServletResponse; 
import java.io.*; import java.net.URLEncoder; 
import java.nio.charset.StandardCharsets; 
import java.util.Map;
 public class WordUtils {
    /**
     * 根据模板生成word
     *
     * @param datamap       模板要填充的 数据
     * @param tempalateName 模板名称
     * @param fileName      要生成的 word名称 后缀仅支持.doc
     */
    public static void generateWordToFile(Map<String, Object> datamap, String tempalateName, String fileName) throws Exception {
        OutputStreamWriter outputStreamWriter = null;
        FileOutputStream fileOutputStream = null;
        try {
            Configuration configuration = new Configuration(new Version("2.3.28"));
            configuration.setDefaultEncoding("UTF-8");
            configuration.setClassForTemplateLoading(WordUtils.class, "/template");
            Template template = configuration.getTemplate(tempalateName, "UTF-8");
            fileOutputStream = new FileOutputStream(new File(fileName));
            outputStreamWriter = new OutputStreamWriter(new FileOutputStream(new File(fileName)), StandardCharsets.UTF_8);
            Writer out = new BufferedWriter(outputStreamWriter);
            template.process(datamap, out);
            out.flush();
            out.close();
            } catch (Exception e) { 
                throw new RuntimeException(e); 
            } finally { 
                    if (fileOutputStream != null) { 
                        fileOutputStream.close(); 
                    } if (outputStreamWriter != null) {
                         outputStreamWriter.close(); 
                    } 
            } 
    }
/**
     * 根据模板生成文件流返回前端
     *
     * @param datamap       模板要填充的 数据
     * @param tempalateName 模板名称
     * @param fileName      要生成的 word名称
     */
    public static void generateWordToStream(Map<String, Object> datamap, String tempalateName, String fileName, HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("utf-8");
        response.setContentType("applicaiton/msword;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName+"doc");
        Configuration configuration = new Configuration(new Version("2.3.28"));
        configuration.setDefaultEncoding("UTF-8");
        configuration.setClassForTemplateLoading(WordUtils.class, "/template");
        Template template = configuration.getTemplate(tempalateName, "UTF-8");
        Writer out = response.getWriter();
        template.process(datamap, out);
        out.flush();
        out.close();
    }
}

word模板占位符:${aa}
操作流程:首先创建docx/doc 的word模板,接着将word另存为xml,最后将xml文件重命名为xx.ftl

相关推荐
Wang's Blog5 分钟前
Java 中间件之 RabbitMQ 快速入门: MQ 常见技术选型对比
java·中间件·java-rabbitmq
蜗牛互联网6 分钟前
Java Agent 工具调用的 allowlist、参数校验与调用预算
java·开发语言·人工智能·后端·oracle
大文说跨境6 分钟前
多账号环境隔离方案技术选型:指纹浏览器、VPS 与云手机的三种架构对比
java·开发语言·前端
chenbingjie_c9 分钟前
手撕 C++ priority_queue:二叉堆 + 模板仿函数 + 模板特化(保姆级逐函数拆解)
开发语言·c++
骉马代驾15 分钟前
代驾系统长连接实战(二):心跳、断线重连与消息补偿的完整实现
java
ttwuai18 分钟前
Go后台管理系统开源项目有哪些?如何按交付形态筛掉不合适的仓库
开发语言·golang·开源
程序员Sunday21 分钟前
Spring @Transactional 没回滚?按代理调用、异常和传播行为排查
java·后端·spring
冰暮流星30 分钟前
sql之is null 运算符
java·数据库·sql
Rain的Java大神之路31 分钟前
🔥13年Java老兵转型AI Agent:90%的人挂在同一个坑,根本不用学Python!(万字实战复盘,建议收藏)
java·后端·面试
yuniko-n31 分钟前
【Java】关于容器选型:Deque、PriorityQueue、LinkedList
java·开发语言