通用的读取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 表示的是类型
转换核心代码:
使用以上方式进行处理之后, 这样后续如果需要读取另外一个excel中的数据, 则只需要改变excel的配置文件以及对应的对象即可, 主要逻辑将无需改变../** * * @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; } }