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

相关推荐
学长毕设6 小时前
基于SpringBoot的实验室资产管理系统
java·spring boot·后端
C^h6 小时前
c++ string
开发语言·c++
raindayinrain6 小时前
c++高性能--原子操作,无锁编程,操作顺序
开发语言·c++·性能优化
学逆向的6 小时前
C++ 合并命名空间
开发语言·c++·网络安全
Zhao-Jichao6 小时前
VS2026 打包 C++ 程序生成 EXE,借助 Installer Project 扩展工具
开发语言·c++
zzu123zsw7 小时前
从臃肿的模板调用到声明式契约:深入 UniHttp 动态代理与多渠道生命周期扩展
java·spring boot·microsoft·unihttp
学逆向的7 小时前
C 局部静态变量
c语言·开发语言·网络安全
大都督会赢的8 小时前
C++第一节课
开发语言·c++
“AI国潮设计-小江”8 小时前
【AIGC实战】Python+Stable Diffusion打造“英歌舞财神”IP,解锁“马到成功”等爆款情绪营销文案设计
开发语言·人工智能·python·prompt·aigc
源码集结号8 小时前
云诊所门诊系统源码,基于SpringBoot+Vue.js架构,支持多租户设计与Docker容器化部署
java·spring boot·源码·医院信息系统·门诊系统·诊所系统