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

相关推荐
萌动的小火苗15 分钟前
1、python基础面试题
java·开发语言·python
运维行者_25 分钟前
网络监控与ITSM集成:从告警到工单,实现运维自动化闭环
开发语言·网络·分布式·后端·架构·flask·php
麻雀聊技术1 小时前
一重启 AI 就失忆?用 Spring AI + PostgreSQL 3步搞定“长期记忆”持久化(附完整代码)
java·spring·openai
小叮当爱咖啡1 小时前
Day3.参数+Prompt三板斧
开发语言·python·prompt
菜冻鱼1 小时前
Python-pandas-索引与筛选
开发语言·笔记·python·numpy·pandas·学习方法
码农进化录1 小时前
Java 程序员的 AI 进化论 | LangChain4j 入门,Java 开发者的 AI 应用框架
java·spring boot·openai
番茄炒鸡蛋加糖1 小时前
中级核心技术1--MySQL/Java 并发
java·数据库·mysql
鹿角片ljp1 小时前
Java面试复盘(二):String不可变、字符串常量池与字符串拼接
开发语言·python
he___H2 小时前
Spring-Configur注解
java·spring
SunnyDays10112 小时前
Java 如何为 PDF 添加、修改和删除书签
java·pdf