工具篇-EasyExcel-web普通导入


前言

EasyExcel web 导入excel 数据。


提示:以下是本篇文章正文内容,下面案例可供参考

一、准备工作:

pom引入jar:

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>easyexcel</artifactId>
   <version>3.1.4</version>
</dependency>

二、数据导入:

1.定义导入的实体类:

根据业务自行定义即可

代码如下(示例):

package com.cric.zhongjian.system.dto;

import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
public class ProductAdaptExcelStageOrSpecialDto implements Serializable {
    @ExcelProperty("指标名称")
//    @ExcelProperty(index = 0)
    private String name;
    @ExcelProperty("指标类型")
//    @ExcelProperty(index = 1)
    private String dictType;
    @ExcelProperty("指标编码")
//    @ExcelProperty(index = 2)
    private String dictCode;
    @ExcelProperty("指标所属航道")
//    @ExcelProperty(index = 3)
    private String channel;
        @ExcelProperty("指标排序")
//    @ExcelProperty(index = 4)
    private String order;
    @ExcelProperty("指标备注")
//    @ExcelProperty(index = 5)
    private String remark;

}

  • 使用@Data 生成get set 方法;
  • @ExcelProperty 定义excel 的列取值;

2.定义listener:

package com.cric.zhongjian.system.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cric.zhongjian.system.dto.ProductAdaptExcelStageOrSpecialDto;

import java.util.ArrayList;
import java.util.List;

public class ProductAdaptExcelListener extends AnalysisEventListener<ProductAdaptExcelStageOrSpecialDto> {
    private List<ProductAdaptExcelStageOrSpecialDto> fileDatas = new ArrayList<>(1 << 5);

    @Override
    public void invoke(ProductAdaptExcelStageOrSpecialDto data, AnalysisContext context) {
        fileDatas.add(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 解析完成后的操作
    }

    public List<ProductAdaptExcelStageOrSpecialDto> getFileDatas() {
        return fileDatas;
    }
}

  • 继承AnalysisEventListener 重新数据导入的方法
  • 使用 invoke 方法可以对读到的数据进行整合;
  • 使用doAfterAllAnalysed 可以读已经读取完成,对获取到的excel 数据进行校验;

3.web 后端文件接收:

3.1 控制器:

 @ResponseBody
@PostMapping("stageOrSpecial")
 public AjaxResult initStageOrSpecial(@RequestParam("file") MultipartFile file,
                                      @RequestParam("tentCode") @Valid @NotEmpty String tentCode) {

     InitProductAdaptDataReqDto reqDto = new InitProductAdaptDataReqDto();
     reqDto.setFile(file).setTentCode(tentCode);
     return AjaxResult.success(initDataService.initStageOrSpecial(reqDto));
 }

3.2 实现类:

@Override
@Transactional(rollbackFor = Exception.class)
public Map initStageOrSpecial(InitProductAdaptDataReqDto reqDto) {
   MultipartFile file = reqDto.getFile();
   Map<String, Object> mapData = new HashMap<>(1 << 2);
   mapData.put("success", true);
   mapData.put("msg", "");
   if (ilegalFile(file)) {
       mapData.put("success", false);
       mapData.put("msg", "文件非法");
       return mapData;
   }

   // read
   List<ProductAdaptExcelStageOrSpecialDto> fileData = null;
   try {
       fileData = getFileData(file);
   } catch (Exception e) {
       mapData.put("success", false);
       mapData.put("msg", "文件数据读取异常:" + ExceptionFormatUtil.buildErrorMessage(e));
       return mapData;
   }

   // deal 对读取到的数据进行处理,根据自身业务处理
   dealFileData(fileData, reqDto.getTentCode());

   return mapData;
}
 // 读取文件
private List<ProductAdaptExcelStageOrSpecialDto> getFileData(MultipartFile file) throws IOException {
    // 创建 Excel 读取监听器
    ProductAdaptExcelListener listener = new ProductAdaptExcelListener();
    // 读取 Excel 文件
    EasyExcel.read(file.getInputStream(), ProductAdaptExcelStageOrSpecialDto.class, listener).sheet().doRead();
    return listener.getFileDatas();
}
/**
* 文件非法
 *
 * @param file
 * @return
 */
private boolean ilegalFile(MultipartFile file) {
    String[] split = file.getOriginalFilename().split("\\.");
    if (StringUtils.isEmpty(file.getOriginalFilename()) || !(split[split.length - 1].equals("xlsx"))) {
        return true;
    }
    return false;
}

三、总结

本文通过EasyExcel 实现web 段读取excel ;

四、参考:

EasyExcel web 中的读;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值