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

相关推荐
z落落11 分钟前
C#WinForm控件实战:Panel与单选框动态创建
开发语言·c#
ptc学习者12 分钟前
python 中描述符@property property 大概的样子
开发语言·python
zmzb010314 分钟前
Python课后习题训练记录Day129
开发语言·python
终将老去的穷苦程序员19 分钟前
基于SpringBoot的餐饮管理系统
java·spring boot·后端
心之伊始20 分钟前
Spring AI Tool Calling 实战:让 Java Agent 调用本地 Bean 工具方法
java·spring boot·agent·spring ai·tool calling
张忠琳22 分钟前
【Go 1.26.4】Golang Map 深度解析
开发语言·后端·golang
Vertira23 分钟前
如何对QT开发的软件进行打包[已解决]
开发语言·qt
AI人工智能+电脑小能手25 分钟前
【大白话说Java面试题 第110题】【并发篇】第10题:CAS 存在哪些问题?
java·开发语言·面试
石一峰69933 分钟前
C 语言函数设计模式实战经验
c语言·开发语言·设计模式
sitellla39 分钟前
Pydub:用 Python 处理音频,不写废话
开发语言·python·其他·音视频