com.alibaba.excel.metadata.property.ExcelContentProperty Java Examples

The following examples show how to use com.alibaba.excel.metadata.property.ExcelContentProperty. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EnumExcelConverter.java    From easyexcel-utils with Apache License 2.0 6 votes vote down vote up
@Override
public Enum convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
    String cellDataStr = cellData.getStringValue();

    EnumFormat annotation = contentProperty.getField().getAnnotation(EnumFormat.class);
    Class enumClazz = annotation.value();
    String[] fromExcel = annotation.fromExcel();
    String[] toJavaEnum = annotation.toJavaEnum();

    Enum anEnum = null;
    if (ArrayUtils.isNotEmpty(fromExcel) && ArrayUtils.isNotEmpty(toJavaEnum)) {
        Assert.isTrue(fromExcel.length == toJavaEnum.length, "fromExcel 与 toJavaEnum 的长度必须相同");
        for (int i = 0; i < fromExcel.length; i++) {
            if (Objects.equals(fromExcel[i], cellDataStr)) {
                anEnum = EnumUtils.getEnum(enumClazz, toJavaEnum[i]);
            }
        }
    } else {
        anEnum = EnumUtils.getEnum(enumClazz, cellDataStr);
    }

    Assert.notNull(anEnum, "枚举值不合法");
    return anEnum;
}
 
Example #2
Source File: EnumExcelConverter.java    From easyexcel-utils with Apache License 2.0 6 votes vote down vote up
@Override
public CellData convertToExcelData(Enum value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {

    String enumName = value.name();

    EnumFormat annotation = contentProperty.getField().getAnnotation(EnumFormat.class);
    String[] fromExcel = annotation.fromExcel();
    String[] toJavaEnum = annotation.toJavaEnum();

    if (ArrayUtils.isNotEmpty(fromExcel) && ArrayUtils.isNotEmpty(toJavaEnum)) {
        Assert.isTrue(fromExcel.length == toJavaEnum.length, "fromExcel 与 toJavaEnum 的长度必须相同");
        for (int i = 0; i < toJavaEnum.length; i++) {
            if (Objects.equals(toJavaEnum[i], enumName)) {
                return new CellData(fromExcel[i]);
            }
        }
    }
    return new CellData(enumName);
}
 
Example #3
Source File: ConverterUtils.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param cellData
 * @param clazz
 * @param contentProperty
 * @param converterMap
 * @param globalConfiguration
 * @param rowIndex
 * @param columnIndex
 * @return
 */
private static Object doConvertToJavaObject(CellData cellData, Class clazz, ExcelContentProperty contentProperty,
    Map<String, Converter> converterMap, GlobalConfiguration globalConfiguration, Integer rowIndex,
    Integer columnIndex) {
    Converter converter = null;
    if (contentProperty != null) {
        converter = contentProperty.getConverter();
    }
    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(cellData, contentProperty, globalConfiguration);
    } catch (Exception e) {
        throw new ExcelDataConvertException(rowIndex, columnIndex, cellData, contentProperty,
            "Convert data " + cellData + " to " + clazz + " error ", e);
    }
}
 
Example #4
Source File: StringNumberConverter.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
@Override
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    // If there are "DateTimeFormat", read as date
    if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
        return DateUtils.format(
            DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
                contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null),
            contentProperty.getDateTimeFormatProperty().getFormat());
    }
    // If there are "NumberFormat", read as number
    if (contentProperty != null && contentProperty.getNumberFormatProperty() != null) {
        return NumberUtils.format(cellData.getNumberValue(), contentProperty);
    }
    // Excel defines formatting
    if (cellData.getDataFormat() != null && !StringUtils.isEmpty(cellData.getDataFormatString())) {
        return NumberDataFormatterUtils.format(cellData.getNumberValue().doubleValue(), cellData.getDataFormat(),
            cellData.getDataFormatString(), globalConfiguration);
    }
    // Default conversion number
    return NumberUtils.format(cellData.getNumberValue(), contentProperty);
}
 
Example #5
Source File: DisabledConverter.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public CellData convertToExcelData(Integer integer, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    if (integer==0){
        return new CellData(ENABLE);
    }else {
        return new CellData(NOT_ENABLE);
    }
}
 
Example #6
Source File: BigDecimalBooleanConverter.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public CellData convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (BigDecimal.ONE.equals(value)) {
        return new CellData(Boolean.TRUE);
    }
    return new CellData(Boolean.FALSE);
}
 
Example #7
Source File: FloatBooleanConverter.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (ONE.equals(value)) {
        return new CellData(Boolean.TRUE);
    }
    return new CellData(Boolean.FALSE);
}
 
Example #8
Source File: DeleteConverter.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    String stringValue = cellData.getStringValue();
    if (DELETE.equals(stringValue)){
        return 1;
    }else {
        return 0;
    }
}
 
Example #9
Source File: AbstractExcelWriteExecutor.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
protected CellData convert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
    ExcelContentProperty excelContentProperty) {
    if (value == null) {
        return new CellData(CellDataTypeEnum.EMPTY);
    }
    // This means that the user has defined the data.
    if (value instanceof CellData) {
        CellData cellDataValue = (CellData)value;
        if (cellDataValue.getType() != null) {
            return cellDataValue;
        } else {
            if (cellDataValue.getData() == null) {
                cellDataValue.setType(CellDataTypeEnum.EMPTY);
                return cellDataValue;
            }
        }
        CellData cellDataReturn = doConvert(currentWriteHolder, cellDataValue.getData().getClass(), cell,
            cellDataValue.getData(), excelContentProperty);
        // The formula information is subject to user input
        if (cellDataValue.getFormula() != null) {
            cellDataReturn.setFormula(cellDataValue.getFormula());
            cellDataReturn.setFormulaValue(cellDataValue.getFormulaValue());
        }
        return cellDataReturn;
    }
    return doConvert(currentWriteHolder, clazz, cell, value, excelContentProperty);
}
 
Example #10
Source File: AbstractExcelWriteExecutor.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
protected CellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
    ExcelContentProperty excelContentProperty, Head head, Integer relativeRowIndex) {
    if (value == null) {
        return new CellData(CellDataTypeEnum.EMPTY);
    }
    if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) {
        value = ((String)value).trim();
    }
    CellData cellData = convert(currentWriteHolder, clazz, cell, value, excelContentProperty);
    if (cellData.getFormula() != null && cellData.getFormula()) {
        cell.setCellFormula(cellData.getFormulaValue());
    }
    if (cellData.getType() == null) {
        cellData.setType(CellDataTypeEnum.EMPTY);
    }
    WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE);
    switch (cellData.getType()) {
        case STRING:
            cell.setCellValue(cellData.getStringValue());
            return cellData;
        case BOOLEAN:
            cell.setCellValue(cellData.getBooleanValue());
            return cellData;
        case NUMBER:
            cell.setCellValue(cellData.getNumberValue().doubleValue());
            return cellData;
        case IMAGE:
            setImageValue(cellData, cell);
            return cellData;
        case EMPTY:
            return cellData;
        default:
            throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), cellData,
                excelContentProperty, "Not supported data:" + value + " return type:" + cell.getCellType()
                    + "at row:" + cell.getRow().getRowNum());
    }
}
 
Example #11
Source File: DateStringConverter.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public Date convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) throws ParseException {
    if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
        return DateUtils.parseDate(cellData.getStringValue(), null);
    } else {
        return DateUtils.parseDate(cellData.getStringValue(),
            contentProperty.getDateTimeFormatProperty().getFormat());
    }
}
 
Example #12
Source File: BoxingByteArrayImageConverter.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public CellData convertToExcelData(Byte[] value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    byte[] byteValue = new byte[value.length];
    for (int i = 0; i < value.length; i++) {
        byteValue[i] = value[i];
    }
    return new CellData(byteValue);
}
 
Example #13
Source File: BooleanNumberConverter.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public CellData convertToExcelData(Boolean value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (value) {
        return new CellData(BigDecimal.ONE);
    }
    return new CellData(BigDecimal.ZERO);
}
 
Example #14
Source File: BooleanNumberConverter.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (BigDecimal.ONE.compareTo(cellData.getNumberValue()) == 0) {
        return Boolean.TRUE;
    }
    return Boolean.FALSE;
}
 
Example #15
Source File: ShortBooleanConverter.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (cellData.getBooleanValue()) {
        return ONE;
    }
    return ZERO;
}
 
Example #16
Source File: DateNumberConverter.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public Date convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
        return DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
            globalConfiguration.getUse1904windowing(), null);
    } else {
        return DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
            contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null);
    }
}
 
Example #17
Source File: DisabledConverter.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public CellData convertToExcelData(Integer integer, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    if (integer==0){
        return new CellData(ENABLE);
    }else {
        return new CellData(NOT_ENABLE);
    }
}
 
Example #18
Source File: DeleteConverter.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    String stringValue = cellData.getStringValue();
    if (DELETE.equals(stringValue)){
        return 1;
    }else {
        return 0;
    }
}
 
Example #19
Source File: DeleteConverter.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public CellData convertToExcelData(Integer integer, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    if (integer==0){
        return new CellData(NOT_DELETE);
    }else {
        return new CellData(DELETE);
    }
}
 
Example #20
Source File: ModelBuildEventListener.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
private Object buildUserModel(Map<Integer, CellData> cellDataMap, ReadHolder currentReadHolder,
    AnalysisContext context) {
    ExcelReadHeadProperty excelReadHeadProperty = currentReadHolder.excelReadHeadProperty();
    Object resultModel;
    try {
        resultModel = excelReadHeadProperty.getHeadClazz().newInstance();
    } catch (Exception e) {
        throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), 0,
            new CellData(CellDataTypeEnum.EMPTY), null,
            "Can not instance class: " + excelReadHeadProperty.getHeadClazz().getName(), e);
    }
    Map<Integer, Head> headMap = excelReadHeadProperty.getHeadMap();
    Map<String, Object> map = new HashMap<String, Object>(headMap.size() * 4 / 3 + 1);
    Map<Integer, ExcelContentProperty> contentPropertyMap = excelReadHeadProperty.getContentPropertyMap();
    for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
        Integer index = entry.getKey();
        if (!cellDataMap.containsKey(index)) {
            continue;
        }
        CellData cellData = cellDataMap.get(index);
        if (cellData.getType() == CellDataTypeEnum.EMPTY) {
            continue;
        }
        ExcelContentProperty excelContentProperty = contentPropertyMap.get(index);
        Object value = ConverterUtils.convertToJavaObject(cellData, excelContentProperty.getField(),
            excelContentProperty, currentReadHolder.converterMap(), currentReadHolder.globalConfiguration(),
            context.readRowHolder().getRowIndex(), index);
        if (value != null) {
            map.put(excelContentProperty.getField().getName(), value);
        }
    }
    BeanMap.create(resultModel).putAll(map);
    return resultModel;
}
 
Example #21
Source File: IntegerBooleanConverter.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (cellData.getBooleanValue()) {
        return ONE;
    }
    return ZERO;
}
 
Example #22
Source File: DeleteConverter.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    String stringValue = cellData.getStringValue();
    if (DELETE.equals(stringValue)){
        return 1;
    }else {
        return 0;
    }
}
 
Example #23
Source File: LongBooleanConverter.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (cellData.getBooleanValue()) {
        return ONE;
    }
    return ZERO;
}
 
Example #24
Source File: ShortBooleanConverter.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (ONE.equals(value)) {
        return new CellData(Boolean.TRUE);
    }
    return new CellData(Boolean.FALSE);
}
 
Example #25
Source File: BigDecimalBooleanConverter.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (cellData.getBooleanValue()) {
        return BigDecimal.ONE;
    }
    return BigDecimal.ZERO;
}
 
Example #26
Source File: IntegerBooleanConverter.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty,
    GlobalConfiguration globalConfiguration) {
    if (ONE.equals(value)) {
        return new CellData(Boolean.TRUE);
    }
    return new CellData(Boolean.FALSE);
}
 
Example #27
Source File: ConverterUtils.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
/**
 * Convert it into a Java object
 *
 * @param cellData
 * @param field
 * @param contentProperty
 * @param converterMap
 * @param globalConfiguration
 * @param rowIndex
 * @param columnIndex
 * @return
 */
public static Object convertToJavaObject(CellData cellData, Field field, ExcelContentProperty contentProperty,
    Map<String, Converter> converterMap, GlobalConfiguration globalConfiguration, Integer rowIndex,
    Integer columnIndex) {
    Class clazz;
    if (field == null) {
        clazz = String.class;
    } else {
        clazz = field.getType();
    }
    if (clazz == CellData.class) {
        Type type = field.getGenericType();
        Class classGeneric;
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType)type;
            classGeneric = (Class)parameterizedType.getActualTypeArguments()[0];
        } else {
            classGeneric = String.class;
        }
        CellData cellDataReturn = new CellData(cellData);
        cellDataReturn.setData(doConvertToJavaObject(cellData, classGeneric, contentProperty, converterMap,
            globalConfiguration, rowIndex, columnIndex));
        return cellDataReturn;
    }
    return doConvertToJavaObject(cellData, clazz, contentProperty, converterMap, globalConfiguration, rowIndex,
        columnIndex);
}
 
Example #28
Source File: NumberUtils.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
/**
 * parse
 *
 * @param string
 * @param contentProperty
 * @return
 * @throws ParseException
 */
private static Number parse(String string, ExcelContentProperty contentProperty) throws ParseException {
    String format = contentProperty.getNumberFormatProperty().getFormat();
    RoundingMode roundingMode = contentProperty.getNumberFormatProperty().getRoundingMode();
    DecimalFormat decimalFormat = new DecimalFormat(format);
    decimalFormat.setRoundingMode(roundingMode);
    decimalFormat.setParseBigDecimal(true);
    return decimalFormat.parse(string);
}
 
Example #29
Source File: NumberUtils.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
/**
 * parse
 *
 * @param string
 * @param contentProperty
 * @return
 */
public static BigDecimal parseBigDecimal(String string, ExcelContentProperty contentProperty)
    throws ParseException {
    if (!hasFormat(contentProperty)) {
        return new BigDecimal(string);
    }
    return BigDecimal.valueOf(parse(string, contentProperty).doubleValue());
}
 
Example #30
Source File: AbstractExcelWriteExecutor.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
private CellData doConvert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
    ExcelContentProperty excelContentProperty) {
    Converter converter = null;
    if (excelContentProperty != null) {
        converter = excelContentProperty.getConverter();
    }
    if (converter == null) {
        converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz));
    }
    if (converter == null) {
        throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(),
            new CellData(CellDataTypeEnum.EMPTY), excelContentProperty,
            "Can not find 'Converter' support class " + clazz.getSimpleName() + ".");
    }
    CellData cellData;
    try {
        cellData =
            converter.convertToExcelData(value, excelContentProperty, currentWriteHolder.globalConfiguration());
    } catch (Exception e) {
        throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(),
            new CellData(CellDataTypeEnum.EMPTY), excelContentProperty,
            "Convert data:" + value + " error,at row:" + cell.getRow().getRowNum(), e);
    }
    if (cellData == null || cellData.getType() == null) {
        throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(),
            new CellData(CellDataTypeEnum.EMPTY), excelContentProperty,
            "Convert data:" + value + " return null,at row:" + cell.getRow().getRowNum());
    }
    return cellData;
}