Skip to content

Commit a25fbcd

Browse files
added support for optional diff excel generation
1 parent e900e39 commit a25fbcd

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

apps/excel-diff-checker.jar

47 Bytes
Binary file not shown.

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

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
*
1616
*/
1717
public class ExcelDiffChecker {
18-
18+
1919
static boolean diffFound = false;
20+
static boolean commentFlag = true;
2021

2122
public static void main(String[] args) {
2223

2324
String FILE_NAME1 = args[0];
2425
String FILE_NAME2 = args[1];
26+
commentFlag = args.length == 2;
2527

2628
String RESULT_FILE = FILE_NAME1.substring(0, FILE_NAME1.lastIndexOf(".")) + " vs " +
2729
FILE_NAME2;
@@ -60,7 +62,7 @@ public static void main(String[] args) {
6062
for(int rowIndex = 0; rowIndex <= sheet1.getLastRowNum(); rowIndex++) {
6163
XSSFRow row1 = (XSSFRow) sheet1.getRow(rowIndex);
6264
XSSFRow row2 = (XSSFRow) sheet2.getRow(rowIndex);
63-
65+
6466
if(row1 == null || row2 == null) {
6567
if(row1 != row2)
6668
System.out.println("Both rows are not null at rowIndex = " + rowIndex);
@@ -80,44 +82,37 @@ public static void main(String[] args) {
8082
if(Utility.hasContent(cell2)) {
8183
if(cell1 == null)
8284
cell1 = row1.createCell(columnIndex);
83-
System.out.println(String.format("Diff at cell[%s] of sheet[%s]",
84-
cell1.getReference(), sheet1.getSheetName()));
85-
85+
8686
Utility.addComment(resultWorkbook, sheet1, rowIndex, cell1, "SYSTEM", cell2);
8787
}
8888
} else if(Utility.hasNoContent(cell2)) {
8989
if(Utility.hasContent(cell1)) {
90-
System.out.println(String.format("Diff at cell[%s] of sheet[%s]",
91-
cell1.getReference(), sheet1.getSheetName()));
92-
9390
Utility.addComment(resultWorkbook, sheet1, rowIndex, cell1, "SYSTEM", null);
9491
}
9592
} else if(!cell1.getRawValue().equals(cell2.getRawValue())) {
96-
System.out.println(String.format("Diff at cell[%s] of sheet[%s]",
97-
cell1.getReference(), sheet1.getSheetName()));
98-
9993
Utility.addComment(resultWorkbook, sheet1, rowIndex, cell1, "SYSTEM", cell2);
10094
}
10195
}
10296
}
10397
}
104-
105-
try (FileOutputStream outputStream = new FileOutputStream(RESULT_FILE)) {
106-
resultWorkbook.write(outputStream);
107-
}
98+
99+
if(success) {
100+
if(diffFound) {
101+
if(commentFlag) {
102+
try (FileOutputStream outputStream = new FileOutputStream(RESULT_FILE)) {
103+
resultWorkbook.write(outputStream);
104+
System.out.println("Diff excel has been generated!");
105+
}
106+
}
107+
} else
108+
System.out.println("No diff found!");
109+
}
108110
} catch (Exception e) {
109111
e.printStackTrace(System.out);
110-
success = false;
111-
} finally {
112-
if(success && diffFound)
113-
System.out.println("Diff excel has been generated!");
114-
else {
115-
if(success)
116-
System.out.println("No diff found!");
112+
113+
if(resultFile.exists())
117114
resultFile.delete();
118-
}
119115
}
120-
121116
}
122-
117+
123118
}

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

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

6-
import org.apache.poi.ss.usermodel.Cell;
76
import org.apache.poi.ss.usermodel.CellType;
87
import org.apache.poi.ss.usermodel.ClientAnchor;
98
import org.apache.poi.ss.usermodel.Comment;
@@ -29,7 +28,17 @@ public static boolean hasContent(XSSFCell cell) {
2928
}
3029

3130
@SuppressWarnings("rawtypes")
32-
public static void addComment(Workbook workbook, Sheet sheet, int rowIndex, Cell cell1, String author, XSSFCell cell2) throws Exception {
31+
public static void addComment(Workbook workbook, Sheet sheet, int rowIndex, XSSFCell cell1, String author, XSSFCell cell2) throws Exception {
32+
33+
System.out.println(String.format("Diff at cell[%s] of sheet[%s]", cell1.getReference(), sheet.getSheetName()));
34+
35+
ExcelDiffChecker.diffFound = true;
36+
37+
if(!ExcelDiffChecker.commentFlag) {
38+
System.out.println(String.format("Expected: [%s], Found: [%s]", getCellValue(cell1), getCellValue(cell2)));
39+
return;
40+
}
41+
3342
CreationHelper factory = workbook.getCreationHelper();
3443
//get an existing cell or create it otherwise:
3544

@@ -48,8 +57,6 @@ public static void addComment(Workbook workbook, Sheet sheet, int rowIndex, Cell
4857
comment.setAuthor(author);
4958

5059
cell1.setCellComment(comment);
51-
52-
ExcelDiffChecker.diffFound = true;
5360
}
5461

5562
public static String getCellValue(XSSFCell cell) throws Exception {

0 commit comments

Comments
 (0)