Search in sources :

Example 1 with ReadConverterContext

use of com.alibaba.excel.converters.ReadConverterContext in project easyexcel by alibaba.

the class ConverterUtils method convertToStringMap.

/**
 * Convert it into a String map
 *
 * @param cellDataMap
 * @param context
 * @return
 */
public static Map<Integer, String> convertToStringMap(Map<Integer, ReadCellData<?>> cellDataMap, AnalysisContext context) {
    Map<Integer, String> stringMap = MapUtils.newHashMapWithExpectedSize(cellDataMap.size());
    ReadSheetHolder readSheetHolder = context.readSheetHolder();
    int index = 0;
    for (Map.Entry<Integer, ReadCellData<?>> entry : cellDataMap.entrySet()) {
        Integer key = entry.getKey();
        ReadCellData<?> cellData = entry.getValue();
        while (index < key) {
            stringMap.put(index, null);
            index++;
        }
        index++;
        if (cellData.getType() == CellDataTypeEnum.EMPTY) {
            stringMap.put(key, null);
            continue;
        }
        Converter<?> converter = readSheetHolder.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(new ReadConverterContext<>(cellData, null, context))));
        } catch (Exception e) {
            throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), key, cellData, null, "Convert data " + cellData + " to String error ", e);
        }
    }
    return stringMap;
}
Also used : ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ReadCellData(com.alibaba.excel.metadata.data.ReadCellData) ReadSheetHolder(com.alibaba.excel.read.metadata.holder.ReadSheetHolder) ReadConverterContext(com.alibaba.excel.converters.ReadConverterContext) Map(java.util.Map)

Aggregations

ReadConverterContext (com.alibaba.excel.converters.ReadConverterContext)1 ExcelDataConvertException (com.alibaba.excel.exception.ExcelDataConvertException)1 ReadCellData (com.alibaba.excel.metadata.data.ReadCellData)1 ReadSheetHolder (com.alibaba.excel.read.metadata.holder.ReadSheetHolder)1 Map (java.util.Map)1