|
3 | 3 | import java.io.File; |
4 | 4 | import java.io.FileInputStream; |
5 | 5 | import java.io.FileOutputStream; |
| 6 | +import java.util.function.Consumer; |
6 | 7 |
|
| 8 | +import org.apache.poi.ss.usermodel.Sheet; |
7 | 9 | import org.apache.poi.xssf.usermodel.XSSFCell; |
8 | 10 | import org.apache.poi.xssf.usermodel.XSSFRow; |
9 | 11 | import org.apache.poi.xssf.usermodel.XSSFSheet; |
|
16 | 18 | */ |
17 | 19 | public class ExcelDiffChecker { |
18 | 20 |
|
19 | | - private static boolean success = true; |
20 | 21 | private static boolean diffFound = false; |
21 | 22 | private static boolean commentFlag = true; |
| 23 | + private static String FILE_NAME1, FILE_NAME2; |
22 | 24 |
|
23 | 25 | public static void main(String[] args) { |
24 | 26 |
|
25 | | - String FILE_NAME1 = args[0]; |
26 | | - String FILE_NAME2 = args[1]; |
| 27 | + FILE_NAME1 = args[0]; |
| 28 | + FILE_NAME2 = args[1]; |
27 | 29 | commentFlag = args.length == 2; |
28 | 30 |
|
29 | 31 | String RESULT_FILE = FILE_NAME1.substring(0, FILE_NAME1.lastIndexOf(".")) + " vs " + FILE_NAME2; |
30 | 32 |
|
31 | 33 | File resultFile = new File(RESULT_FILE); |
32 | | - if(resultFile.exists()) |
33 | | - resultFile.delete(); |
| 34 | + |
| 35 | + Utility.deleteIfExists(resultFile); |
34 | 36 |
|
35 | 37 | try(XSSFWorkbook resultWorkbook = new XSSFWorkbook(new FileInputStream(new File(FILE_NAME1))); |
36 | 38 | XSSFWorkbook workbook2 = new XSSFWorkbook(new FileInputStream(new File(FILE_NAME2)))) { |
37 | 39 |
|
38 | | - if(resultWorkbook.getNumberOfSheets() != workbook2.getNumberOfSheets()) { |
39 | | - System.out.println("Unequal number of sheets present!"); |
40 | | - success = false; |
41 | | - } |
| 40 | + processAllSheets(resultWorkbook, workbook2); |
42 | 41 |
|
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!"); |
58 | 47 | } |
| 48 | + } |
| 49 | + } else |
| 50 | + System.out.println("No diff found!"); |
| 51 | + } catch (Exception e) { |
| 52 | + e.printStackTrace(System.out); |
| 53 | + } |
| 54 | + } |
59 | 55 |
|
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); |
98 | 68 | } |
| 69 | + }; |
99 | 70 |
|
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; |
110 | 85 | } |
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"); |
116 | 125 | } |
117 | 126 | } |
118 | 127 |
|
|
0 commit comments