如何比较 Java 中 Excel 文件中两行的相等性?

How to compare equality of two rows in Excel file in Java?

是否有任何内置函数可用于轻松比较 Excel 文件中的两行。我正在使用 Apache POI。

        try {

            Workbook wb1 = WorkbookFactory.create(new File(file1Path));
            Sheet sheet1 = wb1.getSheetAt(0);

            Workbook wb2 = WorkbookFactory.create(new File(file2Path));
            Sheet sheet2 = wb2.getSheetAt(0);

            for (Row myrow : sheet1) {
                if (myrow.getRowNum() == 0) {
                    // add entire row to a sheet for 'found in file 1 but not file 2' -> sheet 0
                    write(myrow, output_filename_path, dstSheetNumberInOutputFile);
                    continue;
                }
                // look for this key in the other sheet
                for (Row otherRow : sheet2) {
                    if (rowsAreEqual(myrow, otherRow)) {
                        write(myrow, output_filename_path, dstSheetNumberInOutputFile);
                        break;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

所以我有一个 if statement 函数 rowsAreEqual() 需要比较两行的相等性。如何实现 rowsAreEqual() 功能?

我试过了,但没成功:

    private static boolean rowsAreEqual(Row myrow, Row otherRow) {
        int equalCount = 0;
        System.out.println("last cell num: " + myrow.getLastCellNum());
        for(int i=0; i < myrow.getLastCellNum(); i++){
            if(myrow.getCell(i) == otherRow.getCell(i)){
                equalCount++;
                System.out.println("Cells are the same: " + myrow.getCell(i) + " && " + otherRow.getCell(i));
            }
        }

        if(equalCount == myrow.getLastCellNum()){
            return true;
        }
        return false;
    }

我是这么想的。我必须创建一个名为 getCellContentsAsString() 的额外函数,以便我可以正确比较单元格:

    private static boolean rowsAreEqual(Row myrow, Row otherRow) {
        int equalCount = 0;
        System.out.println("last cell num: " + myrow.getLastCellNum());
        for(int i=0; i < myrow.getLastCellNum(); i++){
            System.out.println(myrow.getCell(i) + " is the cell content in file1");
            System.out.println(otherRow.getCell(i) + " is the cell content in file2");
            Cell c1 = myrow.getCell(i);
            Cell c2 = otherRow.getCell(i);
            
            String s1 = getCellContentAsString(c1);
            String s2 = getCellContentAsString(c2);
            
            if(s1.equals(s2)){
                equalCount++;
                System.out.println("Cells are the same: " + myrow.getCell(i) + " && " + otherRow.getCell(i));
            }
        }

        if(equalCount == myrow.getLastCellNum()){
            return true;
        }
        return false;
    }

    private static String getCellContentAsString(Cell cell) {
        String data = null;
        if(cell.getCellType()==CellType.STRING) {
            data = cell.getStringCellValue(); 
        }
        else if(cell.getCellType()==CellType.NUMERIC) {
            data = String.valueOf(cell.getNumericCellValue());
        }
        return data;
    }

您可以抽象为比较一系列单元格。 这将允许您比较行或列,或单个区域(例如 A1:Z256)——所谓的 Range.

所以要比较的对象 - 在 contents 中相等 - 是 CellRange.

在Java中boolean equals() method of any object relies on their int hashCode()函数。在名为 RangeContentsUtils 的实用程序 class 中,您可以同时实现:

public class RangeContentsUtils {

  public static boolean contentsEquals(CellRange rangeA, CellRange rangeB) {
    if (rangeA == null || rangeB == null) {
        return false;
    }
    if (rangeA.size() != rangeB.size()) {
        return false;
    }
    // alternative would be:
    // return stringContentsOf(rangeA).hashCode() == stringContentsOf(rangeB).hashCode();

    return contentsToString(rangeA).equals(contentsToString(rangeB));
  }

  public static String contentsToString(CellRange range) {
    StringBuilder sb = new StringBuilder();
    
    Iterator<Cell> cellIterator = range.iterator();
    while(cellIterator.hasNext()) {
      Cell cell = cellIterator.next();
      switch(cell.getCellType()) {
        case Cell.CELL_TYPE_BOOLEAN:
          sb.append(cell.getBooleanCellValue());
          break;
        case Cell.CELL_TYPE_NUMERIC:
          sb.append(cell.getNumericCellValue());
          break;
        case Cell.CELL_TYPE_STRING:
          sb.append(cell.getStringCellValue());
          break;
      }
      sb.append("[=10=]"); // use NUL char as cell-separator 
    }

    return sb.toString();
  }

}

现在您的行比较可以使用此实用程序 class。

另请参阅: Compare specific row or column of xls sheet using java and Apache POI