import org.apache.poi.ss.usermodel.*; import java.io.FileInputStream; import java.io.IOException; public class ExcelComparator { public static void main(String[] args) { String file1Path = "path/to/file1.xlsx"; String file2Path = "path/to/file2.xlsx"; try { FileInputStream file1 = new FileInputStream(file1Path); FileInputStream file2 = new FileInputStream(file2Path); Workbook workbook1 = WorkbookFactory.create(file1); Workbook workbook2 = WorkbookFactory.create(file2); boolean areEqual = compareWorkbooks(workbook1, workbook2); if (areEqual) { System.out.println("The Excel files are the same."); } else { System.out.println("The Excel files differ."); // Print the differences compareSheets(workbook1, workbook2); } workbook1.close(); workbook2.close(); } catch (IOException e) { e.printStackTrace(); } } private static boolean compareWorkbooks(Workbook workbook1, Workbook workbook2) { int numSheets1 = workbook1.getNumberOfSheets(); int numSheets2 = workbook2.getNumberOfSheets(); if (numSheets1 != numSheets2) { return false; } for (int i = 0; i < numSheets1; i++) { Sheet sheet1 = workbook1.getSheetAt(i); Sheet sheet2 = workbook2.getSheetAt(i); if (!compareSheets(sheet1, sheet2)) { return false; } } return true; } private static boolean compareSheets(Sheet sheet1, Sheet sheet2) { int numRows1 = sheet1.getLastRowNum() + 1; int numRows2 = sheet2.getLastRowNum() + 1; if (numRows1 != numRows2) { return false; } for (int i = 0; i < numRows1; i++) { Row row1 = sheet1.getRow(i); Row row2 = sheet2.getRow(i); if (!compareRows(row1, row2)) { return false; } } return true; } private static boolean compareRows(Row row1, Row row2) { int numCells1 = row1.getLastCellNum(); int numCells2 = row2.getLastCellNum(); if (numCells1 != numCells2) { return false; } for (int i = 0; i < numCells1; i++) { Cell cell1 = row1.getCell(i); Cell cell2 = row2.getCell(i); if (!compareCells(cell1, cell2)) { return false; } } return true; } private static boolean compareCells(Cell cell1, Cell cell2) { if (cell1 == null && cell2 == null) { return true; } if (cell1 == null || cell2 == null) { return false; } CellType cellType1 = cell1.getCellType(); CellType cellType2 = cell2.getCellType(); if (cellType1 != cellType2) { return false; } switch (cellType1) { case STRING: return cell1.getStringCellValue().equals(cell2.getStringCellValue()); case NUMERIC: return cell1.getNumericCellValue() == cell2.getNumericCellValue(); case BOOLEAN: return cell1.getBooleanCellValue() == cell2.getBooleanCellValue(); // Add more cases for other cell types if needed } return true; } }