直接上代码
实体类;
@ExcelProperty(value = "表格名称" ,index = 0)
value就是第几列的表格的名称 index就是第几列;
//实体类
@Data
@TableName("element_info")
public class ElementInfo implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
* 元素名称
*/
@TableField("element_name")
@ExcelProperty(value = "元素名称", index = 0)
private String elementName;
/**
* 元素图标id
*/
@TableField("ico_id")
private Integer icoId;
/**
* 元素类型id
*/
@TableField("type_id")
private Integer typeId;
/**
* 经度
*/
@TableField("longitude")
@ExcelProperty(value = "经度", index = 1)
private Double longitude;
/**
* 纬度
*/
@TableField("latitude")
@ExcelProperty(value = "纬度", index = 2)
private Double latitude;
/**
* create_time
*/
@TableField("create_time")
private Date createTime;
/**
* 修改时间
*/
@TableField("update_time")
private Date updateTime;
}
Controller层
//Controller层
@PostMapping("/upload")
public Result InfoImport(@RequestParam("file") MultipartFile file){
return elementInfoService.ReadExcel(file);
}
Service层
说明:
headRowNumber(1) 从哪一行开始读 因为这里涉及到表头 1就是从第一行开始读。
EasyExcelListenr是拦截器 请看下面
//serviceImpl
public Result ReadExcel(MultipartFile file) {
try {
EasyExcel.read(file.getInputStream(),ElementInfo.class,new EasyExcelListener()).sheet().
headRowNumber(1).doRead();
} catch (IOException e) {
throw new RuntimeException(e);
}
return Result.ok();
}
拦截器
//拦截器
@Component
public class EasyExcelListener extends AnalysisEventListener<ElementInfo> {
//Service 使用静态注入
private static ElementInfoService elementInfoService;
//设置最大数
private static final int BATCH_COUNT = 100;
//临时缓存List
public static List<ElementInfo> importList = new ArrayList<>();
public EasyExcelListener (){
}
//注入service
@Autowired
public void setElementInfoService(ElementInfoService elementInfoService){
EasyExcelListener.elementInfoService = elementInfoService;
}
//每一次解析
@Override
public void invoke(ElementInfo data, AnalysisContext context) {
//这里进行判断 如果为空就不进行解析
if (data.getElementName() != null && data.getLongitude() != null && data.getLatitude() != null){
log.info("解析到的一条数据: excelRow = {}", data);
//这里使用了mybatis-plus 判断当前解析到的数据 数据库是否存在 不存在则放进临时缓存List
QueryWrapper<ElementInfo> wrapper = new QueryWrapper<>();
wrapper.eq("element_name",data.getElementName());
List<ElementInfo> list = elementInfoService.list(wrapper);
if (list.size() == 0){
importList.add(data);
}
}
//判断当前缓存List的size是否大于最大数 如果是则进行一次存储
if (importList.size() >= BATCH_COUNT){
elementInfoService.saveBatch(importList);
importList.clear();
}
}
//解析完毕需要做的功能 这里是解析完毕存入数据库 清空当前List
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
log.info("解析的所有数据 list = {}", importList);
if (importList.size() > 0){
elementInfoService.saveBatch(importList);
importList.clear();
}
}
//解析表头
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
log.info("表头数据 excelHead= {}", headMap);
}
//处理异常
@Override
public void onException(Exception exception, AnalysisContext context) {
if (exception instanceof ExcelDataConvertException) {
ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
log.error("第{}行,第{}列解析异常", excelDataConvertException.getRowIndex() + 1,
excelDataConvertException.getColumnIndex() + 1);
}
}
}
到这里你可以进行Ctrl+C和Ctrl+V了。