-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompareExcel.java
159 lines (141 loc) · 5.62 KB
/
CompareExcel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.sl;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CompareExcel {
public static void main(String[] args) {
try {
// get input excel files
FileInputStream excellFile1 = new FileInputStream(new File(
"C:\\sheet1.xlsx"));
FileInputStream excellFile2 = new FileInputStream(new File(
"C:\\sheet2.xlsx"));
// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook1 = new XSSFWorkbook(excellFile1);
XSSFWorkbook workbook2 = new XSSFWorkbook(excellFile2);
// Get first/desired sheet from the workbook
XSSFSheet sheet1 = workbook1.getSheetAt(0);
XSSFSheet sheet2 = workbook2.getSheetAt(0);
// Compare sheets
if(compareTwoSheets(sheet1, sheet2)) {
System.out.println("\n\nThe two excel sheets are Equal");
} else {
System.out.println("\n\nThe two excel sheets are Not Equal");
}
//close files
excellFile1.close();
excellFile2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Compare Two Sheets
public static boolean compareTwoSheets(XSSFSheet sheet1, XSSFSheet sheet2) {
int firstRow1 = sheet1.getFirstRowNum();
int lastRow1 = sheet1.getLastRowNum();
boolean equalSheets = true;
for(int i=firstRow1; i <= lastRow1; i++) {
System.out.println("\n\nComparing Row "+i);
XSSFRow row1 = sheet1.getRow(i);
XSSFRow row2 = sheet2.getRow(i);
if(!compareTwoRows(row1, row2)) {
equalSheets = false;
System.out.println("Row "+i+" - Not Equal");
break;
} else {
System.out.println("Row "+i+" - Equal");
}
}
return equalSheets;
}
// Compare Two Rows
public static boolean compareTwoRows(XSSFRow row1, XSSFRow row2) {
if((row1 == null) && (row2 == null)) {
return true;
} else if((row1 == null) || (row2 == null)) {
return false;
}
int firstCell1 = row1.getFirstCellNum();
int lastCell1 = row1.getLastCellNum();
boolean equalRows = true;
// Compare all cells in a row
for(int i=firstCell1; i <= lastCell1; i++) {
XSSFCell cell1 = row1.getCell(i);
XSSFCell cell2 = row2.getCell(i);
if(!compareTwoCells(cell1, cell2)) {
equalRows = false;
System.err.println(" Cell "+i+" - NOt Equal");
break;
} else {
System.out.println(" Cell "+i+" - Equal");
}
}
return equalRows;
}
// Compare Two Cells
public static boolean compareTwoCells(XSSFCell cell1, XSSFCell cell2) {
if((cell1 == null) && (cell2 == null)) {
return true;
} else if((cell1 == null) || (cell2 == null)) {
return false;
}
boolean equalCells = false;
int type1 = cell1.getCellType();
int type2 = cell2.getCellType();
if (type1 == type2) {
if (cell1.getCellStyle().equals(cell2.getCellStyle())) {
// Compare cells based on its type
switch (cell1.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
if (cell1.getCellFormula().equals(cell2.getCellFormula())) {
equalCells = true;
}
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (cell1.getNumericCellValue() == cell2
.getNumericCellValue()) {
equalCells = true;
}
break;
case HSSFCell.CELL_TYPE_STRING:
if (cell1.getStringCellValue().equals(cell2
.getStringCellValue())) {
equalCells = true;
}
break;
case HSSFCell.CELL_TYPE_BLANK:
if (cell2.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
equalCells = true;
}
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
if (cell1.getBooleanCellValue() == cell2
.getBooleanCellValue()) {
equalCells = true;
}
break;
case HSSFCell.CELL_TYPE_ERROR:
if (cell1.getErrorCellValue() == cell2.getErrorCellValue()) {
equalCells = true;
}
break;
default:
if (cell1.getStringCellValue().equals(
cell2.getStringCellValue())) {
equalCells = true;
}
break;
}
} else {
return false;
}
} else {
return false;
}
return equalCells;
}
}