欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Easypoi导入提示错误行号和错误描述,拿来即用

程序员文章站 2022-05-18 18:06:58
实现效果{ "success": false, "code": "400", "msg": "请求参数有误", "errorMsg": "第2行,参考价格不能为空, 参考价格Excel 值获取失败", "data": null}1,pom导入 ...

实现效果

{
    "success": false,
    "code": "400",
    "msg": "请求参数有误",
    "errorMsg": "第2行,参考价格不能为空, 参考价格Excel 值获取失败",
    "data": null
}

1,pom导入

    <!-- https://mvnrepository.com/artifact/cn.afterturn/easypoi-annotation -->
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-annotation</artifactId>
        <version>${easypoi.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/cn.afterturn/easypoi-base -->
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-base</artifactId>
        <version>${easypoi.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/cn.afterturn/easypoi-web -->
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-web</artifactId>
        <version>${easypoi.version}</version>
    </dependency>

2,设置导入对象

@Data
public class SuppliesExcelBO implements IExcelModel, IExcelDataModel {

    @NotEmpty(message = "物资名称不能为空")
    @Excel(name = "物资名称", orderNum = "1")
    private String name;
    
    @NotNull(message = "物资类型不能为空")
    @Excel(name = "物资类型", orderNum = "2")
    private Integer typeMax;
    
    @NotEmpty(message = "物资规格不能为空")
    @Excel(name = "物资规格", orderNum = "3")
    private String spec;
    
    @Excel(name = "型号", orderNum = "4")
    private String model;
    
    @Excel(name = "品牌", orderNum = "5")
    private String brand;
    
    @NotNull(message = "参考价格不能为空")
    @Excel(name = "参考价格", orderNum = "6")
    private java.math.BigDecimal price;
    
    @Excel(name = "单位", orderNum = "7")
    private String unit;
    
    @Excel(name = "备注", orderNum = "8")
    private String remark;

    private String errorMsg;

    private Integer rowNum;

    @Override
    public int getRowNum() {
        return rowNum;
    }

    @Override
    public void setRowNum(int rowNum) {
        this.rowNum = rowNum;
    }
}

3,控制器接收请求

    /**
     * 导入物资
     */
    @PostMapping("/import")
    public Result<Integer> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        //使用EasyPoi获取文件数据,并校验
        ImportParams params = new ImportParams();
        params.setNeedVerfiy(true);
        ExcelImportResult<SuppliesExcelBO> importResult = ExcelImportUtil.importExcelMore(file.getInputStream(), SuppliesExcelBO.class, params);
        //导入校验存在失败则返回失败行
        if(importResult.isVerfiyFail()){
            SuppliesExcelBO a = importResult.getFailList().get(0);
            return ResultUtils.failure(ResultCodeEnum.BAD_REQUEST,"第"+a.getRowNum()+"行,"+a.getErrorMsg());
        }
        return suppliesService.importExcel(importResult.getList());
    }

4,导入对象转换为数据库对象并保存

    public Result importExcel(List<SuppliesExcelBO> suppliesList) {
        List<SuppliesDomain> SuppliesList = new ArrayList<>();
        for(SuppliesExcelBO suppliesExcelBO:suppliesList){
            SuppliesDomain supplies = new SuppliesDomain();
            BeanUtil.copyProperties(suppliesExcelBO, supplies);
            supplies.setStatus(1);
            SuppliesList.add(supplies);
        }
        mapper.insertList(SuppliesList);
        return Result.success();
    }

5,测试导入异常数据

Easypoi导入提示错误行号和错误描述,拿来即用

{
    "success": false,
    "code": "400",
    "msg": "请求参数有误",
    "errorMsg": "第2行,参考价格不能为空, 参考价格Excel 值获取失败",
    "data": null
}

本文地址:https://blog.csdn.net/u012796085/article/details/107335527