excel 工具类 @author zc
| 27 | * @author zc |
| 28 | */ |
| 29 | public class ExcelUtils { |
| 30 | /** |
| 31 | * 正则表达式:否为整数或者浮点数,涵盖负数 |
| 32 | */ |
| 33 | private static final Pattern NUMBER_PATTERN = Pattern.compile("-?\\d+(\\.\\d+)?"); |
| 34 | |
| 35 | |
| 36 | |
| 37 | private static Logger log = LoggerFactory.getLogger(ExcelUtils.class); |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * java对象转map |
| 42 | * @param obj |
| 43 | * @return |
| 44 | */ |
| 45 | public static Map<?, ?> objectToMap(Object obj) { |
| 46 | Map userMap = new HashMap(); |
| 47 | if (obj != null) { |
| 48 | userMap = JSON.parseObject(JSON.toJSONString(obj), new TypeReference<Map<String, String>>() { |
| 49 | }); |
| 50 | } |
| 51 | return userMap; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * java对象转map |
| 56 | * @param objList |
| 57 | * @return |
| 58 | */ |
| 59 | public static List<Map<String, Object>> objectToMapList(List objList) { |
| 60 | List<Map<String, Object>> objMapList = new ArrayList<>(); |
| 61 | if (ObjectUtil.isNotEmpty(objList)) { |
| 62 | objMapList = JSONArray.parseObject(JSONArray.parseArray(JSON.toJSONString(objList)).toJSONString(), List.class); |
| 63 | } |
| 64 | return objMapList; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * excel 对象数据校验 |
| 69 | * @param excelList |
| 70 | * @param clazz |
| 71 | * @return |
| 72 | */ |
| 73 | public static List<String> excelCheckObj(List<?> excelList, Class<?> clazz) { |
| 74 | List<String> errList = new ArrayList<>(); |
| 75 | Map<String, ExcelCheck> excelCheckMap = new HashMap<String, ExcelCheck>(); |
| 76 | Map<String, ExcelProperty> excelPropertyMap = new HashMap<String, ExcelProperty>(); |
| 77 | for (Field field : TableInfoHelper.getAllFields(clazz)) { |
| 78 | ExcelCheck excelCheck = field.getAnnotation(ExcelCheck.class); |
| 79 | ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); |
| 80 | if (excelCheck != null) { |
| 81 | excelCheckMap.put(field.getName(), excelCheck); |
| 82 | excelPropertyMap.put(field.getName(), excelProperty); |
| 83 | } |
| 84 | } |
| 85 | for (int i = 0; i < excelList.size(); i++) { |
| 86 | Object obj = excelList.get(i); |
nothing calls this directly
no outgoing calls
no test coverage detected