easyexcel导入(数据校验)

1.引入依赖(实体类引用了lombok,另引入这个依赖)

        
            com.alibaba
            easyexcel
            2.1.4
        

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 children;

    @ExcelIgnore
    private String orgType;

    @ExcelIgnore
    private Long createBy;

    @ExcelIgnore
    private Long updateBy;

    @ExcelIgnore
    private String rowNum = "";
}

3.controller

@ApiOperation(value = "部门-导入", tags = {SwaggerTagConstants.ADMIN})
    @POST
    @Path("import")
    @Consumes({"multipart/form-data"})
    public void importSysDepart( @FormDataParam("file") InputStream inputStream){
        SysDepartListener sysDepartListener = new SysDepartListener();
        EasyExcel.read(inputStream, SysDepartDto.class, sysDepartListener)
                .sheet().headRowNumber(2).doRead();
        List list = sysDepartListener.list;
        Map 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 {

    List list = new ArrayList();
    Map 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) {

    }
}

 

你可能感兴趣的:(easyexcel导入(数据校验))