Search in sources :

Example 6 with ExcelDataConvertException

use of com.alibaba.excel.exception.ExcelDataConvertException in project easyexcel by alibaba.

the class ModelBuildEventListener method buildUserModel.

private Object buildUserModel(Map<Integer, ReadCellData<?>> cellDataMap, ReadSheetHolder readSheetHolder, AnalysisContext context) {
    ExcelReadHeadProperty excelReadHeadProperty = readSheetHolder.excelReadHeadProperty();
    Object resultModel;
    try {
        resultModel = excelReadHeadProperty.getHeadClazz().newInstance();
    } catch (Exception e) {
        throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), 0, new ReadCellData<>(CellDataTypeEnum.EMPTY), null, "Can not instance class: " + excelReadHeadProperty.getHeadClazz().getName(), e);
    }
    Map<Integer, Head> headMap = excelReadHeadProperty.getHeadMap();
    BeanMap dataMap = BeanMapUtils.create(resultModel);
    for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
        Integer index = entry.getKey();
        Head head = entry.getValue();
        String fieldName = head.getFieldName();
        if (!cellDataMap.containsKey(index)) {
            continue;
        }
        ReadCellData<?> cellData = cellDataMap.get(index);
        Object value = ConverterUtils.convertToJavaObject(cellData, head.getField(), ClassUtils.declaredExcelContentProperty(dataMap, readSheetHolder.excelReadHeadProperty().getHeadClazz(), fieldName), readSheetHolder.converterMap(), context, context.readRowHolder().getRowIndex(), index);
        if (value != null) {
            dataMap.put(fieldName, value);
        }
    }
    return resultModel;
}
Also used : Head(com.alibaba.excel.metadata.Head) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ReadCellData(com.alibaba.excel.metadata.data.ReadCellData) BeanMap(org.springframework.cglib.beans.BeanMap) BeanMap(org.springframework.cglib.beans.BeanMap) Map(java.util.Map) ExcelReadHeadProperty(com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)

Example 7 with ExcelDataConvertException

use of com.alibaba.excel.exception.ExcelDataConvertException 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 8 with ExcelDataConvertException

use of com.alibaba.excel.exception.ExcelDataConvertException in project eden-architect by shiyindaxiaojie.

the class AnalysisEventListenerAdapter method onException.

@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
    log.error("读取 Excel 的数据发生异常:{}", exception.getMessage(), exception);
    if (exception instanceof ExcelDataConvertException) {
        ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
        log.error("读取 Excel 发生异常的数据为第 {} 行,第 {} 列,内容为:{}", excelDataConvertException.getRowIndex(), excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData());
    }
    if (isStopOnException) {
        super.onException(exception, context);
    }
}
Also used : ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException)

Example 9 with ExcelDataConvertException

use of com.alibaba.excel.exception.ExcelDataConvertException in project bubble-fireworks by fxbin.

the class AbstractExcelListener method onException.

@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
    log.error("解析失败,是否继续解析下一行:[{}], msg:[{}]", isContinueAfterThrowing, exception.getMessage());
    if (!isContinueAfterThrowing) {
        throw exception;
    }
    if (exception instanceof ExcelDataConvertException) {
        ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
        log.error("第 {} Sheet页,第{}行,第{}列解析异常", excelDataConvertException.getRowIndex(), excelDataConvertException.getRowIndex(), excelDataConvertException.getColumnIndex());
    }
}
Also used : ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException)

Example 10 with ExcelDataConvertException

use of com.alibaba.excel.exception.ExcelDataConvertException in project RuoYi-Vue-Plus by JavaLionLi.

the class DefaultExcelListener method onException.

/**
 * 处理异常
 *
 * @param exception ExcelDataConvertException
 * @param context   Excel 上下文
 */
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
    String errMsg = null;
    if (exception instanceof ExcelDataConvertException) {
        // 如果是某一个单元格的转换异常 能获取到具体行号
        ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
        Integer rowIndex = excelDataConvertException.getRowIndex();
        Integer columnIndex = excelDataConvertException.getColumnIndex();
        errMsg = StrUtil.format("第{}行-第{}列-表头{}: 解析异常<br/>", rowIndex + 1, columnIndex + 1, headMap.get(columnIndex));
        if (log.isDebugEnabled()) {
            log.error(errMsg);
        }
    }
    if (exception instanceof ConstraintViolationException) {
        ConstraintViolationException constraintViolationException = (ConstraintViolationException) exception;
        Set<ConstraintViolation<?>> constraintViolations = constraintViolationException.getConstraintViolations();
        String constraintViolationsMsg = constraintViolations.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(", "));
        errMsg = StrUtil.format("第{}行数据校验异常: {}", context.readRowHolder().getRowIndex() + 1, constraintViolationsMsg);
        if (log.isDebugEnabled()) {
            log.error(errMsg);
        }
    }
    excelResult.getErrorList().add(errMsg);
    throw new ExcelAnalysisException(errMsg);
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ConstraintViolationException(javax.validation.ConstraintViolationException) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Aggregations

ExcelDataConvertException (com.alibaba.excel.exception.ExcelDataConvertException)12 ConstraintViolation (javax.validation.ConstraintViolation)4 ExcelAnalysisException (com.alibaba.excel.exception.ExcelAnalysisException)3 ReadCellData (com.alibaba.excel.metadata.data.ReadCellData)3 ConstraintViolationException (javax.validation.ConstraintViolationException)3 Head (com.alibaba.excel.metadata.Head)2 ExcelReadHeadProperty (com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)2 Map (java.util.Map)2 EasyExcel (com.alibaba.excel.EasyExcel)1 ExcelWriter (com.alibaba.excel.ExcelWriter)1 AnalysisContext (com.alibaba.excel.context.AnalysisContext)1 NullableObjectConverter (com.alibaba.excel.converters.NullableObjectConverter)1 ReadConverterContext (com.alibaba.excel.converters.ReadConverterContext)1 ModelBuildEventListener (com.alibaba.excel.read.listener.ModelBuildEventListener)1 ReadListener (com.alibaba.excel.read.listener.ReadListener)1 ReadSheetHolder (com.alibaba.excel.read.metadata.holder.ReadSheetHolder)1 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)1 BindDict (com.diboot.core.binding.annotation.BindDict)1 BaseConfig (com.diboot.core.config.BaseConfig)1 BusinessException (com.diboot.core.exception.BusinessException)1