ModelBuildEventListener.java 4.7 KB
Newer Older
Z
zhuangjiaju 已提交
1 2
package com.alibaba.excel.read.listener;

Z
zhuangjiaju 已提交
3
import java.util.ArrayList;
Z
zhuangjiaju 已提交
4
import java.util.HashMap;
Z
zhuangjiaju 已提交
5
import java.util.List;
Z
zhuangjiaju 已提交
6 7 8 9 10
import java.util.Map;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.enums.HeadKindEnum;
11
import com.alibaba.excel.event.AbstractIgnoreExceptionReadListener;
Z
zhuangjiaju 已提交
12 13 14 15
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
16 17
import com.alibaba.excel.read.metadata.holder.ReadHolder;
import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty;
庄家钜's avatar
庄家钜 已提交
18
import com.alibaba.excel.util.ConverterUtils;
Z
zhuangjiaju 已提交
19 20 21 22 23

import net.sf.cglib.beans.BeanMap;

/**
 * Convert to the object the user needs
24
 *
Z
zhuangjiaju 已提交
25 26
 * @author jipengfei
 */
Z
zhuangjiaju 已提交
27
public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener<Map<Integer, CellData>> {
Z
zhuangjiaju 已提交
28

庄家钜's avatar
庄家钜 已提交
29 30 31
    @Override
    public void invokeHead(Map<Integer, CellData> cellDataMap, AnalysisContext context) {}

Z
zhuangjiaju 已提交
32
    @Override
Z
zhuangjiaju 已提交
33
    public void invoke(Map<Integer, CellData> cellDataMap, AnalysisContext context) {
34 35
        ReadHolder currentReadHolder = context.currentReadHolder();
        if (HeadKindEnum.CLASS.equals(currentReadHolder.excelReadHeadProperty().getHeadKind())) {
Z
zhuangjiaju 已提交
36
            context.readRowHolder().setCurrentRowAnalysisResult(buildUserModel(cellDataMap, currentReadHolder));
Z
zhuangjiaju 已提交
37 38
            return;
        }
Z
zhuangjiaju 已提交
39
        context.readRowHolder().setCurrentRowAnalysisResult(buildStringList(cellDataMap, currentReadHolder, context));
Z
zhuangjiaju 已提交
40 41
    }

Z
zhuangjiaju 已提交
42 43 44 45 46 47 48 49 50 51
    private Object buildStringList(Map<Integer, CellData> cellDataMap, ReadHolder currentReadHolder,
        AnalysisContext context) {
        if (context.readWorkbookHolder().getDefaultReturnMap()) {
            Map<Integer, String> map = new HashMap<Integer, String>(cellDataMap.size() * 4 / 3 + 1);
            for (Map.Entry<Integer, CellData> entry : cellDataMap.entrySet()) {
                CellData cellData = entry.getValue();
                if (cellData.getType() == CellDataTypeEnum.EMPTY) {
                    map.put(entry.getKey(), null);
                    continue;
                }
52
                map.put(entry.getKey(), (String)ConverterUtils.convertToJavaObject(cellData, null, null,
Z
zhuangjiaju 已提交
53
                    currentReadHolder.converterMap(), currentReadHolder.globalConfiguration()));
Z
zhuangjiaju 已提交
54
            }
Z
zhuangjiaju 已提交
55 56 57 58 59 60 61 62 63 64
            return map;
        } else {
            // Compatible with the old code the old code returns a list
            List<String> list = new ArrayList<String>();
            for (Map.Entry<Integer, CellData> entry : cellDataMap.entrySet()) {
                CellData cellData = entry.getValue();
                if (cellData.getType() == CellDataTypeEnum.EMPTY) {
                    list.add(null);
                    continue;
                }
65
                list.add((String)ConverterUtils.convertToJavaObject(cellData, null, null,
庄家钜's avatar
庄家钜 已提交
66
                    currentReadHolder.converterMap(), currentReadHolder.globalConfiguration()));
Z
zhuangjiaju 已提交
67 68
            }
            return list;
Z
zhuangjiaju 已提交
69 70 71
        }
    }

Z
zhuangjiaju 已提交
72
    private Object buildUserModel(Map<Integer, CellData> cellDataMap, ReadHolder currentReadHolder) {
73
        ExcelReadHeadProperty excelReadHeadProperty = currentReadHolder.excelReadHeadProperty();
Z
zhuangjiaju 已提交
74 75
        Object resultModel;
        try {
76
            resultModel = excelReadHeadProperty.getHeadClazz().newInstance();
Z
zhuangjiaju 已提交
77
        } catch (Exception e) {
78 79
            throw new ExcelDataConvertException(
                "Can not instance class: " + excelReadHeadProperty.getHeadClazz().getName(), e);
Z
zhuangjiaju 已提交
80
        }
81
        Map<Integer, Head> headMap = excelReadHeadProperty.getHeadMap();
82
        Map<String, Object> map = new HashMap<String, Object>(headMap.size() * 4 / 3 + 1);
83
        Map<Integer, ExcelContentProperty> contentPropertyMap = excelReadHeadProperty.getContentPropertyMap();
Z
zhuangjiaju 已提交
84 85
        for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
            Integer index = entry.getKey();
Z
zhuangjiaju 已提交
86
            if (!cellDataMap.containsKey(index)) {
Z
zhuangjiaju 已提交
87 88
                continue;
            }
Z
zhuangjiaju 已提交
89
            CellData cellData = cellDataMap.get(index);
Z
zhuangjiaju 已提交
90 91 92 93
            if (cellData.getType() == CellDataTypeEnum.EMPTY) {
                continue;
            }
            ExcelContentProperty excelContentProperty = contentPropertyMap.get(index);
94
            Object value = ConverterUtils.convertToJavaObject(cellData, excelContentProperty.getField(),
庄家钜's avatar
庄家钜 已提交
95
                excelContentProperty, currentReadHolder.converterMap(), currentReadHolder.globalConfiguration());
Z
zhuangjiaju 已提交
96 97 98 99 100 101 102 103 104 105 106
            if (value != null) {
                map.put(excelContentProperty.getField().getName(), value);
            }
        }
        BeanMap.create(resultModel).putAll(map);
        return resultModel;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {}
}