use of com.alibaba.excel.exception.ExcelDataConvertException in project diboot by dibo-software.
the class ReadExcelListener method onException.
/**
* <h3>异常处理</h3>
* 修补数据,回写错误
*/
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
// 数据类型转化异常
if (exception instanceof ExcelDataConvertException) {
ExcelDataConvertException dataConvertException = (ExcelDataConvertException) exception;
Map<Integer, ReadCellData<?>> cellMap = new HashMap<>((Map) context.readRowHolder().getCellMap());
Map<String, String> errorDataMap = new HashMap<>();
Map<String, String> errorMsgMap = new HashMap<>();
Consumer<ExcelDataConvertException> addErrorData = e -> {
Integer columnIndex = e.getColumnIndex();
String key = fieldNameMap.get(columnIndex);
errorDataMap.put(key, cellMap.remove(columnIndex).getStringValue());
errorMsgMap.put(key, "数据格式转换异常,非期望的数据类型[" + e.getExcelContentProperty().getField().getType().getSimpleName() + "]");
};
addErrorData.accept(dataConvertException);
ReadListener<?> readListener = context.readWorkbookHolder().getReadListenerList().get(0);
if (readListener instanceof ModelBuildEventListener) {
while (true) {
try {
((ModelBuildEventListener) readListener).invoke(cellMap, context);
break;
} catch (ExcelDataConvertException convertException) {
addErrorData.accept(convertException);
}
}
} else {
log.error("数据转换异常", exception);
StringBuilder errorMsg = new StringBuilder().append("第 ").append(context.readRowHolder().getRowIndex() + 1).append(" 行,");
errorMsgMap.forEach((fieldName, msg) -> errorMsg.append(fieldHeadMap.get(fieldName)).append(":").append(msg));
addExceptionMsg(errorMsg.toString());
return;
}
T currentRowAnalysisResult = (T) context.readRowHolder().getCurrentRowAnalysisResult();
currentRowAnalysisResult.setRowIndex(context.readRowHolder().getRowIndex());
errorDataMap.forEach(currentRowAnalysisResult::addInvalidValue);
errorMsgMap.forEach(currentRowAnalysisResult::addComment);
// 校验异常
Set<ConstraintViolation<T>> constraintViolations = V.validateBean(currentRowAnalysisResult);
if (V.notEmpty(constraintViolations)) {
for (ConstraintViolation<T> violation : constraintViolations) {
String propertyName = violation.getPropertyPath().toString();
// 剔除解析识别的数据校验
if (!errorDataMap.containsKey(propertyName)) {
currentRowAnalysisResult.addComment(propertyName, violation.getMessage());
}
}
}
this.cachedData(currentRowAnalysisResult);
} else {
log.error("出现未预知的异常:", exception);
addExceptionMsg("第 " + (context.readRowHolder().getRowIndex() + 1) + " 行,解析异常: " + exception.getMessage());
}
}
use of com.alibaba.excel.exception.ExcelDataConvertException in project easyexcel by alibaba.
the class DemoHeadDataListener method onException.
/**
* 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。
*
* @param exception
* @param context
* @throws Exception
*/
@Override
public void onException(Exception exception, AnalysisContext context) {
log.error("解析失败,但是继续解析下一行:{}", exception.getMessage());
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 easyexcel by alibaba.
the class DemoExceptionListener method onException.
/**
* 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。
*
* @param exception
* @param context
* @throws Exception
*/
@Override
public void onException(Exception exception, AnalysisContext context) {
log.error("解析失败,但是继续解析下一行:{}", exception.getMessage());
// 如果要获取头的信息 配合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 easyexcel by alibaba.
the class ConverterUtils method doConvertToJavaObject.
/**
* @param cellData
* @param clazz
* @param contentProperty
* @param converterMap
* @param context
* @param rowIndex
* @param columnIndex
* @return
*/
private static Object doConvertToJavaObject(ReadCellData<?> cellData, Class<?> clazz, ExcelContentProperty contentProperty, Map<ConverterKey, Converter<?>> converterMap, AnalysisContext context, Integer rowIndex, Integer columnIndex) {
Converter<?> converter = null;
if (contentProperty != null) {
converter = contentProperty.getConverter();
}
boolean canNotConverterEmpty = cellData.getType() == CellDataTypeEnum.EMPTY && !(converter instanceof NullableObjectConverter);
if (canNotConverterEmpty) {
return null;
}
if (converter == null) {
converter = converterMap.get(ConverterKeyBuild.buildKey(clazz, cellData.getType()));
}
if (converter == null) {
throw new ExcelDataConvertException(rowIndex, columnIndex, cellData, contentProperty, "Converter not found, convert " + cellData.getType() + " to " + clazz.getName());
}
try {
return converter.convertToJavaData(new ReadConverterContext<>(cellData, contentProperty, context));
} catch (Exception e) {
throw new ExcelDataConvertException(rowIndex, columnIndex, cellData, contentProperty, "Convert data " + cellData + " to " + clazz + " error ", e);
}
}
use of com.alibaba.excel.exception.ExcelDataConvertException in project easyexcel by alibaba.
the class ConverterUtils method convertToStringMap.
/**
* Convert it into a String map
*
* @param cellDataMap
* @param context
* @return
*/
public static Map<Integer, String> convertToStringMap(Map<Integer, ReadCellData<?>> cellDataMap, AnalysisContext context) {
Map<Integer, String> stringMap = MapUtils.newHashMapWithExpectedSize(cellDataMap.size());
ReadSheetHolder readSheetHolder = context.readSheetHolder();
int index = 0;
for (Map.Entry<Integer, ReadCellData<?>> entry : cellDataMap.entrySet()) {
Integer key = entry.getKey();
ReadCellData<?> cellData = entry.getValue();
while (index < key) {
stringMap.put(index, null);
index++;
}
index++;
if (cellData.getType() == CellDataTypeEnum.EMPTY) {
stringMap.put(key, null);
continue;
}
Converter<?> converter = readSheetHolder.converterMap().get(ConverterKeyBuild.buildKey(String.class, cellData.getType()));
if (converter == null) {
throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), key, cellData, null, "Converter not found, convert " + cellData.getType() + " to String");
}
try {
stringMap.put(key, (String) (converter.convertToJavaData(new ReadConverterContext<>(cellData, null, context))));
} catch (Exception e) {
throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), key, cellData, null, "Convert data " + cellData + " to String error ", e);
}
}
return stringMap;
}
Aggregations