目 录CONTENT

文章目录

EasyExcel读excel

在水一方
2022-01-06 / 0 评论 / 0 点赞 / 211 阅读 / 2,801 字 / 正在检测是否收录...

今天来回顾一下easyExcel如何读excel

  • 导入jar包
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>easyexcel</artifactId>
			<version>2.2.6</version>
		</dependency>

第一种写法:

 @Test
    public void test01(){
        // 获取ExcelReaderBuilder对象
        ExcelReaderBuilder readerBuilder = EasyExcel.read();
        //读取的文件位置
        readerBuilder.file("D:\\test.xlsx");
        // 自动关闭流
        readerBuilder.autoCloseStream(true);
        //读取哪张表
        readerBuilder.sheet("工作表");
        // excel的格式
        readerBuilder.excelType(ExcelTypeEnum.XLSX);

        // 注册监听器
        readerBuilder.registerReadListener(new AnalysisEventListener<Map<Integer,String>>() {
            @Override
            public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
                Set<Integer> set = integerStringMap.keySet();
                Iterator<Integer> iterator = set.iterator();
                while(iterator.hasNext()){
                    Integer key = iterator.next();
                    System.out.println(key + ":" + integerStringMap.get(key)+",");
                }

            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                // 通知文件读取完毕
                System.out.println("读取完毕");
            }
        });

        // 读取数据
        ExcelReader reader  = readerBuilder.build();

        reader.readAll();

        reader.finish();

    }

简写的方式


 @Test
    public void test02(){
        List<Map<Integer,String>> list = new LinkedList<>();
        EasyExcel.read().file("D:\\test.xlsx").sheet("工作表")
                .registerReadListener(new AnalysisEventListener<Map<Integer, String>>() {

                    @Override
                    public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
                        list.add(integerStringMap);
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                    }
                }).doRead();


        List<Demo> li = new ArrayList<>();

        for(Map<Integer,String> integerStringMap :list){
            Set<Integer> set = integerStringMap.keySet();
            Iterator<Integer> iterator = set.iterator();
            while (iterator.hasNext()){
                Integer key = iterator.next();
                System.out.println(key + ":" + integerStringMap.get(key)+" ,");

            }

        }
    }


将excel中的数据映射到对象中,并返回list

    @Test
    public void test03(){
        List<Demo> list = new LinkedList<>();
        EasyExcel.read().file("D:\\test.xlsx").sheet().headRowNumber(2) // 从第二行开始读
                .head(Demo.class)
                .registerReadListener(new AnalysisEventListener<Demo>() {


                    @Override
                    public void invoke(Demo demo, AnalysisContext analysisContext) {
                        list.add(demo);
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                    }
                }).doRead();


      for(Demo demo : list){

          System.out.println(demo);
      }


    }


Demo实体类的写法


public class Demo {
    @ExcelProperty("姓名")
    private String name;
    @ExcelProperty("年龄")
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Demo{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

说明:Demo实体类中的字段命名必须是驼峰命名法,如果不是则无法将数据封装进去

0

评论区