easyexcel--多sheet页导入导出

多sheet页导出

核心代码就是下图里面的,使用EasyExcel.writeSheet创建一个sheet,然后用excelWriter写入就行了,很简单

java 复制代码
    @GetMapping("downloadMultiSheet")
    public void downloadMultiSheet(HttpServletResponse response) throws IOException {
        // 导出excel如果失败会返回json
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=" + fileName + ".xlsx");

            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
            // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。
            // sheetNo从0开始
            WriteSheet writeSheet = EasyExcel.writerSheet(0, "用户").head(User.class).build();
            // 去数据库查找数据
            List<User> users = userService.list();
            excelWriter.write(users, writeSheet);
            writeSheet = EasyExcel.writerSheet(1, "部门").head(Department.class).build();
            // 去数据库查找数据
            List<Department> departments = departmentService.list();
            excelWriter.write(departments, writeSheet);
            // 写完要关闭,否则缓冲区数据会丢失
            excelWriter.close();
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = MapUtils.newHashMap();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
    }

多sheet导入

核心代码如图所示,使用EasyExcel.readSheet读取不同的sheet

java 复制代码
    @PostMapping("uploadMultiSheet")
    public Result uploadMultiSheet(@RequestPart MultipartFile file) throws IOException {
        try (ExcelReader excelReader = EasyExcel.read(file.getInputStream()).build()) {
            // 这里为了简单 所以注册了 同样的head 和Listener 自己使用功能必须不同的Listener
            // sheetNo从0开始
            ReadSheet readSheet1 =
                    EasyExcel.readSheet(0).head(User.class).registerReadListener(new PageReadListener<User>(users -> {
                        userService.saveBatch(users);
                    })).build();
            ReadSheet readSheet2 =
                    EasyExcel.readSheet(1).head(Department.class).registerReadListener(new PageReadListener<Department>(departments -> {
                        departmentService.saveBatch(departments);
                    })).build();
            // 这里注意 一定要把sheet1 sheet2 一起传进去,不然有个问题就是03版的excel 会读取多次,浪费性能
            excelReader.read(readSheet1, readSheet2);
        }
        return resultOk();
    }
相关推荐
代码游侠13 小时前
复习笔记——C语言指针
linux·c语言·开发语言·笔记·学习
合作小小程序员小小店13 小时前
web网页开发,在线%餐饮点餐%系统,基于Idea,html,css,jQuery,java,ssm,mysql。
java·前端·数据库·html·intellij-idea·springboot
SimonKing13 小时前
IntelliJ IDEA 2025.2.x的小惊喜和小BUG
java·后端·程序员
曾帅16813 小时前
idea springboot开发编译所见即所得应用不需要重启
java·spring boot·intellij-idea
q***420513 小时前
PHP搭建开发环境(Windows系统)
开发语言·windows·php
星光一影13 小时前
基于PHP+MySQL+Uniapp的上门家政服务系统源码
开发语言·mysql·uni-app·php
Antonio91513 小时前
【Swift】 UIKit:UIGestureRecognizer和UIView Animation
开发语言·ios·swift
q***017714 小时前
Spring Boot 热部署
java·spring boot·后端
Seven9714 小时前
SpringCloud 常见面试题(三)
java
D***t13114 小时前
PHP在API开发中的框架选择
开发语言·php