SpringBoot导出word模板并动态渲染数据

导出word模板并动态渲染数据


一、需求介绍

背景:需要导出word模板的时候,有些数据是动态或者图片等不确定因素的时候、根据需求定制好的模板要求填充数据,那么这个时候就需要进行根据word模板进行动态添加数据渲染出 word。

本文主要介绍:

1、SpringBoot导出word模板

2、SpringBoot导出word模板并且渲染动态数据

3、SpringBoot导出word模板包含list数据循环输出

4、SpringBoot导出word模板包含图片信息展示

二、依赖添加

    <!-- JSON-util -->
	<dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.72</version>
    </dependency>
	<!-- framework(主要根据模板引擎导出word使用)  -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>

三、制作模板

制作模板思路:

1、使用word2007或者WPS进行将模板除数据外的格式进行绘制出

2、使用templeat语法将需要动态数据的位置进行替换

3、另存为制作好的word文件为xml(word 2007xml)格式的文件

4、将生成好得到xml文件修改后缀名为.ftl文件并进行代码格式化

5、检查ftl文件中是否有格式错误信息并且设置循环列表的补充、完成图片信息base64的替换

  • 3.1 绘制word模板并填充数据动态语法

在这里插入图片描述

这里图片信息暂放一张图占位(后续会改)

  • 3.2 保存为xml格式

在这里插入图片描述

  • 3.4 修改后缀名

    我们在上一步骤另存为的时候就已经保存为xml格式,在保存的文件夹下找到后修改后缀名为ftl文件。

在这里插入图片描述

  • 3.5 检查并设置循环和图片信息格式

    将ftl文件拖到idea中进行检查格式和替换图片信息

在这里插入图片描述

将base64码替换成占位符

在这里插入图片描述

检查每一处占位符是否都正确,可能再转化后少括号或者 $ 之类的,正常补充添加调整后即可

在这里插入图片描述

完成对循环数组的代码补充

转成xml后找到固定的规律就是

<w:tr> </w:tr> :代表表格中的一行
<w:tc> </w:tc> :代表表格中的一个单元格

那么就需要在动态循环的地方进行添加list循环代码

这个的所有语法就是 framework 语法一样了。

在这里插入图片描述

以上就是模板制作的全过程。下面实现代码部分。

四、后端代码

package com.hebiyusheng.auxililaryreport.controller;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 
 * @date 2022/5/30 - 20:28
 * @Motto "何必余生、及时行乐、Good luck mi"
 * @data
 * @entity
 * @Excprite
 */

@RestController
@RequestMapping("studentWord")
public class ReportController {


    /**
     * 根据模板导出word数据
     *
     * @param response
     * @return
     * @throws Exception
     */
    @GetMapping("word")
    public HttpServletResponse word(HttpServletResponse response) throws Exception {
        /** 初始化配置文件 **/
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        /** 设置编码 **/
        configuration.setDefaultEncoding("utf-8");
        /** 我的ftl文件是放在D盘的**/
        String fileDirectory = "D:\\Spring\\ismy-project\\auxiliary\\auxiliary-report\\src\\main\\resources\\template\\";
        /** 加载文件 **/
        configuration.setDirectoryForTemplateLoading(new File(fileDirectory));
        /** 加载模板 **/
        Template template = configuration.getTemplate("student.ftl");
        List<Map<String, Object>> itemList = new ArrayList<>();//科目
        for (int i = 0; i < 5; i++) {
            Map<String, Object> kemu = new HashMap<>();
            kemu.put("name","科目"+i);
            kemu.put("chengji",90+i);
            itemList.add(kemu);
        }
        Map<String, Object> mapList = new HashMap<>();//参数
        mapList.put("serialNo", "20220629");//编号
        mapList.put("name", "XX一中");//姓名
        mapList.put("pic", getImgFileToBase64("C:\\Users\\hebiy\\Desktop\\XXXX.jpg"));//图片
        mapList.put("itemList", itemList);//科目
        mapList.put("toloclass", "700");//总分

        //* 指定输出word文件的路径 *
        String outFilePath = "D:\\Spring\\ismy-project\\auxiliary\\auxiliary-report\\src\\main\\resources\\template\\" +(System.currentTimeMillis()) + ".doc";
        File docFile = new File(outFilePath);
        //创建输出流
        FileOutputStream fos = new FileOutputStream(docFile);
        //创建缓冲器
        Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"), 10240);
        //导出word
        template.process(mapList, out);
        /*文件下载*/
        File file = new File(outFilePath);
        // 取得文件名。
        String filename = file.getName();
        //下载
        InputStream fis = new BufferedInputStream(new FileInputStream(outFilePath));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        // 清空response
        response.reset();
        // 设置response的Header
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
        response.addHeader("Content-Length", "" + file.length());
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream");
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
        //关闭缓冲器
        if (out != null) {
            out.close();
        }
        System.out.println("下载完成......");
        return null;
    }


    /**
     * 读取图片转成base64
     * @param imgFile
     * @return
     */
    public static String getImgFileToBase64(String imgFile) {
        //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        InputStream inputStream = null;
        byte[] buffer = null;
        //读取图片字节数组
        try {
            inputStream = new FileInputStream(imgFile);
            int count = 0;
            while (count == 0) {
                count = inputStream.available();
            }
            buffer = new byte[count];
            inputStream.read(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    // 关闭inputStream流
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 对字节数组Base64编码
        return new BASE64Encoder().encode(buffer);
    }

}

五、效果展示

在这里插入图片描述
说明:代码补充可自行优化。数据部分可优化为数据库读取数据等,针对部署linux问题将读取配置文件和图片信息替换成linux支持格式即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值