前言
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 ;
1467




被折叠的 条评论
为什么被折叠?



