use of com.github.liaochong.myexcel.exception.ExcelReadException in project myexcel by liaochong.
the class DefaultExcelReader method getSheet.
private Sheet getSheet() {
Sheet sheet;
if (sheetName != null) {
sheet = wb.getSheet(sheetName);
if (sheet == null) {
throw new ExcelReadException("Cannot find sheet based on sheetName:" + sheetName);
}
} else {
sheet = wb.getSheetAt(sheetIndex);
}
getAllPictures(sheet);
return sheet;
}
use of com.github.liaochong.myexcel.exception.ExcelReadException in project myexcel by liaochong.
the class DefaultExcelReader method convertPicture.
private void convertPicture(Row row, T obj, Integer index, Field field) {
byte[] pictureData;
if (isXSSFSheet) {
XSSFPicture xssfPicture = xssfPicturesMap.get(row.getRowNum() + "_" + index);
if (xssfPicture == null) {
return;
}
pictureData = xssfPicture.getPictureData().getData();
} else {
HSSFPicture hssfPicture = hssfPictureMap.get(row.getRowNum() + "_" + index);
if (hssfPicture == null) {
return;
}
pictureData = hssfPicture.getPictureData().getData();
}
try {
field.set(obj, new ByteArrayInputStream(pictureData));
} catch (IllegalAccessException e) {
throw new ExcelReadException("Failed to read picture.", e);
}
}
use of com.github.liaochong.myexcel.exception.ExcelReadException in project myexcel by liaochong.
the class ReadConverterContext method convert.
public static void convert(Object obj, ReadContext context, ConvertContext convertContext, BiFunction<Throwable, ReadContext, Boolean> exceptionFunction) {
ReadConverter<String, ?> readConverter = READ_CONVERTERS.get(context.getField().getType());
if (readConverter == null) {
throw new IllegalStateException("No suitable type converter was found.");
}
Object value = null;
try {
Properties properties = MAPPING_CACHE.get(context.getField());
if (properties == null) {
ExcelColumnMapping mapping = convertContext.excelColumnMappingMap.get(context.getField());
if (mapping != null && !mapping.mapping.isEmpty()) {
properties = PropertyUtil.getReverseProperties(mapping);
} else {
properties = EMPTY_PROPERTIES;
}
MAPPING_CACHE.cache(context.getField(), properties);
}
String mappingVal = properties.getProperty(context.getVal());
if (mappingVal != null) {
context.setVal(mappingVal);
}
value = readConverter.convert(context.getVal(), context.getField(), convertContext);
} catch (Exception e) {
Boolean toContinue = exceptionFunction.apply(e, context);
if (!toContinue) {
throw new ExcelReadException("Failed to convert content,field:[" + context.getField().getDeclaringClass().getName() + "#" + context.getField().getName() + "],content:[" + context.getVal() + "],rowNum:[" + context.getRowNum() + "]", e);
}
}
if (value == null) {
return;
}
try {
context.getField().set(obj, value);
} catch (IllegalAccessException e) {
throw new SaxReadException("Failed to set the " + context.getField().getDeclaringClass().getName() + "#" + context.getField().getName() + " field value to " + context.getVal(), e);
}
}
Aggregations