1515 *
1616 */
1717public 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}
0 commit comments