Search in sources :

Example 1 with ExcelReadHeadProperty

use of com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty in project easyexcel by alibaba.

the class ModelBuildEventListener method buildUserModel.

private Object buildUserModel(Map<Integer, ReadCellData<?>> cellDataMap, ReadSheetHolder readSheetHolder, AnalysisContext context) {
    ExcelReadHeadProperty excelReadHeadProperty = readSheetHolder.excelReadHeadProperty();
    Object resultModel;
    try {
        resultModel = excelReadHeadProperty.getHeadClazz().newInstance();
    } catch (Exception e) {
        throw new ExcelDataConvertException(context.readRowHolder().getRowIndex(), 0, new ReadCellData<>(CellDataTypeEnum.EMPTY), null, "Can not instance class: " + excelReadHeadProperty.getHeadClazz().getName(), e);
    }
    Map<Integer, Head> headMap = excelReadHeadProperty.getHeadMap();
    BeanMap dataMap = BeanMapUtils.create(resultModel);
    for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
        Integer index = entry.getKey();
        Head head = entry.getValue();
        String fieldName = head.getFieldName();
        if (!cellDataMap.containsKey(index)) {
            continue;
        }
        ReadCellData<?> cellData = cellDataMap.get(index);
        Object value = ConverterUtils.convertToJavaObject(cellData, head.getField(), ClassUtils.declaredExcelContentProperty(dataMap, readSheetHolder.excelReadHeadProperty().getHeadClazz(), fieldName), readSheetHolder.converterMap(), context, context.readRowHolder().getRowIndex(), index);
        if (value != null) {
            dataMap.put(fieldName, value);
        }
    }
    return resultModel;
}
Also used : Head(com.alibaba.excel.metadata.Head) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ReadCellData(com.alibaba.excel.metadata.data.ReadCellData) BeanMap(org.springframework.cglib.beans.BeanMap) BeanMap(org.springframework.cglib.beans.BeanMap) Map(java.util.Map) ExcelReadHeadProperty(com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)

Example 2 with ExcelReadHeadProperty

use of com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty in project diboot by dibo-software.

the class ReadExcelListener method invokeHead.

/**
 * excel表头数据
 */
@Override
public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
    this.headMap.clear();
    fieldHeadMap.clear();
    fieldNameMap.clear();
    headNameMap.clear();
    ExcelReadHeadProperty excelReadHeadProperty = context.currentReadHolder().excelReadHeadProperty();
    for (Map.Entry<Integer, Head> entry : excelReadHeadProperty.getHeadMap().entrySet()) {
        Integer index = entry.getKey();
        Head head = entry.getValue();
        String fieldName = head.getFieldName();
        List<String> headNameList = head.getHeadNameList();
        String name = headNameList.get(headNameList.size() - 1);
        this.headMap.put(index, name);
        fieldHeadMap.put(fieldName, name);
        fieldNameMap.put(index, fieldName);
        headNameMap.put(index, headNameList);
    }
}
Also used : Head(com.alibaba.excel.metadata.Head) ExcelReadHeadProperty(com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)

Example 3 with ExcelReadHeadProperty

use of com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty in project easyexcel by alibaba.

the class DefaultAnalysisEventProcessor method buildHead.

private void buildHead(AnalysisContext analysisContext, Map<Integer, ReadCellData<?>> cellDataMap) {
    if (!HeadKindEnum.CLASS.equals(analysisContext.currentReadHolder().excelReadHeadProperty().getHeadKind())) {
        return;
    }
    Map<Integer, String> dataMap = ConverterUtils.convertToStringMap(cellDataMap, analysisContext);
    ExcelReadHeadProperty excelHeadPropertyData = analysisContext.readSheetHolder().excelReadHeadProperty();
    Map<Integer, Head> headMapData = excelHeadPropertyData.getHeadMap();
    Map<Integer, Head> tmpHeadMap = new HashMap<Integer, Head>(headMapData.size() * 4 / 3 + 1);
    for (Map.Entry<Integer, Head> entry : headMapData.entrySet()) {
        Head headData = entry.getValue();
        if (headData.getForceIndex() || !headData.getForceName()) {
            tmpHeadMap.put(entry.getKey(), headData);
            continue;
        }
        List<String> headNameList = headData.getHeadNameList();
        String headName = headNameList.get(headNameList.size() - 1);
        for (Map.Entry<Integer, String> stringEntry : dataMap.entrySet()) {
            if (stringEntry == null) {
                continue;
            }
            String headString = stringEntry.getValue();
            Integer stringKey = stringEntry.getKey();
            if (StringUtils.isEmpty(headString)) {
                continue;
            }
            if (analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
                headString = headString.trim();
            }
            if (headName.equals(headString)) {
                headData.setColumnIndex(stringKey);
                tmpHeadMap.put(stringKey, headData);
                break;
            }
        }
    }
    excelHeadPropertyData.setHeadMap(tmpHeadMap);
}
Also used : Head(com.alibaba.excel.metadata.Head) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ExcelReadHeadProperty(com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)

Aggregations

Head (com.alibaba.excel.metadata.Head)3 ExcelReadHeadProperty (com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty)3 Map (java.util.Map)2 ExcelDataConvertException (com.alibaba.excel.exception.ExcelDataConvertException)1 ReadCellData (com.alibaba.excel.metadata.data.ReadCellData)1 HashMap (java.util.HashMap)1 BeanMap (org.springframework.cglib.beans.BeanMap)1