Skip to content

Commit f0b580c

Browse files
Null cell equivalent to blank cell consideration improvised diff
1 parent aea4beb commit f0b580c

File tree

5 files changed

+44
-48
lines changed

5 files changed

+44
-48
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,6 @@ local.properties
7878

7979
# Custom
8080
/target/
81-
*.xls*
81+
*.xls*
82+
.idea
83+
*.iml

apps/excel-diff-checker.jar

-106 Bytes
Binary file not shown.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private static List<SheetProcessorTask> processAllSheets(XSSFWorkbook resultWork
116116
List<SheetProcessorTask> tasks = new ArrayList<>();
117117

118118
Consumer<Sheet> consumer = sheet1 -> {
119-
XSSFSheet sheet2 = (XSSFSheet) workbook2.getSheet(sheet1.getSheetName());
119+
XSSFSheet sheet2 = workbook2.getSheet(sheet1.getSheetName());
120120

121121
if(sheet2 == null) {
122122
System.out.println(String.format("Sheet[%s] doesn't exist in workbook[%s]", sheet1.getSheetName(), FILE_NAME2));
@@ -135,13 +135,13 @@ private static List<SheetProcessorTask> processSpecifiedSheets(XSSFWorkbook resu
135135
List<String> sheets = Arrays.asList(specifiedSheets.split(","));
136136

137137
Consumer<String> consumer = sheetName -> {
138-
XSSFSheet sheet1 = (XSSFSheet) resultWorkbook.getSheet(sheetName);
139-
XSSFSheet sheet2 = (XSSFSheet) workbook2.getSheet(sheetName);
138+
XSSFSheet sheet1 = resultWorkbook.getSheet(sheetName);
139+
XSSFSheet sheet2 = workbook2.getSheet(sheetName);
140140

141141
if(sheet1 == null || sheet2 == null ) {
142142
System.out.println(String.format("Sheet[%s] doesn't exist in both workbooks", sheetName));
143143
} else
144-
tasks.add(new SheetProcessorTask((XSSFSheet) sheet1, sheet2, remarksOnly));
144+
tasks.add(new SheetProcessorTask(sheet1, sheet2, remarksOnly));
145145
};
146146

147147
sheets.forEach(consumer);

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public CallableValue call() {
3939

4040
private void processAllRows() throws Exception {
4141
for (int rowIndex = 0; rowIndex <= sheet1.getLastRowNum(); rowIndex++) {
42-
XSSFRow row1 = (XSSFRow) sheet1.getRow(rowIndex);
43-
XSSFRow row2 = (XSSFRow) sheet2.getRow(rowIndex);
42+
XSSFRow row1 = sheet1.getRow(rowIndex);
43+
XSSFRow row2 = sheet2.getRow(rowIndex);
4444

4545
if (row1 == null || row2 == null) {
4646
if (!(row1 == null && row2 == null)) {
@@ -59,28 +59,30 @@ private void processAllColumns(XSSFRow row1, XSSFRow row2) throws Exception {
5959
boolean isRow1Blank = true, isRow2Blank = true;
6060

6161
for (int columnIndex = 0; columnIndex <= row1.getLastCellNum(); columnIndex++) {
62-
XSSFCell cell1 = (XSSFCell) row1.getCell(columnIndex);
63-
XSSFCell cell2 = (XSSFCell) row2.getCell(columnIndex);
62+
XSSFCell cell1 = row1.getCell(columnIndex);
63+
XSSFCell cell2 = row2.getCell(columnIndex);
64+
String cell1Value = Utility.getCellValue(cell1);
65+
String cell2Value = Utility.getCellValue(cell2);
6466

65-
if (Utility.hasNoContent(cell1)) {
66-
if (Utility.hasContent(cell2)) {
67+
if (cell1Value.isEmpty()) {
68+
if (!cell2Value.isEmpty()) {
6769
isRow2Blank = false;
6870
crt.setDiffFlag(true);
6971
Utility.processDiffForColumn(cell1 == null ? row1.createCell(columnIndex) : cell1, remarksOnly,
70-
Utility.getCellValue(cell2), rowRemarks);
72+
cell2Value, rowRemarks);
7173
}
72-
} else if (Utility.hasNoContent(cell2)) {
73-
if (Utility.hasContent(cell1)) {
74+
} else if (cell2Value.isEmpty()) {
75+
if (!cell1Value.isEmpty()) {
7476
isRow1Blank = false;
7577
crt.setDiffFlag(true);
76-
Utility.processDiffForColumn(cell1, remarksOnly, cell2 == null? null : Utility.getCellValue(cell2), rowRemarks);
78+
Utility.processDiffForColumn(cell1, remarksOnly, cell2Value, rowRemarks);
7779
}
7880
} else {
7981
isRow1Blank = isRow2Blank = false;
8082

81-
if (!Utility.getCellValue(cell1).equals(Utility.getCellValue(cell2))) {
83+
if (!cell1Value.equals(cell2Value)) {
8284
crt.setDiffFlag(true);
83-
Utility.processDiffForColumn(cell1, remarksOnly, Utility.getCellValue(cell2), rowRemarks);
85+
Utility.processDiffForColumn(cell1, remarksOnly, cell2Value, rowRemarks);
8486
}
8587
}
8688
}
@@ -97,24 +99,24 @@ else if(isRow1Blank && !isRow2Blank)
9799

98100
public void processNullRow(XSSFSheet sheet1, int rowIndex, XSSFRow row2) throws Exception {
99101
XSSFRow row1 = sheet1.getRow(rowIndex);
100-
StringBuilder rowRemarks = new StringBuilder();
102+
// StringBuilder rowRemarks = new StringBuilder();
101103

102104
if (row1 == null) {
103105
if (row2.getPhysicalNumberOfCells() != 0) {
104106
row1 = sheet1.createRow(rowIndex);
105107
crt.setDiffFlag(true);
106-
for (int columnIndex = 0; columnIndex <= row2.getLastCellNum(); columnIndex++) {
107-
Utility.processDiffForColumn(row1.createCell(0), remarksOnly,
108-
Utility.getCellValue(row2.getCell(columnIndex)), rowRemarks);
109-
}
108+
// for (int columnIndex = 0; columnIndex <= row2.getLastCellNum(); columnIndex++) {
109+
// Utility.processDiffForColumn(row1.createCell(0), remarksOnly,
110+
// Utility.getCellValue(row2.getCell(columnIndex)), rowRemarks);
111+
// }
110112
crt.getDiffContainer().append(String.format("\nAdded Row[%s] in Sheet[%s]",
111113
(row1.getRowNum() + 1), sheet1.getSheetName()));
112114
}
113115
} else {
114116
if (row1.getPhysicalNumberOfCells() != 0) {
115117
crt.setDiffFlag(true);
116-
XSSFCell cell1 = row1.getCell(0);
117-
Utility.processDiffForColumn(cell1 == null ? row1.createCell(0) : cell1, remarksOnly, "Null row", rowRemarks);
118+
// XSSFCell cell1 = row1.getCell(0);
119+
// Utility.processDiffForColumn(cell1 == null ? row1.createCell(0) : cell1, remarksOnly, "Null row", rowRemarks);
118120
crt.getDiffContainer().append(String.format("\nRemoved Row[%s] of Sheet[%s]",
119121
(row1.getRowNum() + 1), sheet1.getSheetName()));
120122
}

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

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@
2020
*/
2121
public class Utility {
2222

23-
public static boolean hasNoContent(XSSFCell cell) {
24-
return cell == null || cell.getRawValue() == null || cell.getRawValue().equals("");
25-
}
26-
27-
public static boolean hasContent(XSSFCell cell) {
28-
return cell != null && cell.getRawValue() != null && !cell.getRawValue().equals("");
29-
}
30-
3123
@SuppressWarnings("rawtypes")
3224
public static void processDiffForColumn(XSSFCell cell1, boolean remarksOnly, String note, StringBuilder sb) throws Exception {
3325

@@ -57,7 +49,7 @@ public static void processDiffForColumn(XSSFCell cell1, boolean remarksOnly, Str
5749
Comment comment = drawing.createCellComment(anchor);
5850

5951
//set the comment text and author
60-
comment.setString(factory.createRichTextString("Found " + note));
52+
comment.setString(factory.createRichTextString("Found: " + note));
6153
comment.setAuthor("SYSTEM");
6254

6355
cell1.setCellComment(comment);
@@ -67,27 +59,23 @@ public static void processDiffForColumn(XSSFCell cell1, boolean remarksOnly, Str
6759
public static String getCellValue(XSSFCell cell) throws Exception {
6860
String content = "";
6961

62+
if(cell == null) return content;
63+
7064
CellType cellType = cell.getCellType();
7165

7266
if(cellType == CellType.FORMULA)
7367
cellType = cell.getCachedFormulaResultType();
7468

7569
switch(cellType) {
76-
case BLANK: content += null;
77-
break;
78-
case BOOLEAN: content += cell.getBooleanCellValue();
79-
break;
80-
case ERROR: content += cell.getErrorCellString();
81-
break;
82-
case STRING: content += cell.getRichStringCellValue();
83-
break;
84-
case NUMERIC: content += DateUtil.isCellDateFormatted(cell) ? cell.getDateCellValue() :
85-
cell.getNumericCellValue();
86-
break;
87-
case _NONE: content += null;
88-
break;
89-
default: throw new Exception(String.format("Unexpected Cell[%s] Type[%s] of Sheet[%s]", cell.getReference(),
90-
cell.getCellType(), cell.getSheet().getSheetName()));
70+
case BLANK:
71+
case _NONE: break;
72+
case BOOLEAN: content += cell.getBooleanCellValue(); break;
73+
case ERROR: content += trimToEmpty(cell.getErrorCellString()); break;
74+
case STRING: content += trimToEmpty(cell.getRichStringCellValue().getString()); break;
75+
case NUMERIC: content += DateUtil.isCellDateFormatted(cell) ? cell.getDateCellValue() :
76+
!content.equals(cell.getRawValue())? cell.getNumericCellValue() : content; break;
77+
default: throw new Exception(String.format("Unexpected Cell[%s] Type[%s] of Sheet[%s]", cell.getReference(),
78+
cell.getCellType(), cell.getSheet().getSheetName()));
9179
}
9280
return content;
9381
}
@@ -99,4 +87,8 @@ public static boolean deleteIfExists(File file) {
9987
return false;
10088
}
10189

90+
private static String trimToEmpty(String value) {
91+
return value == null? "" : value.trim();
92+
}
93+
10294
}

0 commit comments

Comments
 (0)