Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将excel中的数据与java进行比较_Java_Excel_Compare - Fatal编程技术网

如何将excel中的数据与java进行比较

如何将excel中的数据与java进行比较,java,excel,compare,Java,Excel,Compare,我有两个excel,列数和行数相同。但行的位置不一样。 我只想检查excel1的第1行是否与excel2的第1行不匹配,然后转到excel2的第2行并比较数据。如果excel2的第2行再次不匹配,则转到excel2的第3行。如果找到匹配项,则退出 public static void CompareTwoExcelFiles(String nasPath, String dbVal) { try { FileInputStream excellFile1 = new F

我有两个excel,列数和行数相同。但行的位置不一样。 我只想检查excel1的第1行是否与excel2的第1行不匹配,然后转到excel2的第2行并比较数据。如果excel2的第2行再次不匹配,则转到excel2的第3行。如果找到匹配项,则退出

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;
                }
publicstaticvoidcomparetwoexcelfiles(stringnaspath,stringdbval){
试一试{
FileInputStream ExcelFile1=新的FileInputStream(nasPath);
FileInputStream ExcelFile2=新的FileInputStream(dbVal);
XSSF工作簿工作簿1=新XSSF工作簿(ExcelFile1);
XSSF工作簿工作簿2=新XSSF工作簿(ExcelFile2);
XSSFSheet sheet1=工作簿1.getSheetAt(0);
XSSFSheet sheet2=工作簿2.getSheetAt(0);
如果(比较表(表1、表2)){
System.out.println(“\n\n两张excel表格相等”);
}否则{
System.err.println(“\n\n两张excel表格不相等”);
}
ExcelFile1.close();
ExcelFile2.close();
}捕获(例外e){
e、 printStackTrace();
}
}
私有静态布尔比较表(XSSFSheet sheet1、XSSFSheet sheet2){
//TODO自动生成的方法存根
int firstRow1=sheet1.getFirstRowNum();
int lastRow1=sheet1.getLastRowNum();
布尔等式表=真;

对于方法compareTwoSheets中的(int i=firstRow1;i,当您比较这两张工作表中的两行时,这两行的索引应该被隔离。否则,代码将只比较这两张工作表中的相同索引,即第1行(excel1)第1行(excel2)、第2行(excel1)第12行(excel2)

私有静态布尔比较表(XSSFSheet sheet1、XSSFSheet sheet2){
int firstRow1=sheet1.getFirstRowNum();
int lastRow1=sheet1.getLastRowNum();
int firstRow2=sheet2.getFirstRowNum(),lastRow2=sheet2.getLastRowNum();
if(lastRow1-firstRow1!=lastRow2-firstRow2){
返回false;
}

对于方法compareTwoSheets中的(int i=firstRow1;i,当您比较这两张工作表中的两行时,这两行的索引应该被隔离。否则,代码将只比较这两张工作表中的相同索引,即第1行(excel1)第1行(excel2)、第2行(excel1)第12行(excel2)

私有静态布尔比较表(XSSFSheet sheet1、XSSFSheet sheet2){
int firstRow1=sheet1.getFirstRowNum();
int lastRow1=sheet1.getLastRowNum();
int firstRow2=sheet2.getFirstRowNum(),lastRow2=sheet2.getLastRowNum();
if(lastRow1-firstRow1!=lastRow2-firstRow2){
返回false;
}

对于(int i=firstRow1;i No),这不是预期的效果。它将所有记录进行37*37次比较@yuexing@Dolly我更新了代码,这应该是正确的。不,这不是预期的工作。它是比较所有记录的37*37倍@yuexing@Dolly我更新了密码,应该没问题。
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;
}