Compare Two Excel Files in Java10 Sept 2024 | 5 min read Comparing two or multiple excel workbooks is very common requirement specially for automated test scenarios. In this section, we will learn how to compare two excel workbooks or to verify if two workbooks have same data set. Comparing Excel FilesWe 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 follows:
When above conditions are 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 SheetsTo 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 NameHere 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. Check If Both Workbooks Have Same Number of Rows In All SheetsWe 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 Column for RowA 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 that 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. We can check using DateUtil.isCellDateFormatted(Cell cell). CompareExcelFiles.java Output: The cell values at (0, 0) are the same The cell values at (0, 1) are the same The cell values at (0, 2) are the same The cell values at (0, 3) are the same The cell values at (1, 0) are the same The cell values at (1, 1) are the same The cell values at (1, 2) are the same The cell values at (1, 3) are the same The cell values at (2, 0) are the same The cell values at (2, 1) are the same The cell values at (2, 2) are the same The cell values at (2, 3) are the same The cell values at (3, 0) are the same The cell values at (3, 1) are the same The cell values at (3, 2) are the same The cell values at (3, 3) are the same Note: In the above program, assumed that file1.xls and file2.xls contain two sheets with the same structure and values.Next TopicConsecutive Prime Sum Program in Java |
? In Java, we can compare two arrays by comparing each element of the array. Java Arrays class provides two predefined methods that is used to compare two arrays in Java. In this section, we will learn how to compare two Arrays using Arrays.equals() method and Arrays.deepEquals() method....
4 min read
In the world of programming, conditional statements play a crucial role in controlling the flow of execution based on specific conditions. Java, being one of the most popular programming languages, provides several conditional operators that allow developers to create dynamic and flexible code. In this...
4 min read
Java, being an object-oriented programming language, offers robust support for handling objects and their interactions. One important aspect of working with objects is the ability to convert or cast them to different types or classes. Java provides mechanisms for casting objects, allowing developers to change the...
6 min read
In this section, we will learn what is middle digit number and also create Java programs to find the middle digit number. It is frequently asked in Java coding tests and academics. Middle Digit Number The middle digit of a number is that which is exactly in-mid of...
2 min read
Arrays and vectors are two commonly used programming structures when working with data sets. Although they are both used to store several pieces of the same sort, their features, performance, flexibility, and memory management are very different. What is an Array? A collection of elements can be...
6 min read
Object cloning indicates the production of a precise duplicate of an article. It makes another occurrence of the class of the ongoing article and introduces every one of its fields with the very items in the comparing fields of this item. Utilizing Assignment Operator to make a...
12 min read
Java is a flexible programming language that offers a number of data structures for organising data sets. Maps, such as HashMap and TreeMap, are essential in situations where mapping keys to values is required. There are circumstances, nevertheless, in which you must link a key to more...
4 min read
In Java, the Stream API is responsible for storing the mapToInt() method which was introduced in the Java 8 version. The main purpose of mapToInt() method is used for the transformation of the elements from a stream into an IntStream. Let's understand about mapToInt() method in detail...
9 min read
Java 15, released in September 2020, brought a wave of exciting features that enhanced developer experience, performance, and security. Java 15 reached general availability in September 2020 and is the short-term release for the JDK platform. It builds on several features from earlier releases and...
5 min read
In Java, Banker's algorithm is a deadlock avoidance and resource allocation algorithm. This algorithm tests for security by simulating allocation for a predetermined maximum possible amount of all resources. After that, before deciding whether the allocation should be allowed to continue or not, it creates an...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India