判断2个excel文件差异的条数

判断2个excel文件差异的条数

java 复制代码
package com.lucky.luckydoc;

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

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExcelComparator {

    public static void main(String[] args) throws IOException {
        String file1Path = "C:\\Users\\luckincoffee\\Downloads\\DBSearch_2024-09-14_10_59_36.xlsx";
        String file2Path = "C:\\Users\\luckincoffee\\Downloads\\DBSearch_2024-09-14_11_00_03.xlsx";

        // 读取两个 Excel 文件
        List<List<String>> file1Data = readExcel(file1Path);
        List<List<String>> file2Data = readExcel(file2Path);

        // 比较两个文件的差异
        compareExcelRows(file1Data, file2Data);
    }

    // 读取 Excel 文件中的数据
    public static List<List<String>> readExcel(String filePath) throws IOException {
        List<List<String>> data = new ArrayList<>();
        FileInputStream fis = new FileInputStream(filePath);
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表

        for (Row row : sheet) {
            List<String> rowData = new ArrayList<>();
            for (Cell cell : row) {
                rowData.add(getCellValueAsString(cell));
            }
            data.add(rowData);
        }
        workbook.close();
        fis.close();
        return data;
    }

    // 根据单元格的类型获取字符串形式的值
    public static String getCellValueAsString(Cell cell) {
        if (cell == null) {
            return "";
        }
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue(); // 字符串类型
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    // 如果是日期类型,返回日期格式
                    return cell.getDateCellValue().toString();
                } else {
                    // 否则返回数值类型
                    return String.valueOf(cell.getNumericCellValue());
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue()); // 布尔类型
            case FORMULA:
                return cell.getCellFormula(); // 公式类型
            case BLANK:
                return ""; // 空白单元格
            default:
                return "未知类型";
        }
    }

    // 比较两个 Excel 文件的行差异
    public static void compareExcelRows(List<List<String>> file1Data, List<List<String>> file2Data) {
        int maxRows = Math.max(file1Data.size(), file2Data.size());
        List<List<String>> result = new ArrayList<>();
        if(file1Data.size()>file2Data.size()){
            file1Data.removeAll(file2Data);
            result=file1Data;
        }else if(file2Data.size()>file1Data.size()){
            file2Data.removeAll(file1Data);
            result=file2Data;
        }
       for (List<String> row : result){
           System.out.println(row);
       }
//        for (int i = 0; i < maxRows; i++) {
//            List<String> row1 = i < file1Data.size() ? file1Data.get(i) : new ArrayList<>();
//            List<String> row2 = i < file2Data.size() ? file2Data.get(i) : new ArrayList<>();
//
//            if (!row1.equals(row2)) {
//                System.out.println("第 " + (i + 1) + " 行不同:");
//                System.out.println("文件1: " + row1);
//                System.out.println("文件2: " + row2);
//            }
//        }
    }
}
相关推荐
懵逼的小黑子3 小时前
excel里面店铺这一列的数据结构是2C【uniteasone17】这种,我想只保留前面的2C部分,后面的【uniteasone17】不要
excel
偷心伊普西隆7 小时前
Python EXCEL 理论探究:格式转换时处理缺失值方法
python·excel
CodeCraft Studio1 天前
Excel处理控件Aspose.Cells教程:使用 Python 将 Pandas DataFrame 转换为 Excel
python·json·excel·pandas·csv·aspose·dataframe
星空的资源小屋1 天前
PPTist,一个完全免费的 AI 生成 PPT 在线网站
人工智能·python·电脑·excel
开开心心_Every1 天前
免费语音合成工具: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
会飞的小菠菜4 天前
如何根据Excel数据表生成多个合同、工作证、录取通知书等word文件?
word·excel·模板·数据表·生成文件