Make Selenium Easy

Compare Two Excel Workbooks Using Apache POI

Comparing two or multiple excel workbooks is very common requirement specially for automated test scenarios. In this post, we will learn How to compare two excel workbooks or to verify if two excel workbooks have same data set.

I hope you are aware of a normal excel workbook. An Excel Workbook is also an Excel File. An Excel Workbook consists of one or multiple sheets. Each sheet consists of rows and columns. Each intersection point of Row and Column is called a Cell. Here is the link o know basics about Excel.

Steps to compare excel workbooks

We must not start comparing excel sheets at cell level or cell data directly. We should first check for basic conditions to be true then proceed for cell comparison.

Basic ordered conditions to be checked first are as below:-

  1. Do workbooks have same number of sheets?
  2. Do workbooks have same name of sheets? You can relax order of sheets depending on requirements. In this post I am relaxing order of sheets but keeping same order to assert cell data.
  3. Do workbooks have same numbers of rows?
  4. Do workbooks have same numbers of columns/cells of corresponding rows? Corresponding rows mean each row may have different number of columns.

When above checked are passed then we should go for cell data comparison. If either check fails then we should not proceed further.

Check if both excel workbooks have same number of sheets

To get number of sheets in a workbook, we need to use getNumberOfSheets() of Workbook interface provided by Apache POI. Workbook interface is implemented by major classes like XSSFWorkbook , HSSFWorkbook.

Check if both excel workbooks have sheets of same name

Here we have two scenarios.

First – Just verify if sheet names are same irrespective of sheet orders.

Second – Verify sheet names are same and appears in same order.

We can get sheet name using getSheetName(int sheetIndex) method.

If you want to check if sheets are in order in both workbook, remove sort() lines of code from above. If you are relaxing sheet order, remember for cell comparison, get sheet by name not by index.

Check if both workbooks have same number of rows in all sheets

We need to check if number of rows in each sheet of workbooks are same. If sheet1 of workbook 1 has 10 rows and sheet2 has 15 rows then workbook 2 should also have 10 rows in sheet1 and 15 rows in sheet2.

To get number of rows in a sheet we need to use getPhysicalNumberOfRows() method of Sheet interface. This method returns the number of physically defined rows (NOT the number of rows in the sheet). If we have 10 rows in a sheet, each row will be indexed or numbered staring from zero. Same we need to do for all sheets. Below is an example code for one sheet.

Check if both workbooks have same columns for row

A row can have different number of columns. So we need to check for each row in all sheets. To iterate through rows, we can use rowIterator() method of Sheet interface which returns an iterator of the physical rows. During iteration of rows, we need to get physical number of cells. Same logic need to do for all sheets.

Now we need to check for cell data and assert.

Each cell has a type like String, Numeric, Boolean etc. To get value as per type, different method needs to be used. For example :- To get data from a Numeric cell type, we need to use getNumericCellValue() and getStringCellValue() for String cell type.

If a cell consists Date, we do not have any such type for Cell. A Cell with date falls under Numeric type. We need to check if cell is date formatted. This we can check using DateUtil.isCellDateFormatted(Cell cell).

Complete Code

Sample Excel Files:-

Unit Tests


Note :-

  1. I have not covered for all Cell type. You can add as per your requirements.
  2. Formatting of cells like fonts, styles etc is not covered.

You can download/clone above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Find all Selenium related post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

4 thoughts on “Compare Two Excel Workbooks Using Apache POI

  1. package TestCases;

    import java.util.Iterator;

    import org.apache.poi.util.SystemOutLogger;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.testng.Assert;
    import org.testng.annotations.Test;

    public class CompareExcel {
    public void CompareRowAndCells() throws IOException
    //int i=0,j=0;
    FileInputStream inputStream=new FileInputStream(“/Users/xxx/Desktop/1ABC/TestDataTest.xlsx”);
    XSSFWorkbook wb1=new XSSFWorkbook(inputStream);
    XSSFSheet sheet1=wb1.getSheet(“Sheet1”);
    int RowCount1= sheet1.getLastRowNum()-sheet1.getFirstRowNum();

    FileInputStream inputStream2=new FileInputStream(“/Users/xxx/Desktop/1ABC/TestDataTest2.xlsx”);
    XSSFWorkbook wb2=new XSSFWorkbook(inputStream2);
    XSSFSheet sheet2=wb2.getSheet(“Sheet1″);

    int RowCount2= sheet2.getLastRowNum()-sheet2.getFirstRowNum();
    //Compares if both excel have equal now of rows
    Assert.assertEquals(RowCount1, RowCount2,”Not sames”);

    //compares if both excel file has same Row Number
    Iterator rowInSheet1 =sheet1.rowIterator();
    Iterator rowInSheet2 =sheet2.rowIterator();
    while(rowInSheet1.hasNext()) {
    int cellcounts1;
    int cellcounts2;
    Assert.assertEquals(cellcounts1, cellcounts2,”Not same”);

    for(int i=0;i<RowCount1;i++) {
    for(int j=0;j Numeric 1->String 4->Boolean
    int type2= c2.getCellType();

    if(c1.getStringCellValue().equals(c2.getStringCellValue())) {
    System.out.println(c1.getStringCellValue()+”, “+c2.getStringCellValue()+”–>”+” Match”);
    System.out.println(c1.getNumericCellValue() +”, “+c2.getNumericCellValue()+” –>”+”Match”);
    else {System.out.println(“NOT MATCHED”);}
    System.out.println(c1.getBooleanCellValue()+”, “+c2.getBooleanCellValue()+”–> “+”Match”);
    System.out.println(c1.getBooleanCellValue()+”, “+ c2.getBooleanCellValue()+”–> “+”Not Matched”);


Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Compare Two Excel Workbooks Using Apache POI

by Amod Mahajan time to read: 15 min
%d bloggers like this: