java - 如何用java比较2个excel中的数据

标签 java excel compare

我有 2 个 Excel,具有相同的列数和相同的行数。但行位置不一样。 我只想检查 excel1 中的 row1 是否与 excel2 的 row1 不匹配,然后转到 excel2 的 row2 并比较数据。如果 excel2 中的 row2 再次不匹配,则转到 excel2 的 row3 。如果找到匹配则退出

public static void CompareTwoExcelFiles(String nasPath, String dbVal) {
    try {

        FileInputStream excellFile1 = new FileInputStream(nasPath);
        FileInputStream excellFile2 = new FileInputStream(dbVal);

        XSSFWorkbook workbook1 = new XSSFWorkbook(excellFile1);
        XSSFWorkbook workbook2 = new XSSFWorkbook(excellFile2);

        XSSFSheet sheet1 = workbook1.getSheetAt(0);
        XSSFSheet sheet2 = workbook2.getSheetAt(0);

        if (compareTwoSheets(sheet1, sheet2)) {
            System.out.println("\n\nThe two excel sheets are Equal");
        } else {
            System.err.println("\n\nThe two excel sheets are Not Equal");
        }

        excellFile1.close();
        excellFile2.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

private static boolean compareTwoSheets(XSSFSheet sheet1, XSSFSheet sheet2) {
    // TODO Auto-generated method stub
    int firstRow1 = sheet1.getFirstRowNum();
    int lastRow1 = sheet1.getLastRowNum();
    boolean equalSheets = true;
    for (int i = firstRow1; i <= lastRow1; i++) {

        System.out.println("\n\nComparing Row " + i);

        XSSFRow row1 = sheet1.getRow(i);
        XSSFRow row2 = sheet2.getRow(i);
        if (!compareTwoRows(row1, row2)) {
            equalSheets = false;
            System.err.println("Row " + i + " - Not Equal");
            break;
        } else {
            System.out.println("Row " + i + " - Equal");
        }
    }
    return equalSheets;
}

private static boolean compareTwoRows(XSSFRow row1, XSSFRow row2) {
    // TODO Auto-generated method stub
    if ((row1 == null) && (row2 == null)) {
        return true;
    } else if ((row1 == null) || (row2 == null)) {
        return false;
    }

    int firstCell1 = row1.getFirstCellNum();
    int lastCell1 = row1.getLastCellNum();
    boolean equalRows = true;

    // Compare all cells in a row
    for (int i = firstCell1; i <= lastCell1; i++) {
        XSSFCell cell1 = row1.getCell(i);
        XSSFCell cell2 = row2.getCell(i);
        System.out.println(cell1);
        System.out.println(cell2);
        if (!compareTwoCells(cell1, cell2)) {
            equalRows = false;
            System.err.println("       Cell " + i + " - NOt Equal");
            break;
        } else {
            System.out.println("       Cell " + i + " - Equal");
        }
    }
    return equalRows;
}

private static boolean compareTwoCells(XSSFCell cell1, XSSFCell cell2) {
    if ((cell1 == null) && (cell2 == null)) {
        return true;
    } else if ((cell1 == null) || (cell2 == null)) {
        return false;
    }

    boolean equalCells = false;
    int type1 = cell1.getCellType();
    int type2 = cell2.getCellType();
    if (type1 == type2) {
        if (cell1.getCellStyle().equals(cell2.getCellStyle())) {
            // Compare cells based on its type
            switch (cell1.getCellType()) {
            case HSSFCell.CELL_TYPE_FORMULA:
                if (cell1.getCellFormula().equals(cell2.getCellFormula())) {
                    equalCells = true;
                }
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                if (cell1.getNumericCellValue() == cell2.getNumericCellValue()) {
                    equalCells = true;
                }
                break;
            case HSSFCell.CELL_TYPE_STRING:
                if (cell1.getStringCellValue().equals(cell2.getStringCellValue())) {
                    equalCells = true;
                }
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                if (cell2.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                    equalCells = true;
                }
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN:
                if (cell1.getBooleanCellValue() == cell2.getBooleanCellValue()) {
                    equalCells = true;
                }

上面的代码仅检查 row1(excel1)=row1(excel2) 组合,如果 excel2 的 row1 处没有数据,则即使匹配的数据位于 excel2 的 row2 处,测试也会失败。

请帮忙..

最佳答案

在compareTwoSheets方法中,当您比较这2个工作表的2行时,这2行的索引应该是隔离的。否则,代码将只比较这2个工作表的相同索引,即row1(excel1)<->row1(excel2),row2(excel1)<->row12(excel2)...

private static boolean compareTwoSheets(XSSFSheet sheet1, XSSFSheet sheet2) {
    int firstRow1 = sheet1.getFirstRowNum();
    int lastRow1 = sheet1.getLastRowNum();
    int firstRow2 = sheet2.getFirstRowNum(), lastRow2 = sheet2.getLastRowNum();
    if (lastRow1 - firstRow1 != lastRow2 - firstRow2) {
        return false;
    }
    for (int i = firstRow1; i <= lastRow1; i++) {
        for (int j = secondRow1; j <= lastRow1; j++) {
            System.out.println("Comparing Row1 " + i + " and Row2 " + j);
            XSSFRow row1 = sheet1.getRow(i);
            XSSFRow row2 = sheet2.getRow(j);
            if (!compareTwoRows(row1, row2)) {
                return false;
            } else {
                System.out.println("Row1 " + i + " and Row2 " + j + " - Equal");
            }
        }
    }
    return true;
}

顺便说一句,在这个方法compareTwoSheets中,您应该检查这两个工作表的lastRowNum或总行数是否相等。如果不相等,则这两个工作表完全不同,因为数据大小不一样。

关于java - 如何用java比较2个excel中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59189010/

相关文章:

java - 如何使用@SpringBootApplication和@Configuration、数据源

java - 使用 libgdx (java) 的碰撞检测 Tmx map

java - 如果从 ArrayList 传递到 excel,如何从 XML 中检测换行符?

excel - excel(vba)中的日志功能 - 没有给我正确的答案

java - 启动 Activity ( Intent );应用程序似乎总是崩溃

java - 如何按最低数字/字符串Android Studio对recyclerview进行排序

vba - 可以根据另一个单元格的值锁定单元格范围吗?

php编译器比较代码?

list - 如何比较排名列表

arrays - 整数数组的校验和?