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,或者多个文件的。

相关推荐
anzhxu2 小时前
SpringBoot 3.x 整合swagger
java·spring boot·后端
小江的记录本2 小时前
【Bean】JavaBean(原生规范)/ Spring Bean 【重点】/ 企业级Bean(EJB/Jakarta Bean)
java·数据库·spring boot·后端·spring·spring cloud·mybatis
中国胖子风清扬2 小时前
Camunda 8 概念详解:梳理新一代工作流引擎的核心概念与组件
java·spring boot·后端·spring cloud·ai·云原生·spring webflux
yhole2 小时前
Spring Boot整合Redisson的两种方式
java·spring boot·后端
sthnyph2 小时前
Spring Boot 集成 Kettle
java·spring boot·后端
殷紫川3 小时前
吃透 Spring Boot 3 + Spring Cloud 云原生新特性
spring boot·spring cloud·架构
bearpping3 小时前
Spring Boot 整合 MyBatis 与 PostgreSQL 实战指南
spring boot·postgresql·mybatis
mygljx3 小时前
Spring Boot从0到1 -day02
java·spring boot·后端
SuniaWang3 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题八:《RAG 系统安全与权限管理:企业级数据保护方案》
java·前端·人工智能·spring boot·后端·spring·架构
不吃香菜学java4 小时前
苍穹外卖-菜品分页查询
数据库·spring boot·tomcat·log4j·maven·mybatis