EasyExcel 简单导入

前边写过使用easyexcel进行简单、多sheet页的导出。今天周日利用空闲写一下对应简单的导入。

重点:springboot、easyExcel、桥接模式;

说明:本次使用实体类student:属性看前边章节内容;

1、公共导入service

java 复制代码
public interface ExcelImportCommonService {

    /**
     * 获取类型: 如果是导入多种 sheet页/文件,可以根据这个 与 sheet页名/文件名比对,决定用哪个 serviceImpl
     * @return
     */
    String getType();


    /**
     * 保存数据
     */
    void save();

    /**
     * 获取 对应 实体类
     * @return
     */
    Class<?> getEntityClazz();

    /**
     * 数据解析:一条一条解析的
     * @param o
     */
    void invoke(Object o);
}

学生信息 实现类:

java 复制代码
@Service
public class ExcelImportStudentServiceImpl implements ExcelImportCommonService {

    /**
     * TODO: 实际项目 引入 dao 保存 数据
     *
     */

    private List<Student> dataList = new ArrayList<>();

    @Override
    public String getType() {
        return "学生信息表";
    }

    @Override
    public void save() {
        // TODO  使用 dao 保存数据: dataList
        System.out.println("保存的数据是:" + Arrays.toString(dataList.toArray()));
        System.out.println(getType()+",保存数据成功!");
    }

    @Override
    public void invoke(Object o) {
        //1、数据转换
        Student student = (Student) o;
        //2、去重:根据 特定字段 进行去重(可以是本次导入的数据,也可以是以前库里有的 做对比)
        //  举例:  如果 一次导入中  学生名 有重复的,就不再插入
        List<Student> repeat = dataList.stream()
                .filter(student1 -> student1.getSName().equals(student.getSName()))
                .collect(Collectors.toList());
        if(repeat.isEmpty()) {
            dataList.add(student);
        }
    }

    @Override
    public Class<?> getEntityClazz() {
        return Student.class;
    }
}

2、extends监听器

监听器内引入:公共导入service (桥接模式的使用)

java 复制代码
public class ExcelImportCommonListening extends AnalysisEventListener {


    // 使用了桥接模式: 抽象类  与  实现类 解耦: 在该类里引用 导入公共service接口
    private ExcelImportCommonService commonService;

    // 监听器中不能 使用 @Autowired 导入,这里使用 构造器
    public ExcelImportCommonListening(ExcelImportCommonService commonService) {
        this.commonService = commonService;
    }

    @Override
    public void invoke(Object o, AnalysisContext analysisContext) {
        // 1、可以先做一些通用解析
        // 2、数据做一条一条具体的解析
        commonService.invoke(o);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 所有数据解析完成之后的操作
        commonService.save();
    }
}

3、学生信息导入 controller

java 复制代码
@RestController
public class ExcelImport {

    @Resource
    private ExcelImportStudentServiceImpl studentService;

    /**
     * 导入学生信息 excel
     */
    @PostMapping(value = "importStudentExcel")
    public String importStudentExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), studentService.getEntityClazz(), new ExcelImportCommonListening(studentService))
                    .sheet().doRead();
        } catch (IOException e) {
            System.out.println("导入学生信息excel异常:"+e);
            return "no";
        }
        return "yes";
    }
}

4、postman测试

示例excel就是上篇文章执行代码导出的,这里直接导入该文件(单sheet页)。

后续补充下一次导入多个sheet,或者多个文件的。

相关推荐
刘一说40 分钟前
深入理解 Spring Boot 中的数据库迁移:Flyway 与 Liquibase 实战指南
数据库·spring boot·oracle
一叶飘零_sweeeet1 小时前
SpringBoot 集成 RabbitMQ
spring boot·rabbitmq·java-rabbitmq
知兀2 小时前
【Spring/SpringBoot】<dependencyManagement> + import 导入能继承父maven项目的所有依赖,类似parent
spring boot·spring·maven
郝开3 小时前
Spring Boot 2.7.18(最终 2.x 系列版本):版本概览;兼容性与支持;升级建议;脚手架工程搭建
java·spring boot·后端
清水4 小时前
Spring Boot企业级开发入门
java·spring boot·后端
Q_Q5110082854 小时前
python+django/flask的校园活动中心场地预约系统
spring boot·python·django·flask·node.js·php
水冗水孚5 小时前
类比前端知识来学习Java的Spring Boot实现MySql的全栈CRUD功能——搭配Svelte+Vite
spring boot·svelte
淘源码d6 小时前
什么是医院随访系统?成熟在用的智慧随访系统源码
java·spring boot·后端·开源·源码·随访系统·随访系统框架
Q_Q19632884757 小时前
python+django/flask基于机器学习的就业岗位推荐系统
spring boot·python·django·flask·node.js·php
ArabySide7 小时前
【Spring Boot】REST与RESTful详解,基于Spring Boot的RESTful API实现
spring boot·后端·restful