com.alibaba.excel.converters.Converter Java Examples

The following examples show how to use com.alibaba.excel.converters.Converter. 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: 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 #2
Source File: AbstractParameterBuilder.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
/**
 * Custom type conversions override the default.
 *
 * @param converter
 * @return
 */
public T registerConverter(Converter converter) {
    if (parameter().getCustomConverterList() == null) {
        parameter().setCustomConverterList(new ArrayList<Converter>());
    }
    parameter().getCustomConverterList().add(converter);
    return self();
}
 
Example #3
Source File: ConverterUtils.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
/**
 * Convert it into a String map
 *
 * @param cellDataMap
 * @param context
 * @return
 */
public static Map<Integer, String> convertToStringMap(Map<Integer, CellData> cellDataMap, AnalysisContext context) {
    Map<Integer, String> stringMap = new HashMap<Integer, String>(cellDataMap.size() * 4 / 3 + 1);
    ReadHolder currentReadHolder = context.currentReadHolder();
    int index = 0;
    for (Map.Entry<Integer, CellData> entry : cellDataMap.entrySet()) {
        Integer key = entry.getKey();
        CellData cellData = entry.getValue();
        while (index < key) {
            stringMap.put(index, null);
            index++;
        }
        index++;
        if (cellData.getType() == CellDataTypeEnum.EMPTY) {
            stringMap.put(key, null);
            continue;
        }
        Converter converter =
            currentReadHolder.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(cellData, null, currentReadHolder.globalConfiguration())));
        } catch (Exception e) {
            throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), key, cellData, null,
                "Convert data " + cellData + " to String error ", e);
        }
    }
    return stringMap;
}
 
Example #4
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 #5
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;
}
 
Example #6
Source File: ExcelContentProperty.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
public Converter getConverter() {
    return converter;
}
 
Example #7
Source File: ExcelContentProperty.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
public void setConverter(Converter converter) {
    this.converter = converter;
}
 
Example #8
Source File: BasicParameter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
public List<Converter> getCustomConverterList() {
    return customConverterList;
}
 
Example #9
Source File: BasicParameter.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
public void setCustomConverterList(List<Converter> customConverterList) {
    this.customConverterList = customConverterList;
}
 
Example #10
Source File: AbstractHolder.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
public Map<String, Converter> getConverterMap() {
    return converterMap;
}
 
Example #11
Source File: AbstractHolder.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
public void setConverterMap(Map<String, Converter> converterMap) {
    this.converterMap = converterMap;
}
 
Example #12
Source File: AbstractHolder.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Converter> converterMap() {
    return getConverterMap();
}
 
Example #13
Source File: AbstractReadHolder.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
public AbstractReadHolder(ReadBasicParameter readBasicParameter, AbstractReadHolder parentAbstractReadHolder,
    Boolean convertAllFiled) {
    super(readBasicParameter, parentAbstractReadHolder);
    if (readBasicParameter.getUse1904windowing() == null && parentAbstractReadHolder != null) {
        getGlobalConfiguration()
            .setUse1904windowing(parentAbstractReadHolder.getGlobalConfiguration().getUse1904windowing());
    } else {
        getGlobalConfiguration().setUse1904windowing(readBasicParameter.getUse1904windowing());
    }

    if (readBasicParameter.getUseScientificFormat() == null) {
        if (parentAbstractReadHolder == null) {
            getGlobalConfiguration().setUseScientificFormat(Boolean.FALSE);
        } else {
            getGlobalConfiguration()
                .setUseScientificFormat(parentAbstractReadHolder.getGlobalConfiguration().getUseScientificFormat());
        }
    } else {
        getGlobalConfiguration().setUseScientificFormat(readBasicParameter.getUseScientificFormat());
    }

    // Initialization property
    this.excelReadHeadProperty = new ExcelReadHeadProperty(this, getClazz(), getHead(), convertAllFiled);
    if (readBasicParameter.getHeadRowNumber() == null) {
        if (parentAbstractReadHolder == null) {
            if (excelReadHeadProperty.hasHead()) {
                this.headRowNumber = excelReadHeadProperty.getHeadRowNumber();
            } else {
                this.headRowNumber = 1;
            }
        } else {
            this.headRowNumber = parentAbstractReadHolder.getHeadRowNumber();
        }
    } else {
        this.headRowNumber = readBasicParameter.getHeadRowNumber();
    }

    if (parentAbstractReadHolder == null) {
        this.readListenerList = new ArrayList<ReadListener>();
    } else {
        this.readListenerList = new ArrayList<ReadListener>(parentAbstractReadHolder.getReadListenerList());
    }
    if (HolderEnum.WORKBOOK.equals(holderType())) {
        Boolean useDefaultListener = ((ReadWorkbook)readBasicParameter).getUseDefaultListener();
        if (useDefaultListener == null || useDefaultListener) {
            readListenerList.add(new ModelBuildEventListener());
        }
    }
    if (readBasicParameter.getCustomReadListenerList() != null
        && !readBasicParameter.getCustomReadListenerList().isEmpty()) {
        this.readListenerList.addAll(readBasicParameter.getCustomReadListenerList());
    }

    if (parentAbstractReadHolder == null) {
        setConverterMap(DefaultConverterLoader.loadDefaultReadConverter());
    } else {
        setConverterMap(new HashMap<String, Converter>(parentAbstractReadHolder.getConverterMap()));
    }
    if (readBasicParameter.getCustomConverterList() != null
        && !readBasicParameter.getCustomConverterList().isEmpty()) {
        for (Converter converter : readBasicParameter.getCustomConverterList()) {
            getConverterMap().put(
                ConverterKeyBuild.buildKey(converter.supportJavaTypeKey(), converter.supportExcelTypeKey()),
                converter);
        }
    }
}
 
Example #14
Source File: ConfigurationHolder.java    From easyexcel with Apache License 2.0 2 votes vote down vote up
/**
 * What converter does the currently operated cell need to execute
 *
 * @return Converter
 */
Map<String, Converter> converterMap();