计算本地Excel某两列的差异值

需求:分析一个D盘下的excel名字叫test.xlsx 文件内容A列数据比c列数据都多出那些数据,还要考虑重复出现的次数,比如 A列某个值出现5次,而 C列出现3次,那么也算"多出2个"。统计每个值在 A 和 C 中的出现次数,计算差值,输出差值>0的项及其多出的数量。

java 复制代码
package com.datalook.dao.dkyw;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

public class ExcelAnalyzer {


    public static void main(String[] args) {
        String filePath = "D:\\test.xlsx";

        try (FileInputStream fis = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0);
            if (sheet == null) {
                System.out.println("Excel中没有Sheet");
                return;
            }

            DataFormatter dataFormatter = new DataFormatter();

            Map<String, Integer> countA = new HashMap<>();
            Map<String, Integer> countC = new HashMap<>();

            for (Row row : sheet) {
                if (row == null) continue;

                Cell cellA = row.getCell(0);
                Cell cellC = row.getCell(2);

                String valA = cellA != null ? dataFormatter.formatCellValue(cellA).trim() : null;
                String valC = cellC != null ? dataFormatter.formatCellValue(cellC).trim() : null;

                if (valA != null && !valA.isEmpty()) {
                    countA.put(valA, countA.getOrDefault(valA, 0) + 1);
                }
                if (valC != null && !valC.isEmpty()) {
                    countC.put(valC, countC.getOrDefault(valC, 0) + 1);
                }
            }

            List<String> extraEntries = new ArrayList<>();

            for (Map.Entry<String, Integer> entry : countA.entrySet()) {
                String key = entry.getKey();
                int freqA = entry.getValue();
                int freqC = countC.getOrDefault(key, 0);
                int diff = freqA - freqC;
                if (diff > 0) {
                    for (int i = 0; i < diff; i++) {
                        extraEntries.add(key);
                    }
                }
            }

            System.out.println("A列比C列多出的数据(共 " + extraEntries.size() + " 条):");
            for (String val : extraEntries) {
                System.out.println("'" + val + "'");
            }

        } catch (IOException e) {
            System.err.println("读取Excel文件错误:" + e.getMessage());
            e.printStackTrace();
        }
    }


//    public static void main(String[] args) {
//        String filePath = "D:\\test.xlsx";
//
//        try (FileInputStream fis = new FileInputStream(filePath);
//             Workbook workbook = new XSSFWorkbook(fis)) {
//
//            Sheet sheet = workbook.getSheetAt(0); // 读取第一个sheet
//            if (sheet == null) {
//                System.out.println("Excel中没有Sheet");
//                return;
//            }
//
//            DataFormatter dataFormatter = new DataFormatter();
//
//            // 用于存放A列和C列的所有数据(去重)
//            Set<String> setA = new HashSet<>();
//            Set<String> setC = new HashSet<>();
//
//            // 读取所有行,跳过空行
//            for (Row row : sheet) {
//                if (row == null) continue;
//
//                // A列索引是0,C列索引是2
//                Cell cellA = row.getCell(0);
//                Cell cellC = row.getCell(2);
//
//                // 使用DataFormatter获取单元格字符串,避免数据格式问题
//                String valA = cellA != null ? dataFormatter.formatCellValue(cellA).trim() : null;
//                String valC = cellC != null ? dataFormatter.formatCellValue(cellC).trim() : null;
//
//                if (valA != null && !valA.isEmpty()) {
//                    setA.add(valA);
//                }
//                if (valC != null && !valC.isEmpty()) {
//                    setC.add(valC);
//                }
//            }
//
//            // 找出A列中但不在C列中的数据
//            Set<String> difference = new HashSet<>(setA);
//            difference.removeAll(setC);
//
//            System.out.println("A列比C列多出的数据如下:");
//            if (difference.isEmpty()) {
//                System.out.println("无多出数据");
//            } else {
//                difference.forEach(System.out::println);
//            }
//
//        } catch (IOException e) {
//            System.err.println("读取Excel文件时出错:" + e.getMessage());
//            e.printStackTrace();
//        }
//    }
}

控制台输出结果,不管有那些差异当前程序都能循环比对出来

相关推荐
CodeCraft Studio17 小时前
Excel处理控件Aspose.Cells教程:使用 Python 将 Pandas DataFrame 转换为 Excel
python·json·excel·pandas·csv·aspose·dataframe
星空的资源小屋21 小时前
PPTist,一个完全免费的 AI 生成 PPT 在线网站
人工智能·python·电脑·excel
开开心心_Every21 小时前
免费语音合成工具:66种音色随心选
人工智能·面试·java-ee·计算机外设·电脑·maven·excel
偷心伊普西隆2 天前
EXCEL VBA 清空Excel工作表(Sheet)的方法
microsoft·excel
Coding_Doggy2 天前
苍穹外卖Day12 | Apache POI、导出Excel报表、HttpServletResponse、工作台
excel
l1t3 天前
张泽鹏先生手搓的纯ANSI处理UTF-8与美团龙猫调用expat库读取Excel xml对比测试
xml·人工智能·excel·utf8·expat
Source.Liu3 天前
【Python自动化】 21 Pandas Excel 操作完整指南
python·excel·pandas
会飞的小菠菜3 天前
如何根据Excel数据表生成多个合同、工作证、录取通知书等word文件?
word·excel·模板·数据表·生成文件
Access开发易登软件3 天前
Access开发导出PDF的N种姿势,你get了吗?
后端·低代码·pdf·excel·vba·access·access开发
課代表3 天前
VBA 中的 Excel 工作表函数
excel·vba·函数·对象·属性·range·静态变量