33import java .io .File ;
44import java .io .FileInputStream ;
55import java .io .FileOutputStream ;
6+ import java .nio .file .Paths ;
67import java .util .ArrayList ;
8+ import java .util .Arrays ;
79import java .util .List ;
810import java .util .concurrent .ExecutorService ;
911import java .util .concurrent .Executors ;
1012import java .util .concurrent .Future ;
1113import java .util .function .Consumer ;
1214
15+ import org .apache .commons .cli .CommandLine ;
16+ import org .apache .commons .cli .CommandLineParser ;
17+ import org .apache .commons .cli .DefaultParser ;
18+ import org .apache .commons .cli .HelpFormatter ;
19+ import org .apache .commons .cli .Option ;
20+ import org .apache .commons .cli .Options ;
21+ import org .apache .commons .cli .ParseException ;
1322import org .apache .poi .ss .usermodel .Sheet ;
1423import org .apache .poi .xssf .usermodel .XSSFSheet ;
1524import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
2231public class ExcelDiffChecker {
2332
2433 private static boolean diffFound = false ;
25- private static boolean commentFlag = true ;
34+ private static boolean remarksOnly = false ;
35+ private static String specifiedSheets = null ;
2636 private static String FILE_NAME1 , FILE_NAME2 ;
37+ private static CommandLine cmd = null ;
2738
2839 public static void main (String [] args ) {
40+
41+ processOptions (args );
42+
43+ String file1path = Paths .get (FILE_NAME1 ).getFileName ().toString (), file2path = Paths .get (FILE_NAME2 ).getFileName ().toString ();
2944
30- FILE_NAME1 = args [0 ];
31- FILE_NAME2 = args [1 ];
32- commentFlag = args .length == 2 ;
33-
34- String RESULT_FILE = FILE_NAME1 .substring (0 , FILE_NAME1 .lastIndexOf ("." )) + " vs " + FILE_NAME2 ;
45+ String RESULT_FILE = file1path .substring (0 , file1path .lastIndexOf ("." )) + " vs " + file2path ;
3546
3647 File resultFile = new File (RESULT_FILE );
3748
3849 Utility .deleteIfExists (resultFile );
3950
4051 try (XSSFWorkbook resultWorkbook = new XSSFWorkbook (new FileInputStream (new File (FILE_NAME1 )));
4152 XSSFWorkbook workbook2 = new XSSFWorkbook (new FileInputStream (new File (FILE_NAME2 )))) {
53+
54+ List <SheetProcessorTask > tasks = specifiedSheets != null ?
55+ processSpecifiedSheets (resultWorkbook , workbook2 ) : processAllSheets (resultWorkbook , workbook2 );
4256
43- diffFound = CallableValue .analyzeResult (processAllSheets ( resultWorkbook , workbook2 ));
57+ diffFound = CallableValue .analyzeResult (executeAllTasks ( tasks ));
4458
4559 if (diffFound ) {
46- if (commentFlag ) {
60+ if (! remarksOnly ) {
4761 try (FileOutputStream outputStream = new FileOutputStream (RESULT_FILE )) {
4862 resultWorkbook .write (outputStream );
4963 System .out .println ("Diff excel has been generated!" );
@@ -56,7 +70,49 @@ public static void main(String[] args) {
5670 }
5771 }
5872
59- private static List <Future <CallableValue >> processAllSheets (XSSFWorkbook resultWorkbook , XSSFWorkbook workbook2 ) throws Exception {
73+ private static List <Future <CallableValue >> executeAllTasks (List <SheetProcessorTask > tasks ) throws Exception {
74+ int effectiveFixedThreadPoolSize = Math .min (Runtime .getRuntime ().availableProcessors (), tasks .size ());
75+ ExecutorService executor = Executors .newFixedThreadPool (effectiveFixedThreadPoolSize );
76+ List <Future <CallableValue >> futures = executor .invokeAll (tasks );
77+ executor .shutdown ();
78+
79+ return futures ;
80+ }
81+
82+ private static void processOptions (String [] args ) {
83+ Options options = new Options ();
84+
85+ Option base = new Option ("b" , "base" , true , "base file path" );
86+ base .setRequired (true );
87+ options .addOption (base );
88+
89+ Option target = new Option ("t" , "target" , true , "target file path" );
90+ target .setRequired (true );
91+ options .addOption (target );
92+
93+ options .addOption (new Option ("s" , "sheets" , true , "Comma separated sheets" ));
94+
95+ options .addOption (new Option ("r" , "remarks" , false , "display only remarks" ));
96+
97+ CommandLineParser parser = new DefaultParser ();
98+ HelpFormatter formatter = new HelpFormatter ();
99+
100+ try {
101+ cmd = parser .parse (options , args );
102+ } catch (ParseException e ) {
103+ System .out .println (e .getMessage ());
104+ formatter .printHelp ("utility-name" , options );
105+
106+ System .exit (1 );
107+ }
108+
109+ FILE_NAME1 = cmd .getOptionValue ("b" );
110+ FILE_NAME2 = cmd .getOptionValue ("t" );
111+ specifiedSheets = cmd .getOptionValue ("s" );
112+ remarksOnly = cmd .hasOption ("r" );
113+ }
114+
115+ private static List <SheetProcessorTask > processAllSheets (XSSFWorkbook resultWorkbook , XSSFWorkbook workbook2 ) {
60116 List <SheetProcessorTask > tasks = new ArrayList <>();
61117
62118 Consumer <Sheet > consumer = sheet1 -> {
@@ -65,17 +121,32 @@ private static List<Future<CallableValue>> processAllSheets(XSSFWorkbook resultW
65121 if (sheet2 == null ) {
66122 System .out .println (String .format ("Sheet[%s] doesn't exist in workbook[%s]" , sheet1 .getSheetName (), FILE_NAME2 ));
67123 } else
68- tasks .add (new SheetProcessorTask ((XSSFSheet ) sheet1 , sheet2 , commentFlag ));
124+ tasks .add (new SheetProcessorTask ((XSSFSheet ) sheet1 , sheet2 , remarksOnly ));
69125 };
70126
71127 resultWorkbook .forEach (consumer );
72128
73- int effectiveFixedThreadPoolSize = Math .min (Runtime .getRuntime ().availableProcessors (), tasks .size ());
74- ExecutorService executor = Executors .newFixedThreadPool (effectiveFixedThreadPoolSize );
75- List <Future <CallableValue >> futures = executor .invokeAll (tasks );
76- executor .shutdown ();
129+ return tasks ;
130+ }
131+
132+ private static List <SheetProcessorTask > processSpecifiedSheets (XSSFWorkbook resultWorkbook , XSSFWorkbook workbook2 ) {
133+ List <SheetProcessorTask > tasks = new ArrayList <>();
77134
78- return futures ;
135+ List <String > sheets = Arrays .asList (specifiedSheets .split ("," ));
136+
137+ Consumer <String > consumer = sheetName -> {
138+ XSSFSheet sheet1 = (XSSFSheet ) resultWorkbook .getSheet (sheetName );
139+ XSSFSheet sheet2 = (XSSFSheet ) workbook2 .getSheet (sheetName );
140+
141+ if (sheet1 == null || sheet2 == null ) {
142+ System .out .println (String .format ("Sheet[%s] doesn't exist in both workbooks" , sheetName ));
143+ } else
144+ tasks .add (new SheetProcessorTask ((XSSFSheet ) sheet1 , sheet2 , remarksOnly ));
145+ };
146+
147+ sheets .forEach (consumer );
148+
149+ return tasks ;
79150 }
80151
81152}
0 commit comments