EasyExcel导入导出

按照阿里大佬github源代码进行编写(抄写)
源代码url:https://github.com/alibaba/easyexcel

一.jar包引入


		<dependency>
            <groupId>com.alibabagroupId>
            <artifactId>easyexcelartifactId>
            <version>2.1.4version>
        dependency>
        

        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxmlartifactId>
            <version>4.1.1version>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poiartifactId>
            <version>4.1.1version>
        dependency>

一.EasyExcel读取

Excel对应的实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student extends BaseRowSet {
    //对应excel中的字段
    @ExcelProperty(value = "name",index = 0)
    private String name;
    @ExcelProperty(value = "age",index = 1)
    private String age;
    @ExcelProperty(value = "classno",index = 2)
    private String classNo;
}

包含日期和数字实体类

@Data
public class ExcelStu {
    @ExcelProperty(value = "姓名",index = 0)
    private String name;
    //将日期转换成对应的String
    @DateTimeFormat("yyyy年MM月DD日")
    private String date;
    //将数字转换成对应的String
    @NumberFormat("#.##")
    private String money;
}

EasyExcel监听器

//继承AnalysisEventListener 泛型为对应的实体类
public class StuExcelListener extends AnalysisEventListener<Student> {

    //用来接收一行行的数据
    List<Student> list = new ArrayList<>();

    //解析中的方法
    @Override
    public void invoke(Student stu, AnalysisContext context) {
        list.add(stu);
        System.out.println(stu);
    }

    //解析完之后的方法
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }
}

读取单个sheet

//读取单个sheet
    @Test
    public void fun1(){
        //创建自定义读取Excel
        ExcelReader excelReader = EasyExcel.read(filepath, ExcelStu.class, new StuExcelListener2()).build();
        //确定读取的sheet
        ReadSheet readSheet = EasyExcel.readSheet(1,"Sheet2").build();
        //读取sheet
        excelReader.read(readSheet);
        //关闭读取流
        excelReader.finish();

    }

读取多个sheet

//读取多个sheet
    @Test
    public void fun2(){
        //获取读取对象
        ExcelReader excelReader = EasyExcel.read(filepath).build();
        //确定读取的sheet
        //head读取数据映射的实体类
        //registerReadListener:创建对应的监听器
        ReadSheet readSheet1 = EasyExcel.readSheet(0).head(Student.class).registerReadListener(new StuExcelListener()).build();
        ReadSheet readSheet2 = EasyExcel.readSheet(1).head(ExcelStu.class).registerReadListener(new StuExcelListener2()).build();
        //这里注意 一定要把sheet1 sheet2 一起传进去,不然有个问题就是03版的excel 会读取多次,浪费性能
        excelReader.read(readSheet1,readSheet2);
        // 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
        excelReader.finish();
    }

二.EasyExcel写出

Excel对应的实体类

@Data
public class WriteData {
    @ExcelProperty("字符串标题")
    private String string;
    @ExcelProperty("日期标题")
    private Date date;
    @ExcelProperty("数字标题")
    private Double doubleData;
}

写出单个sheet

public class WriteTest {
    private String filepath = "C:\\Users\\Administrator\\Desktop\\test.xlsx";

	//单个sheet导出
    @Test
    public void fun1(){
        //创建导出excel对象
        ExcelWriter excelWriter = EasyExcel.write(filepath, WriteData.class).build();
        //创建接收数据的sheet表
        WriteSheet modelSheet = EasyExcel.writerSheet("模板").build();
        //进行数据导入 参数1:数据,参数2:接收的表
        excelWriter.write(data(),modelSheet);
        //关闭写出流
        excelWriter.finish();

    }

    //模拟数据库数据
    private List<WriteData> data() {
        List<WriteData> list = new ArrayList<WriteData>();
        for (int i = 0; i < 10; i++) {
            WriteData data = new WriteData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }
}

你可能感兴趣的:(新姿势)