0.导入依赖
<!-- EasyExcel 依赖开始-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.2.1</version>
</dependency>
<!-- EasyExcel 依赖结束-->
1.在controller中调用
2.实体类中内容,记得要在实体类中加入 MultipartFile 类型的属性,这个就是表格
3.监听器内容
package net.yierde.essay.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import net.yierde.essay.base.domain.EssayBaseArea;
import net.yierde.essay.base.service.IEssayBaseAreaService;
import net.yierde.essay.domain.EssaySchool;
import net.yierde.essay.domain.EssayTeacher;
import net.yierde.essay.mapper.EssaySchoolMapper;
import net.yierde.essay.service.IEssaySchoolService;
import net.yierde.essay.service.IEssayTeacherService;
import net.yierde.essay.system.service.ISysUserService;
import net.yierde.essay.utils.SecurityUtils;
import net.yierde.techkit.cloud.common.core.domain.entity.SysUser;
import net.yierde.techkit.cloud.common.core.utils.RegularUtil;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
/**
* 导入学校信息
* 这是监听器,只能通过构造方法来加入,你所需要的接口方法,mapper接口、
* 实现了ReadListener接口
* 重写了invoke 方法,在这个里面进行加入数据操作
*/
public class EssaySchoolReadListener implements ReadListener<EssaySchool> {
private EssaySchool essaySchool;
private IEssaySchoolService essaySchoolService;
private IEssayTeacherService essayTeacherService;
private Pattern pattern = Pattern.compile(RegularUtil.REGEXP_MOBILETEL);
private ISysUserService userService;
private IEssayBaseAreaService baseAreaService;
private EssaySchoolMapper essaySchoolMapper;
private int effect;
public EssaySchoolReadListener(EssaySchool essaySchool, IEssayTeacherService essayTeacherService, ISysUserService userService, IEssayBaseAreaService baseAreaService, EssaySchoolMapper essaySchoolMapper) {
this.essaySchool = essaySchool;
this.essayTeacherService = essayTeacherService;
this.userService = userService;
this.baseAreaService = baseAreaService;
this.essaySchoolMapper = essaySchoolMapper;
}
public int getEffect() {
return effect;
}
public void setEffect(int effect) {
this.effect = effect;
}
@Override
public void invoke(EssaySchool school, AnalysisContext context) {
// 校验数据
checkSchool(school);
// 加入省市县ID
school.setProvinceId(AreaID(school.getProvinceName(),0L));
school.setCityId(AreaID(school.getCityName(),school.getProvinceId()));
school.setCountyId(AreaID(school.getCountyName(),school.getCityId()));
// 学段中中文逗号 转成英文逗号
school.setPeriod(school.getPeriod().replace(",",","));
try{
SecurityUtils.initInsert(school);
effect+= essaySchoolMapper.insertEssaySchool(school);
// 如果有电话号码是否 加入系统用户
}catch (Exception e){
e.printStackTrace();
throw new RuntimeException("请检查数据是否正确");
}
}
/**
* 校验数据
* 1.导入数据电话存在 校验电话号码 是否重复
*/
private void checkSchool(EssaySchool school) {
//根据需要加入自己的校验
}
/**
* 判断教师模版导入 省市区县名称 关系是否对应
*/
public boolean isAreaRight(EssaySchool school){
//根据需要加入自己的需求
}
/**
* 根据省市县名称 获取 省市县所对应的 省市县的ID
*/
public Long AreaID(String areaName,long pid){
//根据需要加入自己的需求
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
}