1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| public String test(MultipartFile file) throws IOException { Map<String, String> dictMap = new HashMap<>(); dictMap.put("项目", "xm"); dictMap.put("嗨一付订单编号", "hyfddbh"); try (InputStream inputStream = file.getInputStream()) { EasyExcel.read(inputStream, new ReadListener<Map<String, String>>(){ private Map<Integer, String> fieldHead;
@Override public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) { Map<Integer, String> integerStringMap = ConverterUtils.convertToStringMap(headMap, context); log.info("解析到一条头数据:{}", JSON.toJSONString(integerStringMap)); fieldHead = ExcelParsing.setFieldHead(integerStringMap, AgentDeliverOrderImportVo.class); log.info("转化后头数据:{}", JSONObject.toJSONString(fieldHead)); }
@Override public void invoke(Map<String, String> map, AnalysisContext analysisContext) { log.info("解析到一条数据:{}", JSON.toJSONString(map)); Map<String, String> valueMap = ExcelParsing.setFieldValue(fieldHead, dictMap, map); log.info("转化一条数据:{}", JSONObject.toJSONString(valueMap)); log.info("转化一条动态数据:{}", JSONObject.toJSONString(ExcelParsing.getValueMap(valueMap, AgentDeliverOrderImportVo.class))); }
@Override public void doAfterAllAnalysed(AnalysisContext analysisContext) {
} }).sheet().doRead(); } return "完成"; }
class ExcelParsing {
public static Map<Integer, String> setFieldHead(Map<Integer, String> headMap, Class<?> obj) { Field[] fields = obj.getDeclaredFields(); for (Field field : fields) { ExcelProperty annotation = field.getAnnotation(ExcelProperty.class); if (annotation == null) { continue; } List<String> valueList = Arrays.asList(annotation.value()); for (Map.Entry<Integer, String> entry : headMap.entrySet()) { if (valueList.contains(entry.getValue())) { headMap.put(entry.getKey(), field.getName()); } } }
return headMap; }
public static Map<String, String> setFieldValue(Map<Integer, String> headMap, Map<String, String> dictMap, Map<String, String> valueMap) { Map<Integer, String> valueIntegerMap = valueMap.entrySet().stream().collect( Collectors.toMap(item -> Integer.valueOf(String.valueOf(item.getKey())), item -> StrUtil.nullToEmpty(item.getValue())) );
Map<String, String> valueResultMap = new HashMap<>(valueMap.size()); Iterator<Map.Entry<Integer, String>> iterator = valueIntegerMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<Integer, String> entry = iterator.next(); if (dictMap != null && dictMap.containsKey(headMap.get(entry.getKey()))) { valueResultMap.put(dictMap.get(headMap.get(entry.getKey())), entry.getValue()); continue; } valueResultMap.put(headMap.get(entry.getKey()), entry.getValue()); iterator.remove(); } return valueResultMap; }
public static Map<String, Object> getValueMap(Map<String, String> valueMap, Class<?> obj) { Map<String, Object> resultMap = new HashMap<>(valueMap); List<String> commonFieldList = new ArrayList<>(); Field[] fields = obj.getDeclaredFields(); for (Field field : fields) { ExcelProperty annotation = field.getAnnotation(ExcelProperty.class); if (annotation == null) { continue; } commonFieldList.add(field.getName()); } Map<String, String> dynamicMap = valueMap.entrySet().stream() .filter(item -> !commonFieldList.contains(item.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); resultMap.put("dynamicFields", dynamicMap);; return resultMap; } }
|