• springboot 导出word模板


    一、安装依赖

    
                com.deepoove
                poi-tl
                1.12.1
            
    
    • 1
    • 2
    • 3
    • 4
    • 5

    二、定义工具类

    package com.example.springbootmp.utils;
    
    import com.deepoove.poi.XWPFTemplate;
    import com.deepoove.poi.config.Configure;
    
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.Map;
    
    public class WordUtil {
        /**
         * 根据模板填充内容生成word,并下载
         *
         * @param templatePath word模板文件路径
         * @param paramMap     替换的参数集合
         */
        public static void downloadWord(OutputStream out, InputStream templatePath, Map paramMap, Configure config) throws Exception {
    
    /*
    
            // 生成本地文件
    
            Long time = System.currentTimeMillis();
            // 生成的word格式
            String formatSuffix = ".docx";
            // 拼接后的文件名
            String fileName = time + formatSuffix;
    
            //设置生成的文件存放路径,可以存放在你想要指定的路径里面
            String rootPath = "D:/tmp/" + File.separator + "file/word/";
    
    
            String filePath = rootPath + fileName;
            File newFile = new File(filePath);
            //判断目标文件所在目录是否存在
            if (!newFile.getParentFile().exists()) {
                //如果目标文件所在的目录不存在,则创建父目录
                newFile.getParentFile().mkdirs();
            }
    */
    
    
            // 读取模板templatePath并将paramMap的内容填充进模板,即编辑模板(compile)+渲染数据(render)
            XWPFTemplate template = XWPFTemplate.compile(templatePath, config).render(
                    paramMap);
    
    
            // 生成本地文件
            //out = new FileOutputStream(filePath);//输出路径(下载到指定路径)
            // 将填充之后的模板写入filePath
            //将template写到OutputStream中
    
            template.write(out);
            out.flush();
            out.close();
            template.close();
    
        }
    
        /**
         * 远程文件url 转为输入流
         * @param urlOrPath 远程文件url
         * @return
         * @throws Exception
         */
        public static InputStream byteByUrl(String urlOrPath) throws Exception {
            URL url = new URL(urlOrPath);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(3 * 1000);
            //防止屏蔽程序抓取而返回403错误
            //conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //得到输入流
            return conn.getInputStream();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    三、在resource文件夹下定义模板,如template.docx
    在这里插入图片描述
    在这里插入图片描述

    四、测试控制器代码

    package com.example.springbootmp.controller;
    
    import com.deepoove.poi.config.Configure;
    import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
    import com.example.springbootmp.utils.WordUtil;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.servlet.http.HttpServletResponse;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @RestController
    public class fileController {
    
        @GetMapping("/word-download")
        public void word(HttpServletResponse response) throws Exception {
    
            response.setContentType("application/octet-stream");
            //设置文件名称
            response.setHeader("Content-Disposition", "attachment;filename=" + System.currentTimeMillis() + ".docx");
    
            //获取 resources 目录 本地&打Jar包都可用 路径前不需要加/
            //InputStream inputStream = getClass().getClassLoader().getResourceAsStream("word/template.docx");
            //路径前需要加/  具体区别请看https://www.cnblogs.com/geek233/p/15817853.html
            InputStream inputStream = getClass().getResourceAsStream("/word/template.docx");
            //获取远程文件
            //InputStream inputStream = WordUtil.byteByUrl("https://blog.52ipc.top/word/template.docx");
    
    
            // 创建用于插入数据的Map  实际开发要放在service层
            Map paramMap = new HashMap<>(18);
            paramMap.put("title", "Hi, Word模板");
            paramMap.put("uname", "小蜜蜂~");
            List> voList = new ArrayList<>();
    
            for (int i = 0; i < 10; i++) {
                Map vo = new HashMap<>();
                vo.put("areaId" , String.valueOf(i));
                vo.put("name" ,  "北京"+ i);
                voList.add(vo);
            }
    
    
            paramMap.put("area", voList);
    
            // 循环行表渲染策略
            LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
            // 插件绑定
            Configure config = Configure.builder()
                    //area 变量名
                    //policy 策略
                    .bind("area", policy)
                    .build();
    
            WordUtil.downloadWord(response.getOutputStream(), inputStream, paramMap, config);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
  • 相关阅读:
    hadoop程序实例,从新建到上传Hadoop集群跑成功
    Kubeadm - K8S1.20 - 高可用集群部署(博客)
    win10搭建ESP8266_RTOS_SDK编译环境
    toString和valueOf
    同相放大电路电阻热噪声分析的一些思考
    Leetcode 49.字母异位词分组
    完美洗牌问题
    从官方文档看Redis
    PAT 乙级1090危险品装箱
    【区分vue2和vue3下的element UI Message 消息提示组件,分别详细介绍属性,事件,方法如何使用,并举例】
  • 原文地址:https://blog.csdn.net/weixin_41463944/article/details/133822478