Excel操作导出-easyexcel使用入门

资料

github地址:https://github.com/alibaba/easyexcel
官方网站: https://yuque.com/easyexcel

介绍

Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,能够原本一个3M的excel用POI sax依然需要100M左右内存降低到几M,并且再大的excel不会出现内存溢出,03版依赖POI的sax模式。在上层做了模型转换的封装,让使用者更加简单方便。

入门例子

pom.xml

        <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.7</version>
        </dependency>

数据写对象

简单的一个对象,对应到excel的行。


import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;

public class TableHeaderExcelProperty extends BaseRowModel {

    /**
     * value: 表头名称
     * index: 列的号, 0表示第一列
     */
    @ExcelProperty(value = "姓名", index = 0)
    private String name;

    @ExcelProperty(value = "年龄",index = 1)
    private int age;

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }
}

编写main测试例子

public class EasyExcelStudy {

    public static void main(String[] args) {
        String filePath = "F:/测试11.xlsx";
        ArrayList<TableHeaderExcelProperty> data = new ArrayList<>();
        for(int i = 0; i < 1000; i++){
            TableHeaderExcelProperty tableHeaderExcelProperty = new TableHeaderExcelProperty();
            tableHeaderExcelProperty.setName("姓名" + i);
            tableHeaderExcelProperty.setAge(22 + i);
            tableHeaderExcelProperty.setSchool("学校" + i);
            data.add(tableHeaderExcelProperty);
        }

        //ExcelUtil.writeSimpleBySheet(filePath,data);
        EasyExcel.write(filePath, TableHeaderExcelProperty.class).sheet("模板").doWrite(data);

    }
}

运行结果

17:28:13.465 [main] DEBUG com.alibaba.excel.context.WriteContextImpl - Begin to Initialization 'WriteContextImpl'
17:28:13.531 [main] DEBUG com.alibaba.excel.metadata.property.ExcelHeadProperty - The initialization sheet/table 'ExcelHeadProperty' is complete , head kind is CLASS
17:28:13.598 [main] DEBUG com.alibaba.excel.context.WriteContextImpl - CurrentConfiguration is writeWorkbookHolder
17:28:13.968 [main] DEBUG com.alibaba.excel.context.WriteContextImpl - Initialization 'WriteContextImpl' complete
17:28:13.970 [main] DEBUG com.alibaba.excel.metadata.property.ExcelHeadProperty - The initialization sheet/table 'ExcelHeadProperty' is complete , head kind is CLASS
17:28:13.971 [main] DEBUG com.alibaba.excel.context.WriteContextImpl - CurrentConfiguration is writeSheetHolder
17:28:13.971 [main] DEBUG com.alibaba.excel.context.WriteContextImpl - Can not find sheet:null ,now create it
17:28:14.427 [main] DEBUG com.alibaba.excel.context.WriteContextImpl - Finished write.

Excel操作导出-easyexcel使用入门_第1张图片

总结

使用非常的方便和简单。从网上查到的版本,好多事Sheet类,这个新版本舍弃掉了。看官网例子是最保险的做法。

你可能感兴趣的:(Java基础,excel,easyexcle)