基于反射实现通用的Excel读取文件

该博客介绍了一个通用的工具类,通过反射机制从Excel文件中读取数据。首先,依据配置文件获取Excel的列信息,然后将数据转换成Map对象。配置文件指定列的索引和类型,核心转换代码实现数据的解析和映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通用的读取excel文件的工具类, 先根据配置文件依次读取Excel文件所对应的行列信息, 然后转为map对象

private HashMap<String,ExcelPropertyPo> transformExcelPropertyJson(String excelPropertyJson){
// 读取配置文件中配置的行列对应的关系, 然后使用fastJson转为map对象备用
    HashMap<String, ExcelPropertyPo> excelPropertyPoMap = JSON.parseObject(excelPropertyJson, new TypeReference<HashMap<String, ExcelPropertyPo>>(){});
    return excelPropertyPoMap;
}


配置文件表示:

EXCEL_PROPERTY_JSON={"0":{"propertyName":"goodsId","propertyType":"Integer"},"8":{"propertyName":"storeId","propertyType":"Integer"}}

里面"0", "8" 表示的是excel中的第几列数据, propertyName 表示的是Excel中的列名, propertyType 表示的是类型


转换核心代码:

 /**
     *
     * @param sheet excel读取的excel中的数据
     * @param obj   excel中每一行转换对应的对象
     * @param excelPropertyPoMap    excel与对象字段的对应关系,从配置文件中读取
     * @param <T> 泛型支持
     * @return
     * @throws Exception
     */
    private <T> List<T> transformSheetToBean(Sheet sheet, T obj, HashMap<String,ExcelPropertyPo> excelPropertyPoMap) throws Exception{
        List<T> objList = new ArrayList<>();
        Class<?> clazz =obj.getClass();
        for (int i = 1; i <= sheet.getRows() - 1; i++) {
            // 必须new一个对象, 不然会重复操作该对象
            T temp= (T) clazz.newInstance();
            for (Map.Entry<String, ExcelPropertyPo> entry : excelPropertyPoMap.entrySet()) {
                // 获取到每一个单元格的内容
                String excelCellValue = sheet.getCell(Integer.parseInt(entry.getKey()), i).getContents();
                // 根据反射设置对应的数值
                temp= setPropertyValueByName(entry.getValue().getPropertyName(), excelCellValue,temp);
            }
            objList.add(temp);
        }
        return objList;
    }


    /**
     * 基于反射获取对应的值
     * @param propertyName  字段名
     * @param excelCellValue    字段值
     * @param obj   所属对象
     * @param <T>
     * @return
     * @throws Exception
     */
    private <T> T setPropertyValueByName(String propertyName, String excelCellValue, T obj) throws Exception {
        // 首字母大写
        String setPropertyName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
        Class<?> clazz =obj.getClass();
        // 获取到字段的类型
        String typeName = clazz.getDeclaredField(propertyName).getType().getName();
        if ("java.lang.String".equals(typeName)) {
            Method method = obj.getClass().getMethod(setPropertyName, String.class);
            if (method != null) {
                method.invoke(obj, new Object[]{excelCellValue});
            }
        } else if ("java.math.BigDecimal".equals(typeName)) {
            Method method = obj.getClass().getMethod(setPropertyName, BigDecimal.class);
            if (method != null) {
                method.invoke(obj, new Object[]{new BigDecimal(excelCellValue)});
            }
        } else if ("java.lang.Integer".equals(typeName)) {
            Method method = obj.getClass().getMethod(setPropertyName, Integer.class);
            if (method != null) {
                method.invoke(obj, new Object[]{Integer.valueOf(excelCellValue.trim())});
            }
        }
        return obj;
}

附上ExcelPropertyPo类
public class ExcelPropertyPo {
    private  String propertyName;
    private  String propertyType;

    public String getPropertyName() {
        return propertyName;
    }

    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }

    public String getPropertyType() {
        return propertyType;
    }

    public void setPropertyType(String propertyType) {
        this.propertyType = propertyType;
    }
}

使用以上方式进行处理之后, 这样后续如果需要读取另外一个excel中的数据, 则只需要改变excel的配置文件以及对应的对象即可, 主要逻辑将无需改变..


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值