1.引入依赖
之前测试EasyExcel已经引入过了
2.利用代码生成器生成结构
我们做的只需要更改代码生成器的数据库表即可
3.运行代码生成器
4.书写代码
1.controller
@RestController @CrossOrigin @RequestMapping("/eduservice/edu-subject") public class EduSubjectController { @Autowired private EduSubjectService eduSubjectService; //添加课程分类 //获取上传的文件,把文件内容读出来 @PostMapping("addSubject") public R addSubject(MultipartFile file){ eduSubjectService.save(file); return R.OK(); } }
2.service
@Data public class SubjectData { @ExcelProperty(index=0) private String oneSubjectName; @ExcelProperty(index=1) private String secondSubjectName; }
public interface EduSubjectService extends IService<EduSubject> { //添加课程分类 void save(MultipartFile file); }
3.serviceImpl
@Service public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService { @Override public void save(MultipartFile file) { try { InputStream inputStream=file.getInputStream(); EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelsListener()).sheet().doRead(); }catch (Exception e){ e.printStackTrace(); } } }
4.Excel实体类
@Data public class SubjectData { @ExcelProperty(index=0) private String oneSubjectName; @ExcelProperty(index=1) private String secondSubjectName; }
5.
最后关于这个监听器,我们要注意一个问题,因为代码的原因我们没法把监听器交给Spring管理
所以要把eduSujectService传进来
5.书写监听器
public class SubjectExcelsListener extends AnalysisEventListener<SubjectData> { public EduSubjectService subjectService; public SubjectExcelsListener(){} public SubjectExcelsListener(EduSubjectService subjectService){ this.subjectService=subjectService; } //一行一行去读取excle内容 @Override public void invoke(SubjectData subjectData, AnalysisContext analysisContext) { if(subjectData == null) { throw new onlineEduException(20001,"添加失败"); } //添加一级分类 EduSubject existOneSubject = this.existOneSubject(subjectService,subjectData.getOneSubjectName()); if(existOneSubject == null) {//没有相同的 existOneSubject = new EduSubject(); existOneSubject.setTitle(subjectData.getOneSubjectName()); existOneSubject.setParentId("0"); subjectService.save(existOneSubject); } //获取一级分类id值 String pid = existOneSubject.getId(); //添加二级分类 EduSubject existTwoSubject = this.existTwoSubject(subjectService,subjectData.getSecondSubjectName(), pid); if(existTwoSubject == null) { existTwoSubject = new EduSubject(); existTwoSubject.setTitle(subjectData.getSecondSubjectName()); existTwoSubject.setParentId(pid); subjectService.save(existTwoSubject); } } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { } //判断一级分类是否重复 private EduSubject existOneSubject(EduSubjectService subjectService,String name) { QueryWrapper<EduSubject> wrapper = new QueryWrapper<>(); wrapper.eq("title",name); wrapper.eq("parent_id","0"); EduSubject eduSubject = subjectService.getOne(wrapper); return eduSubject; } //判断二级分类是否重复 private EduSubject existTwoSubject(EduSubjectService subjectService,String name,String pid) { QueryWrapper<EduSubject> wrapper = new QueryWrapper<>(); wrapper.eq("title",name); wrapper.eq("parent_id",pid); EduSubject eduSubject = subjectService.getOne(wrapper); return eduSubject; } }