使用Easyexcel处理大批量数据
1、简介
Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析依然需要100M左右内存,改用easyexcel可以降低到几M,并且再大的excel也不会出现内存溢出;03版依赖POI的sax模式,在上层做了模型转换的封装,让使用者更加简单方便
2、依赖注入
<!--easyexcel,推荐使用2.0 以上版本,功能更加完善-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.0.5</version>
</dependency>
3、实体类
- @ExcelProperty(value = "表头名",index = 列下标)
注:不建议index和name同时使用,要么一个对象只用index,要么一个对象只用name去匹配用名字去匹配,这里需要注意一个问题:如果名字重复,会导致只有一个字段读取到数据。此时可以使用
- @TableField:用来指定属性对应的字段名
package com.itheima.entity;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
/**
* 商品表
* @TableName tb_sku
*/
@TableName(value ="tb_sku")
@Data
public class Sku implements Serializable {
/**
*
*/
@TableId
private Long skuId;
/**
* 商品名称
*/
//@ExcelProperty(index = 0)
private String skuName;
/**
* 商品图片
*/
//@ExcelProperty(index = 1)
private String skuImage;
/**
* 基础价格
*/
//@ExcelProperty(index = 2)
private Integer price;
/**
* 商品类别Id
*/
//@ExcelProperty(index = 3)
private Integer classId;
/**
* 是否打折促销
*/
//@ExcelProperty(index = 4)
private Integer isDiscount;
/**
* 净含量
*/
//@ExcelProperty(index = 5)
private String unit;
/**
*
*/
//@ExcelProperty(index = 6)
private String brandName;
/**
* 创建时间
*/
private Date createTime;
/**
* 修改时间
*/
private Date updateTime;
// @TableField(exist = false)
// private SkuClass skuClass;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
Sku other = (Sku) that;
return (this.getSkuId() == null ? other.getSkuId() == null : this.getSkuId().equals(other.getSkuId()))
&& (this.getSkuImage() == null ? other.getSkuImage() == null : this.getSkuImage().equals(other.getSkuImage()))
&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
&& (this.getUnit() == null ? other.getUnit() == null : this.getUnit().equals(other.getUnit()))
&& (this.getSkuName() == null ? other.getSkuName() == null : this.getSkuName().equals(other.getSkuName()))
&& (this.getPrice() == null ? other.getPrice() == null : this.getPrice().equals(other.getPrice()))
&& (this.getClassId() == null ? other.getClassId() == null : this.getClassId().equals(other.getClassId()))
&& (this.getIsDiscount() == null ? other.getIsDiscount() == null : this.getIsDiscount().equals(other.getIsDiscount()))
&& (this.getBrandName() == null ? other.getBrandName() == null : this.getBrandName().equals(other.getBrandName()))
&& (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getSkuId() == null) ? 0 : getSkuId().hashCode());
result = prime * result + ((getSkuImage() == null) ? 0 : getSkuImage().hashCode());
result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
result = prime * result + ((getUnit() == null) ? 0 : getUnit().hashCode());
result = prime * result + ((getSkuName() == null) ? 0 : getSkuName().hashCode());
result = prime * result + ((getPrice() == null) ? 0 : getPrice().hashCode());
result = prime * result + ((getClassId() == null) ? 0 : getClassId().hashCode());
result = prime * result + ((getIsDiscount() == null) ? 0 : getIsDiscount().hashCode());
result = prime * result + ((getBrandName() == null) ? 0 : getBrandName().hashCode());
result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", skuId=").append(skuId);
sb.append(", skuImage=").append(skuImage);
sb.append(", createTime=").append(createTime);
sb.append(", unit=").append(unit);
sb.append(", skuName=").append(skuName);
sb.append(", price=").append(price);
sb.append(", classId=").append(classId);
sb.append(", isDiscount=").append(isDiscount);
sb.append(", brandName=").append(brandName);
sb.append(", updateTime=").append(updateTime);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
4、controller层
//商品导入功能
@Override
public boolean saveSubject(MultipartFile fileName, SkuService skuService) {
try {
//获取文件的输入流
InputStream inputStream = fileName.getInputStream();
/**
* 读取公式和单元格类型
*
* 1. 创建excel对应的实体对象 参照{@link CellDataReadDemoData}
* 2. 由于默认一行行的读取excel,所以需要创建excel一行一行的回调监听器,参照{@link DemoHeadDataListener}
* <p>
* 3. 直接读即可
*
*/
//调用方法进行读取,需要文件流,列的实体类的class对象,监听器(需要传入eduSubjectService才可以调用底层进行数据库操作),sheet表示表格的下面,
EasyExcel.read(inputStream, Sku.class, new ExcelListener()).sheet().doRead();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
5、监听器listener
| 名称 | 说明 |
|---|---|
| AnalysisEventListener |
分析事件侦听器:接收解析的每条数据的返回 |
| SyncReadListener | 同步读取侦听器 |
| AbstractIgnoreExceptionReadListener | 抽象忽略异常读取侦听器 |
| ModelBuildEventListener | 模型构建事件侦听器 |
package com.itheima.config;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.dao.SkuDao;
import com.itheima.entity.Sku;
import com.itheima.service.SkuService;
import com.itheima.service.impl.SkuServiceImpl;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 监听excel的类
*
* @Name ExcelListener
* @Author b'f
* @Date 2022-08-13 17:27
*/
@Slf4j
public class ExcelListener extends AnalysisEventListener<Sku> {
/**
* 定义一个存储的界限,每读取5条数据就存储一次数据库,防止数据过多时发生溢出
* 存储完成之后就清空list重新读取新的数据,方便内存回收
*/
@Autowired
private SkuDao skuDao;
private SkuService skuService;
private List<Sku> skuList = new ArrayList<>();
public ExcelListener() {
}
public ExcelListener(SkuService skuService) {
this.skuService = skuService;
}
/**
* 批处理阈值2000
*/
private static final int BATCH_COUNT = 2000;
// 一行行读取excel内容,然后用MybatisPlus的方法进行导入
@Override
public void invoke(Sku sku, AnalysisContext analysisContext) {
//判断添加的商品是否重复
LambdaQueryWrapper<Sku> lqw = new LambdaQueryWrapper<>();
Sku sku1 = skuDao.selectOne(lqw);
if (sku1 != null) {
return;
}
skuList.add(sku);
if (skuList.size() >= BATCH_COUNT) {
skuService.saveBatch(skuList);
skuList.clear();
}
}
// 读取表头内容,导出可用到
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
System.out.println("表头:" + headMap);
}
// 读取完成之后进行的操作
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
skuService.saveBatch(skuList);
}
}
AnalysisEventListener
所有已实现的接口:Listener、ReadListener
直接已知子类:SyncReadListener
| 方法 | 返回值类型 | 说明 |
|---|---|---|
| hasNext | boolean | 验证是否有另一条数据。您可以通过返回 false 来停止读取 |
| invokeHead | void | 分析第一行时触发调用函数 |
| invokeHeadMap | void | 以map的形式放回表头,覆盖当前方法以接收表头数据 |
| onException | void | 当任何一个监听器进行错误报告时,所有监听器都会收到此方法 |
SyncReadListener
所有已实现的接口:Listener、ReadListener
直接已知父类:AnalysisEventListener
| 方法 | 返回值类型 | 说明 |
|---|---|---|
| doAfterAllAnalysed | void | 如果有什么操作在全部分析结束后执行 |
| getList | List | |
| invoke | void | 当分析一行触发器调用函数 |
| setList(List | void |

浙公网安备 33010602011771号