Search in sources :

Example 1 with ExcelReadException

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;
}
Also used : ExcelReadException(com.github.liaochong.myexcel.exception.ExcelReadException) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet)

Example 2 with ExcelReadException

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ExcelReadException(com.github.liaochong.myexcel.exception.ExcelReadException) XSSFPicture(org.apache.poi.xssf.usermodel.XSSFPicture) HSSFPicture(org.apache.poi.hssf.usermodel.HSSFPicture)

Example 3 with ExcelReadException

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);
    }
}
Also used : ExcelColumnMapping(com.github.liaochong.myexcel.core.ExcelColumnMapping) SaxReadException(com.github.liaochong.myexcel.exception.SaxReadException) ExcelReadException(com.github.liaochong.myexcel.exception.ExcelReadException) Properties(java.util.Properties) SaxReadException(com.github.liaochong.myexcel.exception.SaxReadException) ExcelReadException(com.github.liaochong.myexcel.exception.ExcelReadException)

Aggregations

ExcelReadException (com.github.liaochong.myexcel.exception.ExcelReadException)3 ExcelColumnMapping (com.github.liaochong.myexcel.core.ExcelColumnMapping)1 SaxReadException (com.github.liaochong.myexcel.exception.SaxReadException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Properties (java.util.Properties)1 HSSFPicture (org.apache.poi.hssf.usermodel.HSSFPicture)1 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 XSSFPicture (org.apache.poi.xssf.usermodel.XSSFPicture)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1