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

相关推荐
用户3521802454753 天前
当 Prompt 学会"热更新":Spring Boot × Nacos3 AI 实战
java·spring boot·ai编程
昵称为空C3 天前
手撸一个动态 SQL 执行引擎:不重启服务,在线增删改查任意数据库
spring boot·后端
霸道流氓气质4 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
于先生吖4 天前
SpringBoot对接大模型开发AI命理测算系统:八字排盘与AI解析接口源码全解
人工智能·spring boot·后端
Flittly4 天前
【AgentScope Java新手村系列】(10)实战-多Agent天气助手
java·spring boot·spring
星落zx4 天前
Spring Boot 多模型集成:优雅调用全球主流大模型
人工智能·spring boot·chatgpt
一杯奶茶¥4 天前
水果销售网站 CRM客户信息管理系统 超市管理系 酒店管理系统 健身房管理系统 在线音乐网站 校园招聘系统
java·vue.js·spring boot·mysql·spring·java项目
进阶的小名4 天前
Spring Boot SSE + Nginx 配置:解决 EventSource 不实时返回、连接超时、流式响应被缓冲问题
spring boot·后端·nginx
我登哥MVP4 天前
SpringCloud Alibaba 核心组件解析:服务链路追踪
java·spring boot·后端·spring·spring cloud·java-ee·maven