word模版导出(echarts)ftl

1.引入依赖 

<dependency>
   <groupId>org.freemarker</groupId>
   <artifactId>freemarker</artifactId>
   <version>2.3.31</version>
</dependency>

2. 工具类

package com.jeesite.modules.common.utils;




import freemarker.template.Configuration;
import freemarker.template.Template;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class fltWordUtils {

    private static Configuration configuration = null;
    private static final String templateFolder = fltWordUtils.class.getClassLoader().getResource("").getPath();

    static {
        configuration = new Configuration(Configuration.VERSION_2_3_23);
        configuration.setDefaultEncoding("utf-8");
        try {
            configuration.setDirectoryForTemplateLoading(new File(templateFolder));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private fltWordUtils() {
        throw new AssertionError();
    }
    public static void exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response, Map map, String title, String ftlFile) throws IOException {
        //ftl模板文件
        //这里需要将模板文件放在项目的resources目录下
        configuration.setClassForTemplateLoading(fltWordUtils.class,"/templatesWord");
        //获取模板
        Template template = configuration.getTemplate(ftlFile);
        File file = null;
        InputStream fin = null;
        ServletOutputStream out = null;
        try {
            // 调用工具类的createDoc方法生成Word文档
            file = createDoc(map, template);
            fin = new FileInputStream(file);

            Date date = new Date(); // 创建date对象名
            SimpleDateFormat nowright = new SimpleDateFormat("yyyyMMdd"); // 调用SimpleDateFormat的有参构造且赋值模板
            String format = nowright.format(date); // 调用格式化方法

            String fileName = title + format + ".doc";
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/msword");
            //response.setContentType("application/octet-stream");

            // 设置浏览器以下载的方式处理该文件名
            response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
            out = response.getOutputStream();
            byte[] buffer = new byte[512];  // 缓冲区
            int bytesToRead = -1;
            // 通过循环将读入的Word文件的内容输出到浏览器中
            while ((bytesToRead = fin.read(buffer)) != -1) {

                out.write(buffer, 0, bytesToRead);
            }
        } finally {
            if (fin != null) {
                fin.close();
            }
            if (out != null) {
                out.close();
            }
            if (file != null) {
                file.delete();
            } // 删除临时文件
        }
    }
    //渲染文件
    private static File createDoc(Map<?, ?> dataMap, Template template) {
        String name = "test.doc";
        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;
    }
}

3.制作模版

先把word模版转成后缀xml ,导入idea,改后缀ftl  ,格式化,将数据改成${}形式

ftl 基本语法参考地址

FTL基本语法_ftl语法_如之奈何的博客-CSDN博客

https://www.cnblogs.com/mjtabu/p/14370912.html

4. controller

接收参数 调用工具类

Map<String, Object> map = new HashMap<String, Object>();

把参数放入map

fltWordUtils.exportMillCertificateWord(request, response, map, "文件名称", "template.ftl");

5. 前端调用  form表单提交 传参  不需要返回值

var barBase64Info; 全局变量
barBase64Info = myChart.getDataURL();  渲染echarts 里面 注意 
animation: false, 添加到series,否则获取不到echarts数据

代码参考 java使用ftl模板导出word文档_ftl文件转word_马大厨下饭的博客-CSDN博客

以freemarker导出echarts图表到word文档_spring boot freemarker 导出word 带echarts图形报表_呼延一觅儿的博客-CSDN博客


                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值