EasyExcel定义与概述:
EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel工具。
github地址:GitHub - alibaba/easyexcel: 快速、简洁、解决大文件内存溢出的java处理Excel工具
EasyExcel特点
Java解析、生成Excel比较有名的框架有Apache poi、jxl,但他们都存在一个严重的问题就是非常的耗内存。
EasyExcel 重写了poi,使一个3M的excel只需要几M内存,并且再大的excel不会出现内存溢出。
64M内存1分钟内读取75M(46W行25列)的Excel
目标一:用java代码写出一个excel表格
EasyExcel相关基本操作步骤如下:
1.搭建环境,导入相关的坐标:
<dependencies>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- 注: 需要导入相关的版本号,我的版本号已经在父类导入,此处就不展示了哈-->
</dependency>
<!--easyexcel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!-- 注: 需要导入相关的版本号,我的版本号已经在父类导入,此处就不展示了哈-->
</dependency>
</dependencies>
2.创建对应字段的实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@ColumnWidth(20)//宽
@HeadRowHeight(70)//表头高
@ContentRowHeight(40)//正文行高
public class Student {
@ExcelProperty("编号")//该注解会在文件中生成对应的表头字段
private String id;
@ExcelProperty("姓名")
private String name;
@ExcelProperty("年龄")
private Integer age;
@ExcelProperty("电话")
private String phone;
@ExcelProperty("邮箱")
private String email;
@ExcelProperty("生日")
private Date birthday;
}
3.具体代码:
3.1 生成在本地
public List<Student> getData(){
ArrayList<Student> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(new Student("stu"+i,"zhang"+i,18+i,"110"+i,"zhang"+i+"@163.com",new Date()));
}
return list;
}
@Test
public void test01(){
//把数据写到 E:\02B\aa/student_01.xsl
String file="E:/02B/aa/student_01.xls";
EasyExcel.write(file,Student.class).sheet("班级").doWrite(getData());
}
3.2生成在java的编译target包中:
public List<Student> getData(){
ArrayList<Student> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(new Student("stu"+i,"zhang"+i,18+i,"110"+i,"zhang"+i+"@163.com",new Date()));
}
return list;
}
@Test
public void test01(){
//把数据写到 E:\02B\aa/student_01.xsl
String file="E:/02B/aa/student_01.xls";
EasyExcel.write(file,Student.class).sheet("班级").doWrite(getData());
}
public String getPath(){
String path = this.getClass().getResource("/").getPath();
return path;
}
@Test
public void test02(){
System.out.println(getPath());
}
@Test
public void test03(){
String file=getPath()+"student_01.xls";
EasyExcel.write(file,Student.class).sheet("班级").doWrite(getData());
}
目标二:用java代码读出一个excel表格
1.创建javabean 类:
@Data
@NoArgsConstructor
@AllArgsConstructor
@ColumnWidth(20)//宽
@HeadRowHeight(70)//表头高
@ContentRowHeight(40)//正文行高
public class Student {
@ExcelProperty("编号")
private String id;
@ExcelProperty("姓名")
private String name;
@ExcelProperty("年龄")
private Integer age;
@ExcelProperty("电话")
private String phone;
@ExcelProperty("邮箱")
private String email;
@ExcelProperty("生日")
private Date birthday;
}
2.创建监听类:
package com.czxy.zx.demo01;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
public class StudentListener extends AnalysisEventListener<Student> {
@Override
public void invoke(Student student, AnalysisContext analysisContext) {
//每解析一条数据 就会执行一次该方法
System.out.println("data --> "+student);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("解析数据完毕");
}
}
@Test
public void test04(){
String file=getPath()+"student_01.xls";
EasyExcel.read(file,Student.class,new StudentListener()).sheet("班级").doRead();
}
public String getPath(){
String path = this.getClass().getResource("/").getPath();
return path;
}
本文含有隐藏内容,请 开通VIP 后查看