java解析excel的框架有很多 poi、jxl,存在问题:非常的消耗内存
easyexcel 我们遇到再大的 excel 都不会出现内存溢出的问题,能够将一个原本3M excel文件,poi来操作会占用内存 100M,使用easyexcel减低到几MB,使用起来更加简单
poi 1、创建XSSFWorkbook
2、读取sheet
3、拿到当前 sheet 所有行 row
4、通过当前行去拿到对应的单元格的值
<dependency>
<groupId>com.alibabagroupId>
<artifactId>easyexcelartifactId>
<version>3.3.1version>
dependency>
@Data
@AllArgsConstructor
@NoArgsConstructor
// 行高
@HeadRowHeight(15)
// 行宽
@ColumnWidth(20)
public class TUser implements Serializable {
@TableId(type = IdType.AUTO)
// 每列名称
@ExcelProperty(index = 0, value = "编号")
private Integer id;
@ExcelProperty(index = 1, value = "名称")
private String name;
@ExcelProperty(index = 2, value = "年龄")
private Integer age;
// @ExcelIgnore 不生效
@ExcelProperty(index = 3, value = "性别")
private String sex;
}
// 往excel 中做个sheet中写入同类型数据
@Test
public void excelOutPut2() {
long start = System.currentTimeMillis();
// xls文件最大行数:65536
// xlsx文件最大行数:1048576
// 文件位置
String path = "D:\\user.xls";
// 创建 easyexcel 的写出类构造器 参数 告诉构造器,我的excel将来要写到哪里,以及execl中数据是基于哪个java对象模板创建的
ExcelWriter excelWriter = EasyExcel.write(path, TUser.class).build();
Integer pageSize = userService.selectListCount();
Integer sheetSize = pageSize % 50000 == 0 ? pageSize / 50000 : pageSize / 50000 + 1;
for (int i = 1; i <= sheetSize; i++) {
Page<TUser> page = new Page<TUser>(i, 50000);
List<TUser> users = userService.selectListPage(page);
// 创建 shee 构造器
WriteSheet sheet = EasyExcel.writerSheet(i, "第【" + i + "】批数据").build();
// 使用excel 对象数据写到创建sheet当中
excelWriter.write(users, sheet);
}
excelWriter.finish();
System.out.println("导出完毕");
long end = System.currentTimeMillis();
System.out.println(end - start);
}
//
@Test
public void test3() {
// 导出文件位置
String path = "D:\\user2.xls";
// 告诉其输出位置
ExcelWriter excelWriter = EasyExcel.write(path).build();
// 创建 sheet 构造器时通过head属性告诉其导出类型的模板
WriteSheet build = EasyExcel.writerSheet("地区信息").head(TUser.class).build();
WriteSheet build2 = EasyExcel.writerSheet("学生信息").head(Student.class).build();
Page<TUser> page = new Page<TUser>(1, 100);
List<TUser> users = userService.selectListPage(page);
excelWriter.write(users, build);
excelWriter.write(getStudents(), build2);
excelWriter.finish();
}
public List<Student> getStudents() {
List<Student> students = new ArrayList<Student>();
for (int i = 0; i < 100; i++) {
Student student = new Student();
student.setId(i);
student.setName("student_" + i);
student.setSex(i % 2 + "");
students.add(student);
}
return students;
}
// 读入写入数据库
@Test
public void test11(){
String path = "D:\\ztl\\java\\excel\\user.xls";
// 读入构造器:文件位置,读取成哪个Java对象,读取监听器
ExcelReader excelReader = EasyExcel.read(path, TUser.class, new EasyExcelListener()).build();
ReadSheet readSheet = EasyExcel.readSheet(0).build();
excelReader.read(readSheet);
excelReader.finish();
}
// 编写监听器,继承 AnalysisEventListener 类并指定读取java对象
public class EasyExcelListener extends AnalysisEventListener<TUser> {
// 逐行读取 excel 信息
public void invoke(TUser tUser, AnalysisContext analysisContext) {
System.out.println(tUser);
// 此处可以执行插入操作
}
// excel 读取完毕触发
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("finsh");
}
}
// 当多个类型需要导入时,觉得创建多个监听器麻烦可以使用泛型监听器
public class CommonEasyExcelListener<T> extends AnalysisEventListener<T> {
public void invoke(T t, AnalysisContext analysisContext) {
System.out.println(t);
}
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("finsh");
}
}
// 读取多条sheet
@Test
public void test22() {
String path = "D:\\ztl\\java\\excel\\user.xls";
ExcelReader excelReader = EasyExcel.read(path, TUser.class, new EasyExcelListener()).build();
List<ReadSheet> sheets = excelReader.excelExecutor().sheetList();
for (ReadSheet sheet : sheets) {
String sheetName = sheet.getSheetName();
System.out.println(sheetName);
ReadSheet readSheet = EasyExcel.readSheet(sheetName).build();
excelReader.read(readSheet);
}
excelReader.finish();
}
注意:excel的读取或写入完毕后,需要调用 finish 方法