Is there any built in function that can be used to easily compare two rows in an Excel file. I am using 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();
}
So I have an if statement
that has the function rowsAreEqual()
needs to compare equality of the two rows. How can I make the rowsAreEqual()
function?
I tried this but hasn't worked:
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;
}