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

相关推荐
NeoGressAI外贸数字化几秒前
外贸建站:新手怎么自建独立站完整指南
java·人工智能
前端 贾公子2 分钟前
第09章:上下文与记忆 (4)
java·服务器·前端
增量星球11 分钟前
一个 Python 脚本 + LLM 怎么替代向量检索?ProjQA 技能架构原理深度解析
开发语言·python·架构·embedding·skill
wjjzhbb15 分钟前
中小团队敏捷转型:Scrum还是看板?
java·maven·scrum
GodSure091424 分钟前
Java单一职责原则SRP详解
java·python·单一职责原则
探数API小喇叭28 分钟前
基站查询 API 怎么用?LAC、CELLID 参数详解与实战】
java·开发语言·api·基站定位
catino29 分钟前
spring-IOC、DI
java·spring·rpc
Mr.Lu ‍30 分钟前
C++开发,使用openCV API对图片进行压缩
开发语言·c++·opencv
z落落32 分钟前
C# WinForm NModbus 串口通信+WinForm Modbus4 串口通信
开发语言·单片机·c#
Mojitocean34 分钟前
Win开发环境配置(持续更新)
开发语言·python