easyExcel文件上传验证

/**
  * 处理每行数据
  */
@Override
    public void invoke(Model model, AnalysisContext analysisContext) {
        if (model.getName.length()>20) {
            throw new ExcelAnalysisException("姓名过长");
        }
    }


/**
  * 接收invoke中的异常,将异常处理抛出到service
  *
  * @author jamin
  * @date 2020/7/30 10:28
  */
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {

    //获取行号
    Integer index = context.readRowHolder().getRowIndex() + 1;
    //invoke中的异常
    if (exception instanceof ExcelAnalysisException) {
        ExcelAnalysisException excelAnalysisException = (ExcelAnalysisException) exception;
        String message = excelAnalysisException.getMessage();
        throw new ExcelAnalysisStopException("[数据处理异常--第" + index + "行]----" + message);
    }
    throw new ExcelAnalysisStopException("[数据解析异常] 请检查文件");

}
 /**
   * 导入数据
   *
   * @author jamin
   * @date 2020/7/30 10:37
   */
 public MessageVo importData(MultipartFile file) {
     Map<String, String> map = new HashMap<>();
      map.put("code","0");
      try {
 EasyExcel.read(file.getInputStream()).autoCloseStream(true).autoTrim(true).headRowNumber(2).head(Model.class).registerReadListener(new ExcelListener()).sheet(0).doRead();
        } catch (Exception ex) {
             map.put("code","1");
            map.put("msg",ex.getCause().getMessage());
        }
 }

猜你喜欢

转载自www.cnblogs.com/JaminYe/p/13406386.html