前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >搭建单体SpringBoot项目 集成FreeMarker模板引擎

搭建单体SpringBoot项目 集成FreeMarker模板引擎

作者头像
郭顺发 软件开发
发布2023-07-17 18:23:22
3890
发布2023-07-17 18:23:22
举报
文章被收录于专栏:pandacode_cnpandacode_cn

官网 | 中文官网 本文暂不介绍freemarker api,以案例的方式进行介绍。

项目引入freemarker

代码语言:javascript
复制
<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.31</version>
</dependency>

FreeMarker使用doc模板

使用doc模版,将数据引入。

模版示例:

结果示例:

  1. 先准备doc模版文件。将需要放入值的属性加上{},如:{aa}。
  2. 将准备好的doc文件转换成html文件。
  3. 将html文件放入同项目的templates中(如要调整存放文件位置,可以同步把下方工具类里路径也调整了。)
  4. 引入下方工具类。
  5. 使用工具类。

工具类可以根据个人需求进行调整。

代码语言:javascript
复制
// 工具类
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.util.Map;
import java.util.UUID;

public class WordUtil {
    private static Configuration configuration = null;
    static {
        configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        configuration.setClassForTemplateLoading(WordUtil.class, "/templates");
    }

    private WordUtil() {
        throw new AssertionError();
    }

    public static AjaxResult exportMillCertificateWord(Map map, String title, String ftlFile) throws IOException {
        Template freemarkerTemplate = configuration.getTemplate(ftlFile);
        String fileName = UUID.randomUUID().toString() + "_" + title + ".doc";
        String downloadPath = RuoYiConfig.getDownloadPath() + fileName;
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists())
        {
            desc.getParentFile().mkdirs();
        }
        // 调用工具类的createDoc方法生成Word文档
        createDoc(map,freemarkerTemplate,downloadPath);
        return AjaxResult.success(fileName);
    }

    private static File createDoc(Map<?, ?> dataMap, Template template, String name) {
        File f = new File(name);
        Template t = template;
        try {
            // 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
            Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
            t.process(dataMap, w);
            w.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
        return f;
    }
}
代码语言:javascript
复制
// 使用工具类
Map map = new HashMap();
map.put("aa","张三");
map.put("bb","李四");
map.put("cc","王五");
map.put("dd","赵六");
map.put("ee","孙七");
return WordUtil.exportMillCertificateWord(map,"offer","test.html");

参考文档

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • FreeMarker使用doc模板
  • 参考文档
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档