easyexcel读取

1.导入pom

        
            com.alibaba
            easyexcel
            2.2.6
        
  1. 读取
    // 优势,不是整个文件的读取,而是按行进行读取
    public static void main(String[] args) {
        ExcelReaderBuilder read = EasyExcel.read();
        // 获取文件
        read.file("");
        // 读取的sheet  不指定默认读取全部的sheet页
        read.sheet("用户输入表");
        // 自动关闭流
        read.autoCloseStream(true);
        // 设置文件格式
        read.excelType(ExcelTypeEnum.XLSX);
        // 注册监听器
        read.registerReadListener(new AnalysisEventListener() {
            // 一行读取完成之后进行回调 o表示当前行数据
            @Override
            public void invoke(Object o, AnalysisContext analysisContext) {
                System.out.println(o);
            }

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

        // 构建读取器
        ExcelReader build = read.build();
        // 读取数据
        build.readAll();
        // 读取完毕
        build.finish();
    }

3.泛型使用

 // 优势,不是整个文件的读取,而是按行进行读取
    public static void main(String[] args) {
        ExcelReaderBuilder read = EasyExcel.read();
        // 获取文件
        read.file("F:\\easyexcel.xlsx");
        // 读取的sheet  不指定默认读取全部的sheet页
        read.sheet("学生表");
        // 自动关闭流
        read.autoCloseStream(true);
        // 设置文件格式
        read.excelType(ExcelTypeEnum.XLSX);
        // 注册监听器
        read.registerReadListener(new AnalysisEventListener>() {
            // 一行读取完成之后进行回调 o表示当前行数据
            @Override
            public void invoke(Map map, AnalysisContext analysisContext) {
//                System.out.println(map);
                Iterator iterator = map.keySet().iterator();
                while (iterator.hasNext()) {
                    Integer next = iterator.next();
                    System.out.print(next + ":" + map.get(next) + ',');
                }
                System.out.println("");
            }

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

        // 构建读取器
        ExcelReader build = read.build();
        // 读取数据
        build.readAll();
        // 读取完毕
        build.finish();
    }

4.简化使用

package com.chinasoft.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class MyEasyExcel2 {

    public static void main(String[] args) {
        List> list =new LinkedList<>();
        EasyExcel.read("F:\\easyexcel.xlsx").
                sheet("学生表").
                registerReadListener(new AnalysisEventListener>() {
            @Override
            public void invoke(Map map, AnalysisContext analysisContext) {
                list.add(map); // 如果数据量过大,还是可能内存泄漏
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("数据读取完毕.");
            }
        }).doRead();

        for(Map map: list) {
            Iterator iterator = map.keySet().iterator();
            while (iterator.hasNext()) {
                Integer next = iterator.next();
                System.out.print(next + ":" + map.get(next) + ',');
            }
            System.out.println("");
        }
    }
}

5.对象类型

package com.chinasoft.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.chinasoft.easyexcel.entity.ExcelEntity;

import java.util.LinkedList;
import java.util.List;

public class MyEasyExcel3 {

    public static void main(String[] args) {
        List list = new LinkedList<>();

        EasyExcel.read("F:\\easyexcel.xlsx").
                head(ExcelEntity.class).sheet("学生表").
                registerReadListener(new AnalysisEventListener() {
            @Override
            public void invoke(ExcelEntity entity, AnalysisContext analysisContext) {
                list.add(entity); // 如果数据量过大,还是可能内存泄漏
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("数据读取完毕.");
            }
        }).doRead();

        for(ExcelEntity entity: list) {
            System.out.println(entity);
        }
    }
}

实体类

package com.chinasoft.easyexcel.entity;

import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;

@Data
public class ExcelEntity {

   @ExcelProperty(value = "编号")
   private String id;

   @ExcelProperty(value = "姓名")
   private String name;

   @ExcelProperty(value = "年龄")
   private Integer age;

   @ExcelProperty(value = "生肖")
   private String shen;

   @ExcelProperty(value = "学校")
   private String school;

   @ExcelProperty(value = "成绩")
   private Integer score;
}

对应excel

easyexcel读取_第1张图片

代码截图
easyexcel读取_第2张图片

你可能感兴趣的:(java)