Code前端首页关于Code前端联系我们

JAVA一行代码实现EXCEL读写——EasyExcel方法封装

terry 2年前 (2023-09-25) 阅读数 57 #后端开发

EasyExcel的github地址:github.com/alibaba/eas... EasyExcel的官方介绍:JAVA 一行代码实现 EXCEL 读写——EasyExcel 方法封装

可以看到,EasyExcel最大的特点就是它使用更少的内存。当然,现在它的功能比较简单,能面对的复杂场景也比较少,但是基本的读写完全令人满意。 ?完成Excel的读取或写入的方法

2。 ExcelListener

监听类,可以根据自己的需求和自身情况自定义接收数据的处理。这里我只是将数据添加到列表中。 ? 读取 Excel 时,只需要调用 ExcelUtil.readExcel() 方法

@RequestMapping(value = "readExcel", method = RequestMethod.POST)
public Object readExcel(MultipartFile excel) {
    return ExcelUtil.readExcel(excel, new ImportInfo());
}
复制代码

其中 Excel 是 MultipartFile 类型的文件对象,new ImportInfo() 是 Excel 映射的实体对象,需要继承于 BaseRowModel类,如:

public class ImportInfo extends BaseRowModel {
    @ExcelProperty(index = 0)
    private String name;

    @ExcelProperty(index = 1)
    private String age;

    @ExcelProperty(index = 2)
    private String email;

    /*
        作为 excel 的模型映射,需要 setter 方法
     */
    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;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
复制代码

作为映射实体类,成员变量映射的列可以通过@ExcelProperty注解和索引变量来标记。同时,setter方法

也是必不可少的。四。导出 Excel

1。导出Excel只有一张

只需要ExcelUtil.writeExcelWithSheets()方法:

@RequestMapping(value = "writeExcel", method = RequestMethod.GET)
public void writeExcel(HttpServletResponse response) throws IOException {
    List<ExportInfo> list = getList();
    String fileName = "一个 Excel 文件";
    String sheetName = "第一个 sheet";

    ExcelUtil.writeExcel(response, list, fileName, sheetName, new ExportInfo());
    }
复制代码

FileName、SheetName分别是文件名和导出文件名(分别是Info、导出文件名。)为导出数据实体对象的Mapping,list为导出数据。

对于映射的实体类,可以根据需要通过@ExcelProperty注解自定义头部。当然,还必须继承BaseRowModel类,如:

public class ExportInfo extends BaseRowModel {
    @ExcelProperty(value = "姓名" ,index = 0)
    private String name;

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

    @ExcelProperty(value = "邮箱",index = 2)
    private String email;

    @ExcelProperty(value = "地址",index = 3)
    private String address;
}
复制代码

value是列名,index是列的序号

如果需要更复杂的话,效果可以如图以下。达到:JAVA 一行代码实现 EXCEL 读写——EasyExcel 方法封装

对应的实体类写法如下:

public class MultiLineHeadExcelModel extends BaseRowModel {

    @ExcelProperty(value = {"表头1","表头1","表头31"},index = 0)
    private String p1;

    @ExcelProperty(value = {"表头1","表头1","表头32"},index = 1)
    private String p2;

    @ExcelProperty(value = {"表头3","表头3","表头3"},index = 2)
    private int p3;

    @ExcelProperty(value = {"表头4","表头4","表头4"},index = 3)
    private long p4;

    @ExcelProperty(value = {"表头5","表头51","表头52"},index = 4)
    private String p5;

    @ExcelProperty(value = {"表头6","表头61","表头611"},index = 5)
    private String p6;

    @ExcelProperty(value = {"表头6","表头61","表头612"},index = 6)
    private String p7;

    @ExcelProperty(value = {"表头6","表头62","表头621"},index = 7)
    private String p8;

    @ExcelProperty(value = {"表头6","表头62","表头622"},index = 8)
    private String p9;
}
复制代码

2。导出的Excel有几张表,通过方法finish()完成
public void writeExcelWithSheets(HttpServletResponse response) throws IOException {
    List<ExportInfo> list = getList();
    String fileName = "一个 Excel 文件";
    String sheetName1 = "第一个 sheet";
    String sheetName2 = "第二个 sheet";
    String sheetName3 = "第三个 sheet";

    ExcelUtil.writeExcelWithSheets(response, list, fileName, sheetName1, new ExportInfo())
                .write(list, sheetName2, new ExportInfo())
                .write(list, sheetName3, new ExportInfo())
                .finish();
}
复制代码

write方法。 write方法的参数是当前sheet的列表数据、当前sheet名称以及对应的映射类。

作者:Howie_Y

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门