EasyExcel是一个基于Java的、快速、简洁、解决大文件内存溢出的Excel处理工具。
它能让你在不用考虑性能、内存的等因素的情况下,快速完成Excel的读、写等功能。EasyExcel是在尽可能节约内存的情况下支持读写百M的Excel。
实现Springboot结合EasyExcel实现对Excel中数据的读取,并且将读取的数据通过Mybatis-plus保存到Mysql数据库。
org.springframework.boot spring-boot-starter-web mysql mysql-connector-java com.baomidou mybatis-plus-boot-starter 3.4.2 org.projectlombok lombok com.alibaba easyexcel 3.1.1 com.alibaba druid-spring-boot-starter 1.2.8 com.alibaba fastjson 1.2.76
由于用到MyBatisplus,所以一定不要忘记加下面的这段代码,否则你的mapper是编译不到你的classpath中的。
src/main/java **/*.xml
server: port: 80 spring: datasource: druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/redhorse?serverTimezone=UTC username: root password: root mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
既然要读取Excel,同时存入数据库,那么就必然需要对应的表,以及表对应的实体类,而Excel也需要对应的实体类。因为Excel表格会增加一些不必要的字段,而这些字段并不需要存入数据库中,同理数据库实体类同样存在一些字段不是从表格中获取。
package com.atorientsec.entities;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.format.NumberFormat;
import lombok.Data;
@Data
public class ExcelAttdnOver {
@ExcelProperty(index =2)
private String department;
@ExcelProperty(index = 0)
private String name;
/**
* 这里用String去接日期,才能格式化,接收年月日的格式
*/
@ExcelProperty(index = 3)
@DateTimeFormat(value = "yyyy-MM-dd")
private String overDate;
@ExcelProperty(index = 4)
private Double overHours;
/**
*接收百分比的数字
*/
@ExcelProperty(index = 7)
@NumberFormat("#.##%")
private String rate;
}
package com.atorientsec.entities;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
@Data
public class AttdnOver {
@TableId(type = IdType.AUTO)
private Integer id;
private String department;
private String name;
private String attdnMonth;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date overDate;
private Double overHours;
private Double rate;
}
注释:数据库实体类的overDate是Date类型,而Excel对应的类中overDate是String类型,只有String去接日期才能格式化。
package com.atorientsec.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.atorientsec.entities.AttdnOver;
import com.atorientsec.entities.ExcelAttdnOver;
import com.atorientsec.service.AttdnOverService;
import lombok.extern.slf4j.Slf4j;
import java.text.SimpleDateFormat;
import java.util.*;
@Slf4j
public class AttdnDataListener implements ReadListener {
/**
* Excel模板的读取类
* 有个很重要的点,AttdnDataListener不能被Spring管理
* 要每次读取excel都要new,然后里面用到spring可以构造方法传进去
* @param excelAttdnOver
* @param analysisContext
*/
/**
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
*/
private static final int BATCH_COUNT = 100;
private int count = 0;
/**
* 缓存的数据,List
*/
private List cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
private AttdnOverService attdnOverService;
private String month;
public AttdnDataListener(){
}
/**
* 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
*
*/
public AttdnDataListener(AttdnOverService attdnOverService,String month){
this.attdnOverService = attdnOverService;
this.month = month;
}
/**
* 这个每一条数据解析都会来调用
*
*/
@Override
public void invoke(ExcelAttdnOver excelAttdnOver, AnalysisContext analysisContext) {
log.info("解析到第 {} 条数据:{}", (++count), JSON.toJSONString(excelAttdnOver));
try {
//把表格对应的实体类对象转化成数据库表对应的对象
AttdnOver attdnOver = new AttdnOver();
attdnOver.setDepartment(excelAttdnOver.getDepartment());
attdnOver.setName(excelAttdnOver.getName());
attdnOver.setAttdnMonth(this.month);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(excelAttdnOver.getOverDate());
attdnOver.setOverDate(date);
attdnOver.setOverHours(excelAttdnOver.getOverHours());
attdnOver.setRate(Double.parseDouble(excelAttdnOver.getRate().replace("%", "")));
cachedDataList.add(attdnOver);
if(cachedDataList.size()>=BATCH_COUNT){
saveData();
// 存储完成清理 list
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}
}catch (Exception e){
log.error(e.getMessage());
}
}
/**
* 接收表头信息
@Override
public void invokeHead(Map> headMap, AnalysisContext context) {
Map StringMap = ConverterUtils.convertToStringMap(headMap,context);
Set keySet = StringMap.keySet();
System.out.println("该Excel表头信息是:");
for(int i=0;i
package com.atorientsec.mapper;
import com.atorientsec.entities.AttdnOver;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface AttdnOverMapper extends BaseMapper {
}
package com.atorientsec.service;
import com.atorientsec.entities.AttdnOver;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
public interface AttdnOverService extends IService {
@Transactional
boolean batchSave(ArrayList attdnOverArrayList);
}
package com.atorientsec.service.impl;
import com.atorientsec.entities.AttdnOver;
import com.atorientsec.mapper.AttdnOverMapper;
import com.atorientsec.service.AttdnOverService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@Service
public class AttdnOverServiceImpl extends ServiceImpl implements AttdnOverService {
public boolean batchSave(ArrayList attdnOverArrayList){
//saveBatch是mybatisplus的批量插入方法
boolean status = saveBatch(attdnOverArrayList);
return status;
}
}
EasyExcel读文件
package com.atorientsec.controller;
import com.alibaba.excel.EasyExcel;
import com.atorientsec.entities.ExcelAttdnOver;
import com.atorientsec.listener.AttdnDataListener;
import com.atorientsec.service.AttdnOverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@RestController
@RequestMapping("/excel")
public class AttdnOverController {
@Autowired
private AttdnOverService attdnOverService;
@PostMapping("/upload/{month}")
public String upload(MultipartFile file, @PathVariable String month) throws IOException {
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
EasyExcel.read(file.getInputStream(), ExcelAttdnOver.class,new AttdnDataListener(attdnOverService,month))
.sheet().headRowNumber(1).doRead();
return "success";
}
}
注释:key=file,此处的file变量与Java代码的Controller中的MultipartFile file变量名保持一样,否则不起作用,读不到文件。
@PostMapping("/upload/{month}") public String upload(MultipartFile file, @PathVariable String month) {}