【springboot】【easyexcel】excel文件读取

目录

pom.xml

xml 复制代码
<dependency>
   <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.1.1</version>
</dependency>

ExcelVo

字段映射

java 复制代码
@Data
public class ExcelVo {
    @ExcelProperty(value = "序号", index = 0)
    private Integer num;
    @ExcelProperty(value = "工号", index = 1)
    private String id;
    @ExcelProperty(value = "姓名", index = 2)
    private String username;
    ...
}

逐行读取并处理

controller:接收文件接口

java 复制代码
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public JsonResult upload(@RequestParam("file") MultipartFile file) {
     if (file.isEmpty()) {
         return false;
     }
     try {
         ExcelListener excelListener = new ExcelListener();
         EasyExcel.read(file.getInputStream(), ExcelVo.class, excelListener).sheet().doRead();
         return true;
     }catch (Exception e){
     }
 }

ExcelListener :逐行处理

java 复制代码
@Component
public class ExcelListener extends AnalysisEventListener<ExcelVo> {
    public static ExcelListener excelListener; //声明对象
    @PostConstruct //初始化
    public void init() {
        excelListener = this;
        excelListener.resultService = this.resultService;
    }
    @Autowired
    private ResultService resultService;
    
    @Override
    public void invoke(ExcelVo excelVo, AnalysisContext analysisContext) {
        excelListener.resultService.updateLevel(excelVo);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("excel读取完成");
    }
}

全部读取并处理

controller:接收文件接口

java 复制代码
@RequestMapping(value = "/upload", method = RequestMethod.POST)
    public JsonResult upload(@RequestParam("file") MultipartFile file, @RequestParam("city") String city, @RequestParam("username") String username) {
        if (file.isEmpty()) {
            throw new RuntimeException("文件上传失败");
        }
        try {
            ExcelListener excelListener = new ExcelListener();
            EasyExcel.read(file.getInputStream(), ExcelVo.class, excelListener).sheet().doRead();
            List<ExcelVo> list = excelListener.getList();
            namelistService.insertList(list, city, username);
            return JsonResult.ok(true);
        } catch (Exception e) {
        }
    }

ExcelListener :逐行存入list,再批量处理

java 复制代码
@Component
@Data
public class ExcelListener extends AnalysisEventListener<ExcelVo> {
    List<ExcelVo> list = new ArrayList<>();
    
    @Override
    public void invoke(ExcelVo excelVo, AnalysisContext analysisContext) {
        if (excelVo == null) {
            throw new RuntimeException("excel读取失败");
        }
        list.add(excelVo);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("excel读取完成");
    }
}

向ExcelListener 传参

controller:接收文件接口

java 复制代码
@RequestMapping(value = "/upload", method = RequestMethod.POST)
    public JsonResult upload(@RequestParam("file") MultipartFile file, @RequestParam("city") String city, @RequestParam("username") String username) {
        if (file.isEmpty()) {
            throw new RuntimeException("文件上传失败");
        }
        try {
            ExcelListener excelListener = new ExcelListener(namelistService, city, username);
            EasyExcel.read(file.getInputStream(), ExcelVo.class, excelListener).sheet().doRead();
            return true;
        } catch (Exception e) {
        }
    }

ExcelListener :通过构造函数重载,传入service层接口或其他参数

java 复制代码
@Component
@Data
public class ExcelListener extends AnalysisEventListener<ExcelVo> {
    private NamelistService namelistService;
    private String city;
    private String username;

    public ExcelListener() {
    }

    public ExcelListener(NamelistService namelistService) {
        this.namelistService = namelistService;
    }

    public ExcelListener(NamelistService namelistService, String city, String username) {
        this.namelistService = namelistService;
        this.city = city;
        this.username = username;
    }

    @Override
    public void invoke(ExcelVo excelVo, AnalysisContext analysisContext) {
        if (excelVo == null) {
            throw new RuntimeException("excel读取失败");
        }
        namelistService.insertOne(excelVo,city,username);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("excel读取完成");
    }
}
相关推荐
Smoothcloud润云17 分钟前
5大功能精修,重构AI算力使用体验!
java·人工智能·windows·算法·重构·编辑器·sublime text
我是唐青枫28 分钟前
Java MyBatis-Flex 实战指南:从 BaseMapper 到 QueryWrapper 的轻量 ORM 用法
java·开发语言·mybatis
顺风尿一寸34 分钟前
Java Native 方法底层原理深度解析:从 JNI 注册到 Native Wrapper 生成
java
极客先躯42 分钟前
高级java每日一道面试题-2026年01月18日-实战篇[Docker]-如何清理仓库中的旧镜像?
java·运维·docker·容器
iiiiyu1 小时前
IO流(二)
java·开发语言·数据结构·编程语言
白露与泡影1 小时前
牛客网大厂Java面试题全集(2026版,附答案)
java·开发语言
_Evan_Yao1 小时前
一文搞懂:Git分支管理与团队协作规范——从GitFlow到GitHub Flow,从rebase到merge,打造高效协作流
java·git·后端·github
未若君雅裁1 小时前
AQS 与 ReentrantLock:队列同步器与可重入锁
java
码语智行1 小时前
系统启动时初始化数据功能分析
java·spring boot
invicinble1 小时前
推荐一下,遇到的几本比较好的书
spring boot