SpringBoot中使用easyExcel读取excel文件到数据库中
1、数据库中表的结构如下2、excel中的数据如下3、定义一个与excel表中对应的实体类@Datapublic class SubjectData {@ExcelProperty(index = 0)private String oneSubjectName;@ExcelProperty(index = 1)private String twoSubjectName;}4、controller@
·
1、数据库中表的结构如下
2、excel中的数据如下
3、定义一个与excel表中对应的实体类
@Data
public class SubjectData {
@ExcelProperty(index = 0)
private String oneSubjectName;
@ExcelProperty(index = 1)
private String twoSubjectName;
}
4、controller
@RestController
public class EduSubjectController {
@Autowired
EduSubjectService eduSubjectService;
// 获取上传的Excel文件,从文件中读取数据保存到数据库中
@PostMapping("addSubject")
public R addSubject(MultipartFile file){
eduSubjectService.addSubject(file,eduSubjectService);
return R.ok();
}
}
5、service
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
@Override
public void addSubject(MultipartFile file, EduSubjectService eduSubjectService) {
//文件输入流
InputStream in = null;
try {
in = file.getInputStream();
//调用方法进行读取,通过带参数的构造器将spring容器中的EduSubjectService对象传入到监听器中
EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
} catch (IOException e) {
e.printStackTrace();
}
}
}
6、编写excel的listener
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {
//因为SubjectExcelListener不能交给spring进行管理,需要自己new,不能注入其他对象
//不能实现数据库操作
private EduSubjectService eduSubjectService;
public SubjectExcelListener() {
}
public SubjectExcelListener(EduSubjectService eduSubjectService) {
this.eduSubjectService = eduSubjectService;
}
//读取excel内容,一行一行进行读取,不会读取excel中的表头数据
@Override
public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
if(subjectData==null){
throw new MyException(20001,"文件中的数据为空!");
}
//一行一行读取,每次读取有两个值,第一个值一级分类,第二个值二级分类
//判断一级分类是否重复
// 判断数据库中是否存在一级分类,如果不存在则添加到数据库
EduSubject eduSubject = existOneSubject(eduSubjectService, subjectData.getOneSubjectName());
if(eduSubject==null){
eduSubject=new EduSubject();
eduSubject.setParentId("0");
eduSubject.setTitle(subjectData.getOneSubjectName());
eduSubjectService.save(eduSubject);
}
// 如果一级分类在数据库中存在则获取一级分类的id作为二级分类的parent_id
String pid=eduSubject.getId();
EduSubject eduSubjectTwo = existTwoSubject(eduSubjectService, subjectData.getTwoSubjectName(), pid);
if(eduSubjectTwo==null){
eduSubjectTwo=new EduSubject();
eduSubjectTwo.setParentId(pid);
eduSubjectTwo.setTitle(subjectData.getTwoSubjectName());
eduSubjectService.save(eduSubjectTwo);
}
}
//判断一级分类不能重复添加
public EduSubject existOneSubject(EduSubjectService eduSubjectService,String subjectName){
QueryWrapper<EduSubject> eduSubjectQueryWrapper = new QueryWrapper<>();
eduSubjectQueryWrapper.eq("title",subjectName);
eduSubjectQueryWrapper.eq("parent_id","0");
EduSubject one = eduSubjectService.getOne(eduSubjectQueryWrapper);
return one;
}
//判断二级分类不能重复添加
public EduSubject existTwoSubject(EduSubjectService eduSubjectService,String subjectName,String pid){
QueryWrapper<EduSubject> eduSubjectQueryWrapper = new QueryWrapper<>();
eduSubjectQueryWrapper.eq("title",subjectName);
eduSubjectQueryWrapper.eq("parent_id",pid);
EduSubject two = eduSubjectService.getOne(eduSubjectQueryWrapper);
return two;
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)