package com.sysn.mysql.service.impl; import cn.hutool.poi.excel.ExcelReader; import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelWriter; import org.apache.commons.collections4.MapUtils; import java.util.*; /** * @Author: WeiZJ * @Date: 2023/8/8 10:33 * @Description: */ public class testHtje { public static void main(String[] args) { //右表 ExcelReader reader = ExcelUtil.getReader("C:\\Users\\admin\\Desktop\\新建文件夹 (2)\\m1.xlsx"); List<Map<String, Object>> readAll = reader.readAll(); Iterator<Map<String, Object>> iterator = readAll.iterator(); Map<String, Map<String, Object>> rightMap = new HashMap<>(); while (iterator.hasNext()) { Map<String, Object> next = iterator.next(); String eId = String.valueOf(next.get("id")); Map<String, Object> entity = rightMap.get(eId); if (MapUtils.isEmpty(entity)) { rightMap.put(eId, next); } } ExcelReader reader2 = ExcelUtil.getReader("C:\\Users\\admin\\Desktop\\新建文件夹 (2)\\CRM数据分析.xlsx"); List<Map<String, Object>> readAll2 = reader2.readAll(); Iterator<Map<String, Object>> iterator2 = readAll2.iterator(); Map<String, Map<String, Object>> leftMap = new HashMap<>(); while (iterator2.hasNext()) { Map<String, Object> next2 = iterator2.next(); String eId = String.valueOf(next2.get("id")); Map<String, Object> entity = leftMap.get(eId); if (MapUtils.isEmpty(entity)) { leftMap.put(eId, next2); } } List<Map<String, Object>> list = new ArrayList<>(); rightMap.entrySet().forEach(c -> { String key = c.getKey(); Map<String, Object> left = leftMap.get(key); if (MapUtils.isNotEmpty(left)) { String ht = (String) left.get("是否签合同"); String xm = (String) left.get("是否项目服务"); String zs = (String) left.get("是否有知识产权"); String qycj = String.valueOf(left.get("企业创建时间")); String htlr = String.valueOf(left.get("合同录入时间")); String xt = String.valueOf(left.get("系统")); String hrye = String.valueOf(left.get("合同余额")); Map<String, Object> right = c.getValue(); right.put("是否签合同", ht); right.put("是否项目服务", xm); right.put("是否有知识产权", zs); right.put("企业创建时间", qycj); right.put("合同录入时间", htlr); right.put("系统", xt); right.put("合同余额", hrye); list.add(right); } }); ExcelWriter writer = ExcelUtil.getWriter("C:\\Users\\admin\\Desktop\\新建文件夹 (2)\\m2.xlsx"); writer.passCurrentRow(); writer.write(list, true); writer.close(); } }
excel根据id相同把一边字段拼到另一个
最新推荐文章于 2024-10-30 18:07:32 发布