Skip to content

Commit 5ad0e77

Browse files
Refactors: segregation into APIs and consumer based sheet processor
1 parent 8d5fe04 commit 5ad0e77

File tree

3 files changed

+100
-80
lines changed

3 files changed

+100
-80
lines changed

apps/excel-diff-checker.jar

-149 Bytes
Binary file not shown.

src/main/java/edu/abhi/poi/excel/ExcelDiffChecker.java

Lines changed: 86 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import java.io.File;
44
import java.io.FileInputStream;
55
import java.io.FileOutputStream;
6+
import java.util.function.Consumer;
67

8+
import org.apache.poi.ss.usermodel.Sheet;
79
import org.apache.poi.xssf.usermodel.XSSFCell;
810
import org.apache.poi.xssf.usermodel.XSSFRow;
911
import org.apache.poi.xssf.usermodel.XSSFSheet;
@@ -16,103 +18,110 @@
1618
*/
1719
public class ExcelDiffChecker {
1820

19-
private static boolean success = true;
2021
private static boolean diffFound = false;
2122
private static boolean commentFlag = true;
23+
private static String FILE_NAME1, FILE_NAME2;
2224

2325
public static void main(String[] args) {
2426

25-
String FILE_NAME1 = args[0];
26-
String FILE_NAME2 = args[1];
27+
FILE_NAME1 = args[0];
28+
FILE_NAME2 = args[1];
2729
commentFlag = args.length == 2;
2830

2931
String RESULT_FILE = FILE_NAME1.substring(0, FILE_NAME1.lastIndexOf(".")) + " vs " + FILE_NAME2;
3032

3133
File resultFile = new File(RESULT_FILE);
32-
if(resultFile.exists())
33-
resultFile.delete();
34+
35+
Utility.deleteIfExists(resultFile);
3436

3537
try(XSSFWorkbook resultWorkbook = new XSSFWorkbook(new FileInputStream(new File(FILE_NAME1)));
3638
XSSFWorkbook workbook2 = new XSSFWorkbook(new FileInputStream(new File(FILE_NAME2)))) {
3739

38-
if(resultWorkbook.getNumberOfSheets() != workbook2.getNumberOfSheets()) {
39-
System.out.println("Unequal number of sheets present!");
40-
success = false;
41-
}
40+
processAllSheets(resultWorkbook, workbook2);
4241

43-
breaker_on_failure:
44-
for(int sheetIndex = 0; (sheetIndex < resultWorkbook.getNumberOfSheets()) && success; sheetIndex++) {
45-
XSSFSheet sheet1 = (XSSFSheet) resultWorkbook.getSheetAt(sheetIndex);
46-
XSSFSheet sheet2 = (XSSFSheet) workbook2.getSheetAt(sheetIndex);
47-
48-
if(!sheet1.getSheetName().equals(sheet2.getSheetName())) {
49-
System.out.println(String.format("Expected sheet name[%s] of workbook[%s] but found [%s]!",
50-
sheet1.getSheetName(), FILE_NAME2, sheet2.getSheetName()));
51-
success = false;
52-
break breaker_on_failure;
53-
} else if(sheet1.getLastRowNum() != sheet2.getLastRowNum()) {
54-
System.out.println(String.format("Expected row count[%s] of sheet[%s] of workbook[%s] but found [%s]!",
55-
sheet1.getLastRowNum(), sheet1.getSheetName(), FILE_NAME2, sheet2.getLastRowNum()));
56-
success = false;
57-
break breaker_on_failure;
42+
if(diffFound) {
43+
if(commentFlag) {
44+
try (FileOutputStream outputStream = new FileOutputStream(RESULT_FILE)) {
45+
resultWorkbook.write(outputStream);
46+
System.out.println("Diff excel has been generated!");
5847
}
48+
}
49+
} else
50+
System.out.println("No diff found!");
51+
} catch (Exception e) {
52+
e.printStackTrace(System.out);
53+
}
54+
}
5955

60-
for(int rowIndex = 0; rowIndex <= sheet1.getLastRowNum(); rowIndex++) {
61-
XSSFRow row1 = (XSSFRow) sheet1.getRow(rowIndex);
62-
XSSFRow row2 = (XSSFRow) sheet2.getRow(rowIndex);
63-
64-
if(row1 == null || row2 == null) {
65-
if(row1 != row2)
66-
System.out.println("Both rows are not null at rowIndex = " + rowIndex);
67-
continue;
68-
} else if(row1.getLastCellNum() != row2.getLastCellNum()) {
69-
System.out.println(String.format("Expected column count[%s] of rowIndex[%s] of sheet[%s] of workbook[%s] but found [%s]!",
70-
row1.getLastCellNum(), rowIndex, sheet1.getSheetName(), FILE_NAME2, row2.getLastCellNum()));
71-
success = false;
72-
break breaker_on_failure;
73-
}
74-
75-
for(int columnIndex = 0; columnIndex <= row1.getLastCellNum(); columnIndex++) {
76-
XSSFCell cell1 = (XSSFCell) row1.getCell(columnIndex);
77-
XSSFCell cell2 = (XSSFCell) row2.getCell(columnIndex);
78-
79-
if(Utility.hasNoContent(cell1)) {
80-
if(Utility.hasContent(cell2)) {
81-
if(cell1 == null)
82-
cell1 = row1.createCell(columnIndex);
83-
84-
diffFound = true;
85-
Utility.processDiff(cell1, cell2, commentFlag);
86-
}
87-
} else if(Utility.hasNoContent(cell2)) {
88-
if(Utility.hasContent(cell1)) {
89-
diffFound = true;
90-
Utility.processDiff(cell1, null, commentFlag);
91-
}
92-
} else if(!cell1.getRawValue().equals(cell2.getRawValue())) {
93-
diffFound = true;
94-
Utility.processDiff(cell1, cell2, commentFlag);
95-
}
96-
}
97-
}
56+
private static void processAllSheets(XSSFWorkbook resultWorkbook, XSSFWorkbook workbook2) throws Exception {
57+
58+
Consumer<Sheet> consumer = sheet1 -> {
59+
XSSFSheet sheet2 = (XSSFSheet) workbook2.getSheet(sheet1.getSheetName());
60+
61+
if(sheet2 == null) {
62+
System.out.println(String.format("Sheet[%s] doesn't exist in workbook[%s]", sheet1.getSheetName(), FILE_NAME2));
63+
} else
64+
try {
65+
processAllRows((XSSFSheet) sheet1, sheet2);
66+
} catch (Exception e) {
67+
e.printStackTrace(System.out);
9868
}
69+
};
9970

100-
if(success) {
101-
if(diffFound) {
102-
if(commentFlag) {
103-
try (FileOutputStream outputStream = new FileOutputStream(RESULT_FILE)) {
104-
resultWorkbook.write(outputStream);
105-
System.out.println("Diff excel has been generated!");
106-
}
107-
}
108-
} else
109-
System.out.println("No diff found!");
71+
resultWorkbook.forEach(consumer);
72+
}
73+
74+
private static void processAllRows(XSSFSheet sheet1, XSSFSheet sheet2) throws Exception {
75+
for(int rowIndex = 0; rowIndex <= sheet1.getLastRowNum(); rowIndex++) {
76+
XSSFRow row1 = (XSSFRow) sheet1.getRow(rowIndex);
77+
XSSFRow row2 = (XSSFRow) sheet2.getRow(rowIndex);
78+
79+
if(row1 == null || row2 == null) {
80+
if(!(row1 == null && row2 ==null)) {
81+
diffFound = true;
82+
processNullRow(sheet1, rowIndex, row2);
83+
}
84+
continue;
11085
}
111-
} catch (Exception e) {
112-
e.printStackTrace(System.out);
113-
114-
if(resultFile.exists())
115-
resultFile.delete();
86+
87+
processAllColumns(row1, row2);
88+
}
89+
}
90+
91+
private static void processAllColumns(XSSFRow row1, XSSFRow row2) throws Exception {
92+
for(int columnIndex = 0; columnIndex <= row1.getLastCellNum(); columnIndex++) {
93+
XSSFCell cell1 = (XSSFCell) row1.getCell(columnIndex);
94+
XSSFCell cell2 = (XSSFCell) row2.getCell(columnIndex);
95+
96+
if(Utility.hasNoContent(cell1)) {
97+
if(Utility.hasContent(cell2)) {
98+
diffFound = true;
99+
Utility.processDiffForColumn(cell1 == null? row1.createCell(columnIndex) : cell1, commentFlag, Utility.getCellValue(cell2));
100+
}
101+
} else if(Utility.hasNoContent(cell2)) {
102+
if(Utility.hasContent(cell1)) {
103+
diffFound = true;
104+
Utility.processDiffForColumn(cell1, commentFlag, Utility.getCellValue(cell2));
105+
}
106+
} else if(!cell1.getRawValue().equals(cell2.getRawValue())) {
107+
diffFound = true;
108+
Utility.processDiffForColumn(cell1, commentFlag, Utility.getCellValue(cell2));
109+
}
110+
}
111+
}
112+
113+
public static void processNullRow(XSSFSheet sheet1, int rowIndex, XSSFRow row2) throws Exception {
114+
XSSFRow row1 = sheet1.getRow(rowIndex);
115+
116+
if(row1 == null) {
117+
row1 = sheet1.createRow(rowIndex);
118+
119+
for(int columnIndex = 0; columnIndex <= row2.getLastCellNum(); columnIndex++) {
120+
Utility.processDiffForColumn(row1.createCell(0), commentFlag, Utility.getCellValue(row2.getCell(columnIndex)));
121+
}
122+
} else {
123+
XSSFCell cell1 = row1.getCell(0);
124+
Utility.processDiffForColumn(cell1 == null? row1.createCell(0) : cell1, commentFlag, "Null row");
116125
}
117126
}
118127

src/main/java/edu/abhi/poi/excel/Utility.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*/
44
package edu.abhi.poi.excel;
55

6+
import java.io.File;
7+
68
import org.apache.poi.ss.usermodel.CellType;
79
import org.apache.poi.ss.usermodel.ClientAnchor;
810
import org.apache.poi.ss.usermodel.Comment;
@@ -11,6 +13,8 @@
1113
import org.apache.poi.ss.usermodel.Drawing;
1214
import org.apache.poi.ss.usermodel.Sheet;
1315
import org.apache.poi.xssf.usermodel.XSSFCell;
16+
import org.apache.poi.xssf.usermodel.XSSFRow;
17+
import org.apache.poi.xssf.usermodel.XSSFSheet;
1418

1519
/**
1620
* @author abhishek sarkar
@@ -27,14 +31,14 @@ public static boolean hasContent(XSSFCell cell) {
2731
}
2832

2933
@SuppressWarnings("rawtypes")
30-
public static void processDiff(XSSFCell cell1, XSSFCell cell2, boolean commentFlag) throws Exception {
34+
public static void processDiffForColumn(XSSFCell cell1, boolean commentFlag, String note) throws Exception {
3135

3236
Sheet sheet = cell1.getSheet();
3337

3438
System.out.println(String.format("Diff at cell[%s] of sheet[%s]", cell1.getReference(), sheet.getSheetName()));
3539

3640
if(!commentFlag) {
37-
System.out.println(String.format("Expected: [%s], Found: [%s]", getCellValue(cell1), getCellValue(cell2)));
41+
System.out.println(String.format("Expected: [%s], Found: [%s]", getCellValue(cell1), note));
3842
return;
3943
}
4044

@@ -52,7 +56,7 @@ public static void processDiff(XSSFCell cell1, XSSFCell cell2, boolean commentFl
5256
Comment comment = drawing.createCellComment(anchor);
5357

5458
//set the comment text and author
55-
comment.setString(factory.createRichTextString("Found " + Utility.getCellValue(cell2)));
59+
comment.setString(factory.createRichTextString("Found " + note));
5660
comment.setAuthor("SYSTEM");
5761

5862
cell1.setCellComment(comment);
@@ -85,5 +89,12 @@ public static String getCellValue(XSSFCell cell) throws Exception {
8589
}
8690
return content;
8791
}
92+
93+
public static boolean deleteIfExists(File file) {
94+
if(file.exists())
95+
return file.delete();
96+
97+
return false;
98+
}
8899

89100
}

0 commit comments

Comments
 (0)