Search in sources :

Example 1 with ExcelCommonException

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

the class ConverterDataListener method doAfterAllAnalysed.

@Override
public void doAfterAllAnalysed(AnalysisContext context) {
    Assert.assertEquals(list.size(), 1);
    ConverterReadData data = list.get(0);
    try {
        Assert.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01"), data.getDate());
    } catch (ParseException e) {
        throw new ExcelCommonException("Test Exception", e);
    }
    Assert.assertEquals(DateUtils.parseLocalDateTime("2020-01-01 01:01:01", null, null), data.getLocalDateTime());
    Assert.assertEquals(data.getBooleanData(), Boolean.TRUE);
    Assert.assertEquals(data.getBigDecimal().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0);
    Assert.assertEquals(data.getBigInteger().intValue(), BigInteger.ONE.intValue(), 0.0);
    Assert.assertEquals((long) data.getLongData(), 1L);
    Assert.assertEquals((long) data.getIntegerData(), 1L);
    Assert.assertEquals((long) data.getShortData(), 1L);
    Assert.assertEquals((long) data.getByteData(), 1L);
    Assert.assertEquals(data.getDoubleData(), 1.0, 0.0);
    Assert.assertEquals(data.getFloatData(), (float) 1.0, 0.0);
    Assert.assertEquals(data.getString(), "测试");
    Assert.assertEquals(data.getCellData().getStringValue(), "自定义");
    LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0)));
}
Also used : ParseException(java.text.ParseException) ExcelCommonException(com.alibaba.excel.exception.ExcelCommonException)

Example 2 with ExcelCommonException

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

the class AnnotationDataListener method doAfterAllAnalysed.

@Override
public void doAfterAllAnalysed(AnalysisContext context) {
    Assert.assertEquals(list.size(), 1);
    AnnotationData data = list.get(0);
    try {
        Assert.assertEquals(data.getDate(), DateUtils.parseDate("2020-01-01 01:01:01"));
    } catch (ParseException e) {
        throw new ExcelCommonException("Test Exception", e);
    }
    Assert.assertEquals(data.getNumber(), 99.99, 0.00);
    LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0)));
}
Also used : ParseException(java.text.ParseException) ExcelCommonException(com.alibaba.excel.exception.ExcelCommonException)

Example 3 with ExcelCommonException

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

the class ReadAllConverterDataListener method doAfterAllAnalysed.

@Override
public void doAfterAllAnalysed(AnalysisContext context) {
    Assert.assertEquals(list.size(), 1);
    ReadAllConverterData data = list.get(0);
    Assert.assertEquals(data.getBigDecimalBoolean().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0);
    Assert.assertEquals(data.getBigDecimalNumber().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0);
    Assert.assertEquals(data.getBigDecimalString().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0);
    Assert.assertEquals(data.getBigIntegerBoolean().intValue(), BigInteger.ONE.intValue(), 0.0);
    Assert.assertEquals(data.getBigIntegerNumber().intValue(), BigInteger.ONE.intValue(), 0.0);
    Assert.assertEquals(data.getBigIntegerString().intValue(), BigInteger.ONE.intValue(), 0.0);
    Assert.assertTrue(data.getBooleanBoolean());
    Assert.assertTrue(data.getBooleanNumber());
    Assert.assertTrue(data.getBooleanString());
    Assert.assertEquals((long) data.getByteBoolean(), 1L);
    Assert.assertEquals((long) data.getByteNumber(), 1L);
    Assert.assertEquals((long) data.getByteString(), 1L);
    try {
        Assert.assertEquals(data.getDateNumber(), DateUtils.parseDate("2020-01-01 01:01:01"));
        Assert.assertEquals(data.getDateString(), DateUtils.parseDate("2020-01-01 01:01:01"));
    } catch (ParseException e) {
        throw new ExcelCommonException("Test Exception", e);
    }
    Assert.assertEquals(data.getLocalDateTimeNumber(), DateUtils.parseLocalDateTime("2020-01-01 01:01:01", null, null));
    Assert.assertEquals(data.getLocalDateTimeString(), DateUtils.parseLocalDateTime("2020-01-01 01:01:01", null, null));
    Assert.assertEquals(data.getDoubleBoolean(), 1.0, 0.0);
    Assert.assertEquals(data.getDoubleNumber(), 1.0, 0.0);
    Assert.assertEquals(data.getDoubleString(), 1.0, 0.0);
    Assert.assertEquals(data.getFloatBoolean(), (float) 1.0, 0.0);
    Assert.assertEquals(data.getFloatNumber(), (float) 1.0, 0.0);
    Assert.assertEquals(data.getFloatString(), (float) 1.0, 0.0);
    Assert.assertEquals((long) data.getIntegerBoolean(), 1L);
    Assert.assertEquals((long) data.getIntegerNumber(), 1L);
    Assert.assertEquals((long) data.getIntegerString(), 1L);
    Assert.assertEquals((long) data.getLongBoolean(), 1L);
    Assert.assertEquals((long) data.getLongNumber(), 1L);
    Assert.assertEquals((long) data.getLongString(), 1L);
    Assert.assertEquals((long) data.getShortBoolean(), 1L);
    Assert.assertEquals((long) data.getShortNumber(), 1L);
    Assert.assertEquals((long) data.getShortString(), 1L);
    Assert.assertEquals(data.getStringBoolean().toLowerCase(), "true");
    Assert.assertEquals(data.getStringString(), "测试");
    Assert.assertEquals(data.getStringError(), "#VALUE!");
    if (context.readWorkbookHolder().getExcelType() != ExcelTypeEnum.CSV) {
        Assert.assertEquals("2020-1-1 1:01", data.getStringNumberDate());
    } else {
        Assert.assertEquals("2020-01-01 01:01:01", data.getStringNumberDate());
    }
    double doubleStringFormulaNumber = new BigDecimal(data.getStringFormulaNumber()).doubleValue();
    Assert.assertEquals(doubleStringFormulaNumber, 2.0, 0.0);
    Assert.assertEquals(data.getStringFormulaString(), "1测试");
    LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0)));
}
Also used : ParseException(java.text.ParseException) ExcelCommonException(com.alibaba.excel.exception.ExcelCommonException) BigDecimal(java.math.BigDecimal)

Example 4 with ExcelCommonException

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

the class ClassUtils method declaredFieldContentMap.

private static Map<String, ExcelContentProperty> declaredFieldContentMap(Class<?> clazz) {
    if (clazz == null) {
        return null;
    }
    return CLASS_CONTENT_CACHE.computeIfAbsent(clazz, key -> {
        List<Field> tempFieldList = new ArrayList<>();
        Class<?> tempClass = clazz;
        while (tempClass != null) {
            Collections.addAll(tempFieldList, tempClass.getDeclaredFields());
            // Get the parent class and give it to yourself
            tempClass = tempClass.getSuperclass();
        }
        ContentStyle parentContentStyle = clazz.getAnnotation(ContentStyle.class);
        ContentFontStyle parentContentFontStyle = clazz.getAnnotation(ContentFontStyle.class);
        Map<String, ExcelContentProperty> fieldContentMap = MapUtils.newHashMapWithExpectedSize(tempFieldList.size());
        for (Field field : tempFieldList) {
            ExcelContentProperty excelContentProperty = new ExcelContentProperty();
            excelContentProperty.setField(field);
            ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
            if (excelProperty != null) {
                Class<? extends Converter<?>> convertClazz = excelProperty.converter();
                if (convertClazz != AutoConverter.class) {
                    try {
                        Converter<?> converter = convertClazz.getDeclaredConstructor().newInstance();
                        excelContentProperty.setConverter(converter);
                    } catch (Exception e) {
                        throw new ExcelCommonException("Can not instance custom converter:" + convertClazz.getName());
                    }
                }
            }
            ContentStyle contentStyle = field.getAnnotation(ContentStyle.class);
            if (contentStyle == null) {
                contentStyle = parentContentStyle;
            }
            excelContentProperty.setContentStyleProperty(StyleProperty.build(contentStyle));
            ContentFontStyle contentFontStyle = field.getAnnotation(ContentFontStyle.class);
            if (contentFontStyle == null) {
                contentFontStyle = parentContentFontStyle;
            }
            excelContentProperty.setContentFontProperty(FontProperty.build(contentFontStyle));
            excelContentProperty.setDateTimeFormatProperty(DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class)));
            excelContentProperty.setNumberFormatProperty(NumberFormatProperty.build(field.getAnnotation(NumberFormat.class)));
            fieldContentMap.put(field.getName(), excelContentProperty);
        }
        return fieldContentMap;
    });
}
Also used : ExcelProperty(com.alibaba.excel.annotation.ExcelProperty) ArrayList(java.util.ArrayList) ExcelCommonException(com.alibaba.excel.exception.ExcelCommonException) ContentStyle(com.alibaba.excel.annotation.write.style.ContentStyle) Field(java.lang.reflect.Field) ContentFontStyle(com.alibaba.excel.annotation.write.style.ContentFontStyle) ExcelContentProperty(com.alibaba.excel.metadata.property.ExcelContentProperty) ExcelCommonException(com.alibaba.excel.exception.ExcelCommonException)

Example 5 with ExcelCommonException

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

the class ClassUtils method declaredOneField.

private static void declaredOneField(Field field, Map<Integer, List<Field>> orderFieldMap, Map<Integer, Field> indexFieldMap, Map<String, Field> ignoreMap, ExcelIgnoreUnannotated excelIgnoreUnannotated) {
    ExcelIgnore excelIgnore = field.getAnnotation(ExcelIgnore.class);
    if (excelIgnore != null) {
        ignoreMap.put(field.getName(), field);
        return;
    }
    ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
    boolean noExcelProperty = excelProperty == null && excelIgnoreUnannotated != null;
    if (noExcelProperty) {
        ignoreMap.put(field.getName(), field);
        return;
    }
    boolean isStaticFinalOrTransient = (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) || Modifier.isTransient(field.getModifiers());
    if (excelProperty == null && isStaticFinalOrTransient) {
        ignoreMap.put(field.getName(), field);
        return;
    }
    if (excelProperty != null && excelProperty.index() >= 0) {
        if (indexFieldMap.containsKey(excelProperty.index())) {
            throw new ExcelCommonException("The index of '" + indexFieldMap.get(excelProperty.index()).getName() + "' and '" + field.getName() + "' must be inconsistent");
        }
        indexFieldMap.put(excelProperty.index(), field);
        return;
    }
    int order = Integer.MAX_VALUE;
    if (excelProperty != null) {
        order = excelProperty.order();
    }
    List<Field> orderFieldList = orderFieldMap.computeIfAbsent(order, key -> ListUtils.newArrayList());
    orderFieldList.add(field);
}
Also used : Field(java.lang.reflect.Field) ExcelProperty(com.alibaba.excel.annotation.ExcelProperty) ExcelIgnore(com.alibaba.excel.annotation.ExcelIgnore) ExcelCommonException(com.alibaba.excel.exception.ExcelCommonException)

Aggregations

ExcelCommonException (com.alibaba.excel.exception.ExcelCommonException)5 ParseException (java.text.ParseException)3 ExcelProperty (com.alibaba.excel.annotation.ExcelProperty)2 Field (java.lang.reflect.Field)2 ExcelIgnore (com.alibaba.excel.annotation.ExcelIgnore)1 ContentFontStyle (com.alibaba.excel.annotation.write.style.ContentFontStyle)1 ContentStyle (com.alibaba.excel.annotation.write.style.ContentStyle)1 ExcelContentProperty (com.alibaba.excel.metadata.property.ExcelContentProperty)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1