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

相关推荐
阿虎儿21 分钟前
Spring Boot 4常用依赖包解析与场景搭配
spring boot
Java面试题总结31 分钟前
Spring Boot 包扫描新姿势:AutoScan vs @Import vs @ComponentScan 深度对比
java·数据库·spring boot
却话巴山夜雨时i1 小时前
互联网大厂Java面试:从Spring Boot到Kafka的业务场景深度剖析
spring boot·redis·spring cloud·微服务·kafka·prometheus·java面试
米糕闯编程2 小时前
IDEA新建springboot项目
spring boot·后端·intellij-idea
我登哥MVP2 小时前
【Spring6笔记】 - 15 - Spring中的八大设计模式
java·spring boot·笔记·spring·设计模式·intellij-idea
我登哥MVP2 小时前
【SpringMVC笔记】 - 1 - SpringMVC入门
java·spring boot·spring·tomcat·maven·intellij-idea·springmvc
斌味代码2 小时前
SpringBoot 实战总结:踩坑与解决方案全记录
java·spring boot·后端
0xDevNull2 小时前
Spring Boot 3.0动态多数据源切换实战教程
java·spring boot·后端
神龙斗士2402 小时前
第一个Spring Boot程序
java·spring boot·java-ee·tomcat
gelald2 小时前
Spring Boot - 配置加载
java·spring boot·后端·spring