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;
}
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());
}
}
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);
}
}
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());
}
}
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);
}
Aggregations