谷粒 | 12 |easyExcel使用

一、引入easyexcel依赖

 <!--easyExcel依赖-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
        </dependency>

二、根据excel数据格式创建实体类

@Data
public class SubjectData {

    @ExcelProperty(value = "一级标题",index = 0) //easyexcel注释,列名和列索引值
    private String firstLevelSubject;

    @ExcelProperty(value = "二级标题",index = 1)
    private String secondLevelSubject;
}

三、控制层和业务层接口

//添加课程分类
    //获取上传过来的文件,把文件内容读取出来
    @PostMapping("addSubject")
    @ApiOperation(value = "上传并读取excel表")
    public Result addSubject(MultipartFile file) {
        //上传过来excel文件
        subjectService.saveSubject(file,subjectService);
        return Result.ok();
    }
public interface EduSubjectService extends IService<EduSubject> {

     //添加课程分类
     void saveSubject(MultipartFile file,EduSubjectService subjectService);

     List<FirstLevelSubject> getFirstAndSecondSubjects();
}`
@Override
public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
    try {
        //获取文件输入流
        InputStream inputStream = file.getInputStream();
EasyExcel.read(inputStream, SubjectData.class,new 						           SubjectExcelListener(subjectService)).sheet().doRead();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

四、监听器

public class SubjectExcelListener extends AnalysisEventListener {

    //因为SubjectExcelListener不交给spring进行管理,不能注入其他对象,无法操作数据库
    public EduSubjectService subjectService;

    public SubjectExcelListener() {
    }

    public SubjectExcelListener(EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }

    @Override
    public void invoke(Object data, AnalysisContext context) {
        SubjectData subjectData = (SubjectData) data;
        if (data == null) {
            throw new MyException(20001, "文件数据为空");
        }

        //一行一行读取,每次读取两个值,第一个值一级分类,第二个值二级分类
        //判断一级分类是否重复,一级分类不能重复添加
        EduSubject existOneSubject = existFirstLevelSubject(subjectService, subjectData.getFirstLevelSubject());
        if (existOneSubject == null) {
            existOneSubject = new EduSubject();
            existOneSubject.setParentId("0");
            existOneSubject.setTitle(subjectData.getFirstLevelSubject());
            subjectService.save(existOneSubject);
        }
        String pid = existOneSubject.getId();

        //判断二级分类是否重复
        EduSubject existSecondSubject = existSecondLevelSubject(subjectService, subjectData.getSecondLevelSubject(), pid);
        if (existSecondSubject == null) {
            existSecondSubject = new EduSubject();
            existSecondSubject.setParentId(pid);
            existSecondSubject.setTitle(subjectData.getSecondLevelSubject());
            subjectService.save(existSecondSubject);
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    //判断一级分类不能重复添加
    private EduSubject existFirstLevelSubject(EduSubjectService subjectService, String name) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", name);
        wrapper.eq("parent_id", "0");
        EduSubject firstSubject = subjectService.getOne(wrapper);
        return firstSubject;
    }

    //判断二级分类是否重复
    private EduSubject existSecondLevelSubject(EduSubjectService subjectService, String name, String pid) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", name);
        wrapper.eq("parent_id", pid);
        EduSubject secondSubject = subjectService.getOne(wrapper);
        return secondSubject;
    }
}
上一篇:easyExcel用代码生成的模板填充时变量无法被替换


下一篇:记录一次EasyExcel的使用