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

相关推荐
optimistic_chen11 分钟前
【Java EE进阶 --- SpringBoot】Mybatis - plus 操作数据库
数据库·spring boot·笔记·java-ee·mybatis·mybatis-plus
来旺1 小时前
互联网大厂Java面试全解析及三轮问答专项
java·数据库·spring boot·安全·缓存·微服务·面试
摇滚侠1 小时前
Spring Boot 3零基础教程,yml文件中配置和类的属性绑定,笔记15
spring boot·redis·笔记
thginWalker1 小时前
使用Spring Boot构建消息通信层
spring boot
lang201509281 小时前
Spring Boot 外部化配置最佳实践指南
java·spring boot
摇滚侠1 小时前
Spring Boot 3零基础教程,WEB 开发 HTTP 缓存机制 笔记29
spring boot·笔记·缓存
Knight_AL2 小时前
Spring Boot 中使用自定义注解和 AOP 实现微服务日志记录(包含 URL、状态码和耗时信息)
linux·spring boot·微服务
Q_Q19632884752 小时前
python+vue的在线租房 房屋租赁系统
开发语言·vue.js·spring boot·python·django·flask·node.js
摇滚侠2 小时前
Spring Boot 3零基础教程,WEB 开发 内容协商 接口返回 YAML 格式的数据 笔记35
spring boot·笔记·后端
java水泥工3 小时前
旅游管理系统|基于SpringBoot和Vue的旅游管理系统(源码+数据库+文档)
spring boot·vue·计算机毕业设计·java毕业设计·旅游管理系统