1.引入依赖(实体类引用了lombok,另引入这个依赖)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.4</version>
</dependency>
2.实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SysDepartDto {
@ExcelIgnore
private String id;
@ExcelIgnore
private String orgCode;
@ExcelProperty(value = {"部门名称"},index = 0)
private String departName;
@ExcelProperty(value = {"部门描述"},index = 3)
private String description;
@ExcelProperty(value = {"部门排序"},index = 1)
private Integer sortOrder;
@ExcelIgnore
private String parentId;
@ExcelProperty(value = {"父部门名称"},index = 2)
private String parentName;
@ExcelProperty(value = {"机构类型"},index = 4)
private String deptype;
@ExcelProperty(value = {"机构级别"},index = 5)
private String deplevl;
@ExcelIgnore
private List<SysDepartDto> children;
@ExcelIgnore
private String orgType;
@ExcelIgnore
private Long createBy;
@ExcelIgnore
private Long updateBy;
@ExcelIgnore
private String rowNum = "";
}
3.controller
@PostMapping("/import")
public void importSysDepart( @FormDataParam("file") InputStream inputStream){
SysDepartListener sysDepartListener = new SysDepartListener();
EasyExcel.read(inputStream, SysDepartDto.class, sysDepartListener)
.sheet().headRowNumber(2).doRead();
List<SysDepartDto> list = sysDepartListener.list;
Map<String, String> map = sysDepartListener.map;
if (map.size() != 0){
throw new ServiceException(Integer.parseInt(map.get("code")),map.get("msg"));
}
importSysDepart(list);
}
4.拦截器
package com.jpxx.admin.system.service;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jpxx.admin.system.service.api.dto.SysDepartDto;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class SysDepartListener extends AnalysisEventListener<SysDepartDto> {
List<SysDepartDto> list = new ArrayList<SysDepartDto>();
Map<String,String> map = new HashMap<>();
/**
* 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
*/
public SysDepartListener() {}
/**
* 这个每一条数据解析都会来调用
*
* @param sysDepartDto
* @param analysisContext
*/
@Override
public void invoke(SysDepartDto sysDepartDto, AnalysisContext analysisContext) {
if (sysDepartDto.getDeptype() == null||sysDepartDto.getDeptype()=="") {
map.put("code","400");
map.put("msg","表中有未填写的机构类型项");
return;
}
if (sysDepartDto.getDeplevl() == null||sysDepartDto.getDeplevl()=="") {
map.put("code","400");
map.put("msg","表中有未填写的机构级别项");
return;
}
if (sysDepartDto.getDeptype().equals("区域")){
sysDepartDto.setDeptype("0");
}else if (sysDepartDto.getDeptype().equals("职能部门")){
sysDepartDto.setDeptype("1");
}else {
map.put("code","400");
map.put("msg","机构类型项存在不合法内容");
return;
}
switch (sysDepartDto.getDeplevl()){
case "市":sysDepartDto.setDeplevl("0");break;
case "区":sysDepartDto.setDeplevl("1");break;
case "街道":sysDepartDto.setDeplevl("2");break;
default:
map.put("code","400");
map.put("msg","机构级别项存在不合法内容");
return;
}
list.add(sysDepartDto);
}
/**
* 所有数据解析完成了 都会来调用
*
* @param analysisContext
*/
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
————————————————
版权声明:本文为CSDN博主「深蓝格调_」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_41514643/article/details/107202051