【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读取完成");
    }
}
相关推荐
hanbarger11 分钟前
mybatis框架——缓存,分页
java·spring·mybatis
cdut_suye18 分钟前
Linux工具使用指南:从apt管理、gcc编译到makefile构建与gdb调试
java·linux·运维·服务器·c++·人工智能·python
苹果醋330 分钟前
2020重新出发,MySql基础,MySql表数据操作
java·运维·spring boot·mysql·nginx
小蜗牛慢慢爬行32 分钟前
如何在 Spring Boot 微服务中设置和管理多个数据库
java·数据库·spring boot·后端·微服务·架构·hibernate
azhou的代码园35 分钟前
基于JAVA+SpringBoot+Vue的制造装备物联及生产管理ERP系统
java·spring boot·制造
Swift社区1 小时前
Excel 列名称转换问题 Swift 解答
开发语言·excel·swift
wm10431 小时前
java web springboot
java·spring boot·后端
smile-yan1 小时前
Provides transitive vulnerable dependency maven 提示依赖存在漏洞问题的解决方法
java·maven
老马啸西风1 小时前
NLP 中文拼写检测纠正论文-01-介绍了SIGHAN 2015 包括任务描述,数据准备, 绩效指标和评估结果
java
Earnest~1 小时前
Maven极简安装&配置-241223
java·maven