• SpringBoot+Easyexcel读取多sheet支持xls,xlsx版本的excel


    Java操作excel文件一般有两种方式easyExcel和poi,这里使用的是更简单的easyExcel方式。

    EasyExcel是阿里巴巴开源的一个excel处理框架,以使用简单、节省内存著称。

    EasyExcel是一个基于Java的、快速、简洁、解决大文件内存溢出的Excel处理工具。
    他能让你在不用考虑性能、内存的等因素的情况下,快速完成Excel的读、写等功能。
    EasyExcel基于POI进行封装优化,降低内存使用,再大的excel也不会出现内存溢出,让使用更加简单方便。
    
    • 1
    • 2
    • 3

    EasyExcel操作excel

    导入依赖

    EasyExcel需引入如下依赖:

    
    
        cn.afterturn
        easypoi-spring-boot-starter
        4.3.0
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    示例中还需引入其他依赖:

    
    
        org.projectlombok
        lombok
        1.18.20
        provided
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    多sheet工作表excel导入

    先来定义一个导入实体类(两个不同的sheet,可以定义两个不同的实体类)。

    package com.ypk.swagger.model;
    
    import cn.afterturn.easypoi.excel.annotation.Excel;
    import lombok.Data;
    
    import java.io.Serializable;
    
    /**
     * @ClassName 仪器 * @Description TODO
     * @Author lgn
     * @Date 17:41 2022/9/16
     * @Version 1.0
     **/
    @Data
    public class ImportInstrument implements Serializable {
        private static final long serialVersionUID = 1L;
        /**
         * @Excel 作用在一个filed上面,对列的描述
         * @param name 列名
         * @param orderNum 下标,从0开始。
         */
        @Excel(name = "设备名称", orderNum = "0",width = 10.0)
        private String name;
        @Excel(name = "类型", orderNum = "1",width = 10.0)
        private Integer type;
    }
    
    
    • 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
    package com.ypk.swagger.model;
    
    import cn.afterturn.easypoi.excel.annotation.Excel;
    import lombok.Data;
    
    import java.io.Serializable;
    
    /**
     * @ClassName ImportServer * @Description TODO
     * @Author lgn
     * @Date 18:38 2022/9/16
     * @Version 1.0
     **/
    @Data
    public class ImportServer implements Serializable {
        private static final long serialVersionUID = 1L;
    
        @Excel(name = "计算机名称", orderNum = "0",width = 10.0)
        private String name;
        @Excel(name = "服务器IP", orderNum = "1",width = 10.0)
        private String ip;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    ps:
    如果excel里面有日期格式的数据,实体类这样处理:
    在这里插入图片描述

    添加注解:
    @DateTimeFormat(fallbackPatterns = “yyyy-MM-dd”)
    字段接收类型采用 import java.util.Date;

    /** 设备放行时间 */
    @Excel(name = "设备放行时间", orderNum = "13",width = 10.0)
    @DateTimeFormat(fallbackPatterns = "yyyy-MM-dd")
    private Date completionTime;
    
    • 1
    • 2
    • 3
    • 4

    我这里是两个sheet:
    在这里插入图片描述
    在这里插入图片描述

    Controller添加excel导入方法

    /**
     * excel多sheet导入
     */
    @ApiOperation(value = "excel多sheet导入", notes = "excel多sheet导入")
    @PostMapping(value ="ImportAssetsManage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public void importForSheetUsers(@RequestPart("file") MultipartFile file) throws IOException {
        userService.importForSheetUsers(file);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    实现导入方法(核心)

    package com.ypk.swagger.utils;
    import cn.afterturn.easypoi.excel.ExcelImportUtil;
    import cn.afterturn.easypoi.excel.entity.ImportParams;
    import java.io.InputStream;import java.util.List;
    import java.util.NoSuchElementException;
    /**
     * @ClassName EasyPoiUtils * @Description TODO
     * @Author lgn
     * @Date 17:15 2022/9/16
     * @Version 1.0
     **/
    public class EasyPoiUtils {
        /**
         * 功能描述:根据接收的Excel文件来导入多个sheet,根据索引可返回一个集合
         *
         * @param inputStream  excel输入流
         * @param sheetIndex 导入sheet索引
         * @param titleRows  表标题的行数
         * @param headerRows 表头行数
         * @param pojoClass  Excel实体类
         */
        public static  List importExcel(InputStream inputStream, int sheetIndex, Integer titleRows, Integer headerRows, Class pojoClass) {
            // 根据file得到Workbook,主要是要根据这个对象获取,传过来的excel有几个sheet页
            ImportParams params = new ImportParams();
            // 第几个sheet表页
            params.setStartSheetIndex(sheetIndex);
            //设置表标题行数
            params.setTitleRows(titleRows);
            //设置表头行数
            params.setHeadRows(headerRows);
            List list = null;
            try {
                //读取的excel数据集合
                list = ExcelImportUtil.importExcel(inputStream, pojoClass, params);
            } catch (NoSuchElementException e) {
                throw new RuntimeException("模板不能为空");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return list;
        }
    }
    
    
    • 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

    Service UserServiceImpl

    package com.ypk.swagger.service;
    
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.IOException;
    
    /**
     * @ClassName UserService * @Description TODO
     * @Author lgn
     * @Date 17:12 2022/9/16
     * @Version 1.0
     **/
    public interface UserService {
        void importForSheetUsers(MultipartFile file) throws IOException;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    package com.ypk.swagger.service.impl;
    
    import com.ypk.swagger.model.ImportInstrument;
    import com.ypk.swagger.model.ImportServer;
    import com.ypk.swagger.service.UserService;
    import com.ypk.swagger.utils.EasyPoiUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.IOException;
    import java.util.List;
    
    /**
     * @ClassName UserServiceImpl * @Description TODO
     * @Author lgn
     * @Date 17:13 2022/9/16
     * @Version 1.0
     **/
    @Service
    public class UserServiceImpl implements UserService {
        @Override
        public void importForSheetUsers(MultipartFile file) throws IOException {
    
            //分批进行读取excel的sheet工作表
            //读取第一个sheet表
            List sheetOneUsers = EasyPoiUtils.importExcel(file.getInputStream(), 0, 1, 1, ImportInstrument.class);
            //读取第二个sheet表
            List sheetTwoUsers = EasyPoiUtils.importExcel(file.getInputStream(), 1, 1, 1, ImportServer.class);
    
            for (ImportInstrument o:sheetOneUsers){
    
                System.out.println("------one:"+o);
            }
    
            for (ImportServer o:sheetTwoUsers){
                System.out.println("------two:"+o);
            }
    
        }
    }
    
    
    • 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

    测试一下:

    在这里插入图片描述
    在这里插入图片描述
    源码地址:
    https://github.com/nanist/swagger.git

  • 相关阅读:
    Android 11 定制系统全局监听触摸事件接口
    C语言进阶第六课-----------字符分类函数和内存的开辟
    我的UI自动化测试的感悟
    2023-09-17 LeetCode每日一题(打家劫舍 II)
    《进阶篇第9章》学习vuex知识点后练习:把求和案例改成getters
    PMP考试提分必刷题
    Linux内核驱动开发-字符设备驱动框架
    SpringSession ( 一 ) HttpSession
    使用jquery上传多个图像
    【PG】PostgreSQL查看与修改参数
  • 原文地址:https://blog.csdn.net/u010797364/article/details/126896037