功能描述:将查询出的map数组key1
和key2
相同的数据将value1
与value2
的数值求和,放入新的map数组中,同时保留原始字段。
List<Map> listOld = supplierDistributionService.getSupplierOrderDetail_Detail(map3);
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Map<String, Object>> tempMap = new HashMap<>();
for (Map<String, Object> map : listOld) {
String key = map.get("key1").toString() + "_" + (map.get("key2") == null ? "" : map.get("key2").toString());
if (tempMap.containsKey(key)) {
Map<String, Object> existingMap = tempMap.get(key);
int value1 = Integer.parseInt(existingMap.get("value1") == null ? "0" : existingMap.get("value1").toString());
existingMap.put("value1", drugsInspectionNum + Integer.parseInt(map.get("value1") == null ? "0" : map.get("value1").toString()));
int value2 = Integer.parseInt(existingMap.get("value2") == null ? "0" : existingMap.get("value2").toString());
existingMap.put("value2", drugsDistributionNum + Integer.parseInt(map.get("value2") == null ? "0" : map.get("value2").toString()));
} else {
Map<String, Object> newMap = new HashMap<>(map);
tempMap.put(key, newMap);
list.add(newMap);
}
}
功能描述:使用poi合并单元格,计算相同数据的起始及结束位置。
int startIndex = IntStream.range(0, list.size())
.filter(item -> map1.get("key").equals(list.get(item).get("key")))
.findFirst()
.orElse(-1); // 如果不存在,则返回-1
// 获取结束位置
int endIndex = IntStream.range(0, list.size())
.filter(item -> map1.get("key").equals(list.get(item).get("key")))
.reduce((first, second) -> second) // 选取最后一个符合条件的索引
.orElse(-1);
//合并单元格(起始行,结束行,起始列,结束列)
sheet.addMergedRegion(new CellRangeAddress(startIndex,endIndex, 0, 0));
错误操作:通过循环匹配重复的数据进行合并,每次起始行都是循环的上一行,导致合并单元格时看起来已经合并,但部分数据显示不全问题。
if (map1.get("key").equals(list.get(i - 1).get("key"))) {
sheet.addMergedRegion(new CellRangeAddress(h - 1, h, 0, 0));
}