Search in sources :

Example 1 with ErrorExcelRow

use of com.github.lybgeek.excel.entity.ErrorExcelRow in project springboot-learning by lyb-geek.

the class BaseAnalysisEventListener method onException.

/**
 * 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。
 *
 * @param exception
 * @param analysisContext
 * @throws Exception
 */
@Override
public void onException(Exception exception, AnalysisContext analysisContext) {
    log.error("解析失败,但是继续解析下一行:{}", exception.getMessage());
    ErrorExcelRow errorExcelRow = this.setAndReturnErrorExcelRow(analysisContext, exception.getMessage());
    errorExcelRows.add(errorExcelRow);
    // 如果要获取头的信息 配合invokeHeadMap使用
    if (exception instanceof ExcelDataConvertException) {
        ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
        log.error("第{}行,第{}列解析异常,数据为:{}", excelDataConvertException.getRowIndex(), excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData());
    }
}
Also used : ErrorExcelRow(com.github.lybgeek.excel.entity.ErrorExcelRow) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException)

Example 2 with ErrorExcelRow

use of com.github.lybgeek.excel.entity.ErrorExcelRow in project springboot-learning by lyb-geek.

the class BaseAnalysisEventListener method setAndReturnErrorExcelRow.

private ErrorExcelRow setAndReturnErrorExcelRow(AnalysisContext analysisContext, String errorMsg) {
    int rowIndex = analysisContext.readRowHolder().getRowIndex();
    int sheetNo = analysisContext.readSheetHolder().getSheetNo();
    String sheetName = analysisContext.readSheetHolder().getSheetName();
    ErrorExcelRow errorExcelRow = ErrorExcelRow.builder().rowNum(rowIndex).sheetName(sheetName).sheetNo(sheetNo).errorMessage(errorMsg).build();
    return errorExcelRow;
}
Also used : ErrorExcelRow(com.github.lybgeek.excel.entity.ErrorExcelRow)

Example 3 with ErrorExcelRow

use of com.github.lybgeek.excel.entity.ErrorExcelRow in project springboot-learning by lyb-geek.

the class BaseAnalysisEventListener method validatePass.

private boolean validatePass(T entity, AnalysisContext analysisContext) {
    ValidResult validResult = ValidatorUtils.allCheckValidate(entity, false, ExcelGroup.class);
    if (!validResult.isSuccess()) {
        String errorMsg = StringUtils.join(validResult.getErrorMessages(), ",");
        ErrorExcelRow errorExcelRow = this.setAndReturnErrorExcelRow(analysisContext, errorMsg);
        errorExcelRows.add(errorExcelRow);
    }
    return validResult.isSuccess();
}
Also used : ErrorExcelRow(com.github.lybgeek.excel.entity.ErrorExcelRow) ValidResult(com.github.lybgeek.validator.model.ValidResult)

Example 4 with ErrorExcelRow

use of com.github.lybgeek.excel.entity.ErrorExcelRow in project springboot-learning by lyb-geek.

the class ExcelController method getErrorMsg.

private String getErrorMsg(List<ErrorExcelRow> errorExcelRows) {
    Map<Integer, List<String>> sheetErrorMap = new HashMap<>();
    for (ErrorExcelRow errorRow : errorExcelRows) {
        Integer sheetNo = errorRow.getSheetNo();
        List<String> errorMsgs = sheetErrorMap.get(sheetNo);
        String errorMsg = "第" + errorRow.getRowNum() + "行," + errorRow.getErrorMessage();
        if (CollectionUtils.isEmpty(errorMsgs)) {
            errorMsgs = new ArrayList<>();
            sheetErrorMap.put(sheetNo, errorMsgs);
        }
        errorMsgs.add(errorMsg);
    }
    List<String> errorMsgList = new ArrayList<>();
    sheetErrorMap.forEach((sheetNo, errorMsgs) -> {
        String errorMsg = "在第" + (sheetNo + 1) + "个sheet中存在错误-->" + StringUtils.join(errorMsgs, "|");
        errorMsgList.add(errorMsg);
    });
    return StringUtils.join(errorMsgList, ";");
}
Also used : HashMap(java.util.HashMap) ErrorExcelRow(com.github.lybgeek.excel.entity.ErrorExcelRow) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ErrorExcelRow (com.github.lybgeek.excel.entity.ErrorExcelRow)4 ExcelDataConvertException (com.alibaba.excel.exception.ExcelDataConvertException)1 ValidResult (com.github.lybgeek.validator.model.ValidResult)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1