首页 > 其他分享 >SpringBoot(六) - 阿里巴巴的EasyExcel

SpringBoot(六) - 阿里巴巴的EasyExcel

时间:2022-12-01 15:12:15浏览次数:42  
标签:info sheet SpringBoot EasyExcel list student public 阿里巴巴

1、依赖

<!-- 阿里EasyExcel start -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.1.7</version>
</dependency>

2、写入Excel

2.1 实体

@Data
public class Student {

    //学号
    @ExcelProperty("学号")
    private Integer id;

    // 姓名
    @ExcelProperty("姓名")
    private String name;

    // 年龄
    @ExcelProperty("年龄")
    private Integer age;

    // 班级
    @ExcelProperty("班级")
    private String classRoom;

    // 性别
    @ExcelProperty("性别")
    private String sex;

    // 院校
    @ExcelProperty("院校")
    private String graduate;

    // 毕业时间
    @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")
    @ExcelProperty("毕业时间")
    private Date graduateTime;

    // 手机号
    @ExcelProperty("手机号")
    private String phone;
    
    //@NumberFormat("#.#") //保留1位小数
    //@ExcelProperty(value = "薪资", index = 3) //设置表图和指定的列, 将工资放在 第四列
    //@ExcelIgnore  忽略字段,比如密码
    
}

2.2 实体的数据监听器

@Slf4j
public class StudentDataListener extends AnalysisEventListener<Student> {

    /**
     * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 5; //实际操作的时候看情况修改
    List<Student> list = new ArrayList<>();

    /**
     * 这个每一条数据解析都会来调用
     *
     * @param data
     *            one row value. Is is same as {@link AnalysisContext#readRowHolder()}
     * @param context
     */
    @Override
    public void invoke(Student data, AnalysisContext context) {
        log.info("解析到一条数据:{}", data);
        list.add(data);
        // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
        if (list.size() >= BATCH_COUNT) {
            log.info("存数据库");

            // 批量插入
            // 存储完成清理 list
            list.clear();
        }
    }

    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("所有数据解析完成!");
    }

    //为了方便获取读取的数据
    public List<Student> getList() {
        return list;
    }
}

2.3 03版本的Excel写入

@Test
public void testExcel03(){

    //要写入的文件的路径
    String fileName = "D:\\KEGONGCHANG\\DaiMa\\IDEA\\KH96\\SpringBoot\\SpringBoot\\springboot-03-asyztimer\\excel\\excel03_student.xls";

    // 如果这里想使用03 则 传入excelType参数即可
    //获取学生信息,实际应用中从数据库中查询出来即可,list集合

    Student student =   Student.builder().id(19).name("huayu").age(19)
        .classRoom("KH96").sex("男").graduate("金科")
        .graduateTime(new Date()).phone("13501020304").build();
    List<Student> studentList = new ArrayList<>();
    studentList.add(student);

    //将数据写入文件
    EasyExcel.write(fileName, Student.class)
        	 .excelType(ExcelTypeEnum.XLS)
        	 .sheet("学生信息")
        	 .doWrite(studentList);

    log.info("学生信息写入完成!!!{}",student);

}

测试结果:

2.4 07版本的Excel 写入

@Test
public void testExcel07(){

   //要写入的文件的路径
   String fileName = "D:\\KEGONGCHANG\\DaiMa\\IDEA\\KH96\\SpringBoot\\SpringBoot\\springboot-03-asyztimer\\excel\\excel07_student.xls";


   //获取学生信息
    studentList.add(student);

     //07版写入文件
    EasyExcel.write(fileName, Student.class)
        	 .sheet("学生信息")
        	 .doWrite(studentList);

    log.info("学生信息写入完成!!!{}",student);

}

测试结果:

2.5 写入多个sheet

@Test
public void testWriteSheets(){

    String fileName = "D:\\KEGONGCHANG\\DaiMa\\IDEA\\KH96\\SpringBoot\\SpringBoot\\springboot-03-asyztimer\\excel\\excel07_student_sheets.xlsx";

    //获取学生信息
    studentList.add(student);

    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    ExcelWriter excelWriter = EasyExcel.write(fileName, Student.class).build();

    WriteSheet sheet;

    for (int i = 0; i < 2; i++) {
        sheet = EasyExcel.writerSheet(i, "学生信息"+(i+1)).build();
        excelWriter.write(studentList,sheet);
    }

    excelWriter.finish();

    log.info("学生信息写入完成!!!{}",student);

}

测试结果:

3、读出Excel

3.1 doRead()

3.1.1 sheet1中的数据

sheet1:

3.1.2 doRead() 方法读取

//读取Excel中的学生信息
@Test
public void testReadExcel(){

    String fileName = "D:\\KEGONGCHANG\\DaiMa\\IDEA\\KH96\\SpringBoot\\SpringBoot\\springboot-03-asyztimer\\excel\\excel07_student_sheets.xlsx";

    // 实例化一个数据监听器
    StudentDataListener studentDataListener =  new StudentDataListener();

    //第一种
    //读取后的数据放进list中,所以可以从list中获取数据,(这里的数据是我们处理过的,最多返回5条的数据,可以根据实际境况修改)
    EasyExcel.read(fileName, Student.class,studentDataListener)
        	 .sheet()  //默认读取第一个sheet
        	 .doRead();
    List<Student> list = studentDataListener.getList();
    log.info("------ 读取后的数据放进list中,所以可以从list中获取数据,最多返回5条的数据 ------");
    list.forEach(ersonData->{
        log.info(ersonData.toString());
    });

}

3.1.3 测试结果:

3.1.4 分析 doRead() 方法

//没有任何数据返回
public void doRead() {
    if (this.excelReader == null) {
        throw new ExcelGenerateException("Must use 'EasyExcelFactory.read().sheet()' to call this method");
    } else {
        this.excelReader.read(new ReadSheet[]{this.build()});
        this.excelReader.finish();
    }
}

3.2 doReadSync()

3.2.1 sheet2中的数据

sheet2:

3.2.2 doReadSync() 方法读取

//读取Excel中的学生信息
@Test
public void testReadExcel(){

    String fileName = "D:\\KEGONGCHANG\\DaiMa\\IDEA\\KH96\\SpringBoot\\SpringBoot\\springboot-03-asyztimer\\excel\\excel07_student_sheets.xlsx";

    // 实例化一个数据监听器
    StudentDataListener studentDataListener =  new StudentDataListener();

    //第二种
    log.info("------ 同步获取数据,读取到的所有数据 ------");
    //同步获取数据,读取到的所有数据
    List<Student> list2 =  EasyExcel.read(fileName, Student.class,studentDataListener)
        							.sheet(1) //读取第二个sheet
        							.doReadSync();
    list2.forEach(ersonData->{
        log.info(ersonData.toString());
    });
}

3.2.3 测试结果:

3.2.4 分析 doReadSync() 方法

//会将读取到的数据返回
public <T> List<T> doReadSync() {
    if (this.excelReader == null) {
        throw new ExcelAnalysisException("Must use 'EasyExcelFactory.read().sheet()' to call this method");
    } else {
        SyncReadListener syncReadListener = new SyncReadListener();
        this.registerReadListener(syncReadListener);
        this.excelReader.read(new ReadSheet[]{this.build()});
        this.excelReader.finish();
        return syncReadListener.getList();
    }
}
JAVA 复制 全屏

标签:info,sheet,SpringBoot,EasyExcel,list,student,public,阿里巴巴
From: https://www.cnblogs.com/hanease/p/16941450.html

相关文章

  • SpringBoot(七) - Redis 缓存
    1、五大基本数据类型和操作1.1字符串-string命令说明setkeyvalue如果key还没有,那就可以添加,如果key已经存在了,那会覆盖原有key的值getkey如果key还没有,......
  • SpringBoot(九) - Swagger
    1、依赖<!--swagger核心--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.8.0</version></......
  • vscode+springboot+gradle
    vscode+springboot+gradle项目搭建demo目标:项目中抛弃所有xml格式文件啰嗦:  一直在用maven作为项目的依赖包管理,最近看到基于Java17的Springframwork6......
  • springboot 项目自动重启脚本及注册方式
    创建脚本文件/etc/init.d/prs_xml.jar 添加脚本chkconfig-add prs_xml.jar查看服务列表chkconfig--list 启动服务chkconfigprs_xml.jaron设置启动等级chk......
  • SpringBoot使用restTemplate远程访问时报错
    错误场景SpringBoot使用restTemplate远程访问时报错java.lang.IllegalStateException:Noinstancesavailableforxxx解决方案这个报错一般会出现在使用了负载均衡,如:......
  • SpringBoot 3.0 新特性,内置声明式 HTTP 客户端
    httpinterface从Spring6和SpringBoot3开始,Spring框架支持将远程HTTP服务代理成带有特定注解的Javahttpinterface。类似的库,如OpenFeign和Retrofit仍然......
  • SpringBoot启动流程
    run方法启动时传入了当前类ContractApplication.class,传入当前类的作用主要是为了解析当前类上面的注解(不一定传递当前类,也可以自己写一个类,在写的类上添加对应的注解)......
  • SpringBoot+Bootstrap+Thymeleaf+Restful 实现图书商城管理
    课程名称:企业项目实训II设计题目:大学当图书商城已知技术参数和设计要求:1.问题描述(功能要求):1.1顾客端1)注册登录:游客可浏览所有图书,只有注册用户才能添加购物车、下订单......
  • SpringBoot项目出现404错误--且控制台无任何信息
    直接上链接:(138条消息)SpringBoot项目出现404错误_露天赏雪的博客-CSDN博客_springboot404我自己的错误原因:有启动类的包,也有自己的包,但包不在一起,所以根本访问不到自己......
  • vuecli3项目集成到springboot
    路径配置当springboot中设置项目访问路径server.servlet.context-path=/demovue项目中vue.config.js需配置publicPathmodule.exports={transpileDependenc......