Springboot 导出word,动态填充表格数据

简介: Springboot 导出word,动态填充表格数据

背景

本文将给大家带来如何导入数据到word文档中,固定传值和动态制作表格传值等。


依赖:

        <!-- word导出 -->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.7.3</version>
        </dependency>
        <!--  上面需要的依赖-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.9.1</version>
        </dependency>

创建word模板:

说明:{{ }} 这个是用来做占位符的,后续代码中替换值。

如果导出的数据是个集合list,那么在你的表格标题的第一个单元格输入{{list}}占位符,下面空白单位元是用来占位集合中元素key的,占位符是:[]  如图

代码实例:

public void getStandingBook(String activityId, HttpServletResponse response, HttpServletRequest request) {
        try {
            //获取文件流
            InputStream stream = getClass().getClassLoader().getResourceAsStream("static/word.docx");
            //获取临时文件
            File file = new File("static/word.docx");
            //将读取到的类容存储到临时文件中,后面就可以用这个临时文件访问了
            FileUtils.copyInputStreamToFile(stream, file);
            //这个时候再去获取证书的文件路径 就可以正常获取了
            String filePath = file.getAbsolutePath();
 
            QueryWrapper<TActivity> tActivityQueryWrapper = new QueryWrapper<>();
            tActivityQueryWrapper.eq("uuid", activityId);
            tActivityQueryWrapper.eq("status", "0");
            TActivity tActivity = tActivityService.getOne(tActivityQueryWrapper);
            if (tActivity == null) {
                throw new CustomException("导出失败,活动不存在!");
            }
            List<Map> detailList = new ArrayList<>();
            QueryWrapper<TActivityBudget> budgetQueryWrapper = new QueryWrapper<>();
            budgetQueryWrapper.eq("activity_id", tActivity.getUuid());
            budgetQueryWrapper.eq("status", "0");
            List<TActivityBudget> budgetList = activityBudgetService.list(budgetQueryWrapper);
            if (!CollectionUtils.isEmpty(budgetList)) {
                budgetList.forEach(x -> {
                    Map map = new HashMap();
                    map.put("feeName", x.getFeeName());
                    map.put("purpose", x.getPurpose());
                    map.put("price", x.getPrice());
                    map.put("number", x.getNumber());
                    map.put("totalPrice", x.getTotalPrice());
                    map.put("remarks", x.getRemarks());
                    detailList.add(map);
                });
            }
            QueryWrapper<TActivityProfessional> professionalQueryWrapper = new QueryWrapper<>();
            professionalQueryWrapper.eq("activity_id", tActivity.getUuid());
            professionalQueryWrapper.eq("status", "0");
            List<TActivityProfessional> professionalList = activityProfessionalService.list(professionalQueryWrapper);
            if (!CollectionUtils.isEmpty(professionalList)) {
                professionalList.forEach(x -> {
                    Map map = new HashMap();
                    map.put("feeName", x.getPersonName());
                    map.put("purpose", "社会工作者/专业老师补贴");
                    map.put("price", x.getPrice());
                    map.put("number", "1");
                    map.put("totalPrice", x.getPrice());
                    map.put("remarks", x.getRemarks());
                    detailList.add(map);
                });
            }
            if (detailList.size() == 0) {
                Map map = new HashMap();
                map.put("feeName", "");
                map.put("purpose", "");
                map.put("price", "");
                map.put("number", "");
                map.put("totalPrice", "");
                map.put("remarks", "");
                detailList.add(map);
            }
            //计算合计
            double totalNum = 0d;
            if (!CollectionUtils.isEmpty(detailList)) {
                totalNum = detailList.stream().mapToDouble(x -> Double.valueOf(x.get("totalPrice").toString())).sum();
            }
            log.info("合计:{}", totalNum);
            //处理图片
            String[] arr = tActivity.getActivityReviewUrl().split(",");
            List<Map> pic = new ArrayList<>();
            if (arr != null && arr.length > 0) {
                for (String str : arr) {
                    File ff = aliyunCloudStorageService.getFileByUrl(str);
                    BufferedImage bi = null;
                    try {
                        bi = ImageIO.read(ff);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    int width = bi.getWidth(); // 像素
                    if (width > 500) {
                        width = 500;
                    }
                    int height = bi.getHeight(); // 像素
                    if (height > 500) {
                        height = 500;
                    }
                    Map picMap = new HashMap();
                    picMap.put("urlImg", Pictures.ofUrl(str, PictureType.JPEG).size(width, height).create());
                    pic.add(picMap);
                    System.out.println(str);
                    bi.flush();
                }
            }
            //渲染表格
            HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
            Configure config = Configure.newBuilder().bind("detailList", policy).build();
            double finalTotalNum = totalNum;
            XWPFTemplate template = XWPFTemplate.compile(filePath, config).render(
                    new HashMap<String, Object>() {{
                        put("activityName", tActivity.getActivityName());
                        put("activityBackground", tActivity.getActivityBackground());
                        put("startTime", DateUtils.getStringDate(tActivity.getStartTime()));
                        put("endTime", DateUtils.getStringDate(tActivity.getEndTime()));
                        put("activityContent", tActivity.getActivityContent());
                        put("distinctidName", tActivity.getDistinctidName());
                        put("activityLimit", tActivity.getActivityLimit());
                        put("organizer", tActivity.getOrganizer());
                        put("nowDate", DateUtils.getStringDate(new Date()));
                        put("totalNum", finalTotalNum);
                        put("activityAddress", tActivity.getActivityAddress());
                        put("activityReviewWeb", tActivity.getActivityReviewWeb());
                        put("detailList", detailList);
                        put("picList", pic);
                    }}
            );
            //=================生成文件保存在本地D盘某目录下=================
            //String temDir="D:/mimi/"+File.separator+"file/word/"; ;//生成临时文件存放地址
            //生成文件名
            Long time = new Date().getTime();
            // 生成的word格式
            String fileName = tActivity.getActivityName() + time + ".docx";
            System.out.println("文件名:" + fileName);
            //=================生成word到设置浏览默认下载地址=================
            //解决文件下载名称变为 ____下划线的BUG
            //针对IE或者以IE为内核的浏览器:
            String userAgent = request.getHeader("User-Agent");
            if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
                fileName = java.net.URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
            } else {
                //非IE浏览器:
                fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
                // 设置强制下载不打开
                response.setContentType("application/force-download");
                // 设置文件名
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
                response.setCharacterEncoding("UTF-8");
                OutputStream out = response.getOutputStream();
                template.write(out);
                out.flush();
                out.close();
                template.close();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

重点代码在这里:

 //渲染表格
            HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
            Configure config = Configure.newBuilder().bind("detailList", policy).build();
            double finalTotalNum = totalNum;
            XWPFTemplate template = XWPFTemplate.compile(filePath, config).render(
                    new HashMap<String, Object>() {{
                        put("activityName", tActivity.getActivityName());
                        put("activityBackground", tActivity.getActivityBackground());
                        put("startTime", DateUtils.getStringDate(tActivity.getStartTime()));
                        put("endTime", DateUtils.getStringDate(tActivity.getEndTime()));
                        put("activityContent", tActivity.getActivityContent());
                        put("distinctidName", tActivity.getDistinctidName());
                        put("activityLimit", tActivity.getActivityLimit());
                        put("organizer", tActivity.getOrganizer());
                        put("nowDate", DateUtils.getStringDate(new Date()));
                        put("totalNum", finalTotalNum);
                        put("activityAddress", tActivity.getActivityAddress());
                        put("activityReviewWeb", tActivity.getActivityReviewWeb());
                        put("detailList", detailList);
                        put("picList", pic);
                    }}
            );

关于向word文档中插入多张图片:

代码:

 //处理图片
            String[] arr = tActivity.getActivityReviewUrl().split(",");
            List<Map> pic = new ArrayList<>();
            if (arr != null && arr.length > 0) {
                for (String str : arr) {
                    //File ff = aliyunCloudStorageService.getFileByUrl(str);
                    BufferedImage bi = null;
                    try {
                        bi = ImageIO.read(ff);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //int width = bi.getWidth(); // 像素
                    //if (width > 500) {
                    //    width = 500;
                    //}
                    //int height = bi.getHeight(); // 像素
                    //if (height > 500) {
                    //    height = 500;
                    //}
                    Map picMap = new HashMap();
                    picMap.put("urlImg", Pictures.ofUrl(str, PictureType.JPEG).size(width, height).create());
                    pic.add(picMap);
                    System.out.println(str);
                    bi.flush();
                }
            }

文档中插入多张图片的占位符是:{{?picList}}{{@urlImg}}{{/picList}}    picList是你集合key  ,urlImg是集合内元素

导出结果:

相关文章
|
9月前
|
JSON Java 数据格式
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——封装统一返回的数据结构
本文介绍了在Spring Boot中封装统一返回的数据结构的方法。通过定义一个泛型类`JsonResult&lt;T&gt;`,包含数据、状态码和提示信息三个属性,满足不同场景下的JSON返回需求。例如,无数据返回时可设置默认状态码&quot;0&quot;和消息&quot;操作成功!&quot;,有数据返回时也可自定义状态码和消息。同时,文章展示了如何在Controller中使用该结构,通过具体示例(如用户信息、列表和Map)说明其灵活性与便捷性。最后总结了Spring Boot中JSON数据返回的配置与实际项目中的应用技巧。
730 0
|
9月前
|
JSON Java fastjson
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——使用 fastJson 处理 null
本文介绍如何使用 fastJson 处理 null 值。与 Jackson 不同,fastJson 需要通过继承 `WebMvcConfigurationSupport` 类并覆盖 `configureMessageConverters` 方法来配置 null 值的处理方式。例如,可将 String 类型的 null 转为 &quot;&quot;,Number 类型的 null 转为 0,避免循环引用等。代码示例展示了具体实现步骤,包括引入相关依赖、设置序列化特性及解决中文乱码问题。
471 0
|
9月前
|
JSON Java fastjson
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——Spring Boot 默认对Json的处理
本文介绍了在Spring Boot中返回Json数据的方法及数据封装技巧。通过使用`@RestController`注解,可以轻松实现接口返回Json格式的数据,默认使用的Json解析框架是Jackson。文章详细讲解了如何处理不同数据类型(如类对象、List、Map)的Json转换,并提供了自定义配置以应对null值问题。此外,还对比了Jackson与阿里巴巴FastJson的特点,以及如何在项目中引入和配置FastJson,解决null值转换和中文乱码等问题。
1387 0
|
5月前
|
JSON Java 数据格式
Spring Boot返回Json数据及数据封装
在Spring Boot中,接口间及前后端的数据传输通常使用JSON格式。通过@RestController注解,可轻松实现Controller返回JSON数据。该注解是Spring Boot新增的组合注解,结合了@Controller和@ResponseBody的功能,默认将返回值转换为JSON格式。Spring Boot底层默认采用Jackson作为JSON解析框架,并通过spring-boot-starter-json依赖集成了相关库,包括jackson-databind、jackson-datatype-jdk8等常用模块,简化了开发者对依赖的手动管理。
568 3
|
11月前
|
前端开发 Java API
SpringBoot整合Flowable【06】- 查询历史数据
本文介绍了Flowable工作流引擎中历史数据的查询与管理。首先回顾了流程变量的应用场景及其局限性,引出表单在灵活定制流程中的重要性。接着详细讲解了如何通过Flowable的历史服务API查询用户的历史绩效数据,包括启动流程、执行任务和查询历史记录的具体步骤,并展示了如何将查询结果封装为更易理解的对象返回。最后总结了Flowable提供的丰富API及其灵活性,为后续学习驳回功能做了铺垫。
887 0
SpringBoot整合Flowable【06】- 查询历史数据
|
9月前
|
前端开发 Cloud Native Java
Java||Springboot读取本地目录的文件和文件结构,读取服务器文档目录数据供前端渲染的API实现
博客不应该只有代码和解决方案,重点应该在于给出解决方案的同时分享思维模式,只有思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
Java||Springboot读取本地目录的文件和文件结构,读取服务器文档目录数据供前端渲染的API实现
|
10月前
|
Java 关系型数据库 MySQL
SpringBoot 通过集成 Flink CDC 来实时追踪 MySql 数据变动
通过详细的步骤和示例代码,您可以在 SpringBoot 项目中成功集成 Flink CDC,并实时追踪 MySQL 数据库的变动。
2515 45
|
11月前
|
存储 前端开发 Java
SpringBoot整合Flowable【05】- 使用流程变量传递业务数据
本文介绍了如何使用Flowable的流程变量来管理绩效流程中的自定义数据。首先回顾了之前的简单绩效流程,指出现有流程缺乏分数输入和保存步骤。接着详细解释了流程变量的定义、分类(运行时变量和历史变量)及类型。通过具体代码示例展示了如何在绩效流程中插入全局和局部流程变量,实现各节点打分并维护分数的功能。最后总结了流程变量的使用场景及其在实际业务中的灵活性,并承诺将持续更新Flowable系列文章,帮助读者更好地理解和应用Flowable。 简要来说,本文通过实例讲解了如何利用Flowable的流程变量功能优化绩效评估流程,确保每个环节都能记录和更新分数,同时提供了全局和局部变量的对比和使用方法。
1023 0
SpringBoot整合Flowable【05】- 使用流程变量传递业务数据
|
SQL 前端开发 关系型数据库
SpringBoot使用mysql查询昨天、今天、过去一周、过去半年、过去一年数据
SpringBoot使用mysql查询昨天、今天、过去一周、过去半年、过去一年数据
390 9
|
存储 easyexcel Java
SpringBoot+EasyExcel轻松实现300万数据快速导出!
本文介绍了在项目开发中使用Apache POI进行数据导入导出的常见问题及解决方案。首先比较了HSSFWorkbook、XSSFWorkbook和SXSSFWorkbook三种传统POI版本的优缺点,然后根据数据量大小推荐了合适的使用场景。接着重点介绍了如何使用EasyExcel处理超百万数据的导入导出,包括分批查询、分批写入Excel、分批插入数据库等技术细节。通过测试,300万数据的导出用时约2分15秒,导入用时约91秒,展示了高效的数据处理能力。最后总结了公司现有做法的不足,并提出了改进方向。