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 |
Difference Between Constructors and Methods in Java Constructor The constructor and method differ from each other. However, the constructor is used to initialize the object's state. Constructors can also include data members and member functions, just like methods. The data members and member functions of the constructor...
5 min read
In Java, working with dates is a common task, especially when dealing with applications that require tracking dates and times. One of the most common operations involving dates is getting the year from a given date. Fortunately, Java provides several ways to do this, and one...
4 min read
A right-angled triangle containing successive natural integers is called the Floyd Triangle, after the computer scientist Robert Floyd. It is created by sequentially placing the numbers, beginning with 1 at the top, with each row carrying one more number than the one before it. The row and...
4 min read
| Convert BigDecimal to String in Java In Java, BigDecimal is a class that belongs to java.math package and the package belong to java.base module. It extends the Number class and implements the Comparable<BigDecimal> interface. The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison,...
2 min read
In Java, ServerSocket can be defined as a type of class which is majorly utilized for providing implementation of the server-side socket connection of client or server. Also, the socket connection of the client or client is fully independent of system. Let us understand about ServerSocket class...
20 min read
Contextual keywords formerly known as restricted identifiers and restricted keywords. contextual keywords are identified based on where they will appear in the syntactic grammar. These are the keywords that provides the specific meaning in the code. These are not reserved keywords like abstract, new, final, try,...
3 min read
1. Which of the following is a marker interface? Serializable Cloneable Remote All of the above Show AnswerWorkspace Answer: d) Explanation: Marker interfaces in Java are empty interfaces used to signal to the JVM or other code that objects of the implementing class should be treated differently. Examples include Serializable, Cloneable, and...
3 min read
LinkedLists are fundamental data structures in computer science that offer dynamic storage allocation and flexibility. They consist of nodes connected via pointers, and each node holds data and a reference to the node. In this article, we'll explore various methods to compare two linked lists...
11 min read
Just like the Red-Black Tree, the AVL tree is another self-balancing BST(Binary Search Tree) in Java. In the AVL tree, the difference between heights of the right and left subtree doesn't exceed one for all nodes. It takes O(h) time to perform the search, max, min,...
6 min read
? The English meaning of truncate is to trim or prune, or cut something and the process of trimming is called truncation. In the computer science field, the term is often used in reference to data-types or variables (like String, floating-point numbers, etc.). It is a way...
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