excel根据id相同把一边字段拼到另一个

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();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值