poi对excel进行读取

时间:2025-04-01 10:19:01
public class ExcelReflect { /** * 从object中获取execel注解字段的值 * @param obj object实体对象 * @param excelFieldMap excel字段集合 * @return 值集合 */ public static Map<String, String> getFieldsValue(Object obj, Map<String, Object> excelFieldMap) { Map<String, String> valueMap = new HashedMap<String, String>(); for (String fieldName : excelFieldMap.keySet()) { AppCloudExcel appCloudExcel = (AppCloudExcel) excelFieldMap.get(fieldName); valueMap.put(appCloudExcel.serial() + "", fieldExtValue(fieldName, obj).toString()); } return valueMap; } /** * 获取class类中注解excel的字段集合 * @param clazz class类 * @return excel字段集合 */ public static <T> Map<String, Object> getClassExcelFieldsList(Class<T> clazz) { Field[] fields = clazz.getDeclaredFields(); Map<String, Object> excelFieldMap = new HashedMap<String, Object>(); for (int j = 0; j < fields.length; j++) { AppCloudExcel appCloudExcel = fields[j].getAnnotation(AppCloudExcel.class); if (null != appCloudExcel) { excelFieldMap.put(fields[j].getName(), fields[j].getAnnotation(AppCloudExcel.class)); } } return excelFieldMap; } /** * list的map集合转换为list的object集合 * @param listMap list的map集合 * @param clazz class类 * @return list的object集合 */ @SuppressWarnings({ "unchecked" }) public static <T> List<T> listMapToListObj(List<Map<String, String>> listMap, Class<T> clazz) { List<Object> objList = new ArrayList<>(); try { Field[] fields = clazz.getDeclaredFields(); Map<String, Field> excelFieldMap = new HashedMap<String, Field>(); for (int j = 0; j < fields.length; j++) { int serial = fields[j].getAnnotation(AppCloudExcel.class).serial(); excelFieldMap.put(serial + "", fields[j]); } for (Map<String, String> map : listMap) { Object obj = clazz.newInstance(); for (String key : excelFieldMap.keySet()) { String fieldValue = map.get(key); Field field = excelFieldMap.get(key); obj = fieldAssValue(obj, field, fieldValue); } objList.add(obj); } } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } return (List<T>) objList; } /** * 根据字段名获取字段值 * @param fieldName 字段名 * @param obj 实体 * @return 字段值 */ public static Object fieldExtValue(String fieldName, Object obj) { try { Field[] fields = obj.getClass().getDeclaredFields(); for (Field field : fields) { if (fieldName.equals(field.getName())) { Method method = obj.getClass().getMethod("get" + StringUtils.capitalize(fieldName)); return method.invoke(obj); } } } catch (SecurityException | IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return null; } /** * 字段赋值 * @param obj 实体对象 * @param field 属性对象 * @param value 属性值 * @return */ private static Object fieldAssValue(Object obj, Field field, Object value) { if (null == value || StringUtils.isBlank(value.toString())) { return obj; } try { Method method = null; //此处使用else if减少频繁判断,当条件满足则执行代码,其他else if将不再执行。 if (field.getType() == String.class) { //string类型 method = obj.getClass().getMethod("set" + StringUtils.capitalize(field.getName()), String.class); method.invoke(obj, String.valueOf(value)); } else if (field.getType() == Long.class || field.getType() == long.class) { //long型 method = obj.getClass().getMethod("set" + StringUtils.capitalize(field.getName()), Long.class); method.invoke(obj, new Long(Long.parseLong(value.toString()))); } else if (field.getType() == Integer.class || field.getType() == int.class) { //int型 method = obj.getClass().getMethod("set" + StringUtils.capitalize(field.getName()), Integer.class); method.invoke(obj, new Integer(Integer.parseInt(value.toString()))); } else if (field.getType() == Double.class || field.getType() == double.class) { //double型 method = obj.getClass().getMethod("set" + StringUtils.capitalize(field.getName()), Double.class); method.invoke(obj, new Double(Double.parseDouble(value.toString()))); } else if (field.getType() == Date.class) { //date型 method = obj.getClass().getMethod("set" + StringUtils.capitalize(field.getName()), Date.class); SimpleDateFormat smdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = smdf.parse(value.toString()); method.invoke(obj, date); } else if (field.getType() == Boolean.class || field.getType() == boolean.class) { //boolean型 method = obj.getClass().getMethod("set" + StringUtils.capitalize(field.getName()), Boolean.class); method.invoke(obj, new Boolean(Boolean.parseBoolean(value.toString()))); } else if (field.getType() == Short.class || field.getType() == short.class) { //short型 method = obj.getClass().getMethod("set" + StringUtils.capitalize(field.getName()), Short.class); method.invoke(obj, new Short(Short.parseShort(value.toString()))); } } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ParseException e) { e.printStackTrace(); } return obj; } }