Skip to content

Commit 3f5d8c8

Browse files
OptionProcessor CLI + specific sheet comparison added
1 parent 5244aef commit 3f5d8c8

File tree

9 files changed

+107
-35
lines changed

9 files changed

+107
-35
lines changed

.classpath

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,15 @@
66
<attribute name="maven.pomderived" value="true"/>
77
</attributes>
88
</classpathentry>
9-
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10-
<attributes>
11-
<attribute name="maven.pomderived" value="true"/>
12-
</attributes>
13-
</classpathentry>
149
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
1510
<attributes>
1611
<attribute name="test" value="true"/>
1712
<attribute name="optional" value="true"/>
1813
<attribute name="maven.pomderived" value="true"/>
1914
</attributes>
2015
</classpathentry>
21-
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
16+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
2217
<attributes>
23-
<attribute name="test" value="true"/>
2418
<attribute name="maven.pomderived" value="true"/>
2519
</attributes>
2620
</classpathentry>

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
.mtj.tmp/
1212

1313
# Package Files #
14-
*.jar
1514
*.war
1615
*.nar
1716
*.ear
@@ -78,6 +77,5 @@ local.properties
7877
.worksheet
7978

8079
# Custom
81-
!apps/*
8280
/target/
8381
*.xls*

.settings/org.eclipse.jdt.core.prefs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
33
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
44
org.eclipse.jdt.core.compiler.compliance=1.8
55
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
6+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
67
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
78
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
9+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
810
org.eclipse.jdt.core.compiler.release=disabled
911
org.eclipse.jdt.core.compiler.source=1.8

apps/excel-diff-checker.jar

47.7 KB
Binary file not shown.

apps/launch.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"%java8home%/bin/java" -jar excel-diff-checker.jar -b old* -t new* %* > edc.log
2+
pause

pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
<version>4.12</version>
2929
<scope>test</scope>
3030
</dependency>
31+
<dependency>
32+
<groupId>commons-cli</groupId>
33+
<artifactId>commons-cli</artifactId>
34+
<version>1.4</version>
35+
</dependency>
3136
</dependencies>
3237

3338
<build>
@@ -66,8 +71,6 @@
6671
<plugin>
6772
<groupId>org.apache.maven.plugins</groupId>
6873
<artifactId>maven-assembly-plugin</artifactId>
69-
<version>3.1.1</version>
70-
7174
<configuration>
7275
<descriptorRefs>
7376
<descriptorRef>jar-with-dependencies</descriptorRef>
@@ -82,6 +85,7 @@
8285
<goal>single</goal>
8386
</goals>
8487
<configuration>
88+
<finalName>excel-diff-checker</finalName>
8589
<archive>
8690
<manifest>
8791
<addClasspath>true</addClasspath>
@@ -91,6 +95,7 @@
9195
<descriptorRefs>
9296
<descriptorRef>jar-with-dependencies</descriptorRef>
9397
</descriptorRefs>
98+
<appendAssemblyId>false</appendAssemblyId>
9499
</configuration>
95100
</execution>
96101
</executions>

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

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33
import java.io.File;
44
import java.io.FileInputStream;
55
import java.io.FileOutputStream;
6+
import java.nio.file.Paths;
67
import java.util.ArrayList;
8+
import java.util.Arrays;
79
import java.util.List;
810
import java.util.concurrent.ExecutorService;
911
import java.util.concurrent.Executors;
1012
import java.util.concurrent.Future;
1113
import 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;
1322
import org.apache.poi.ss.usermodel.Sheet;
1423
import org.apache.poi.xssf.usermodel.XSSFSheet;
1524
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@@ -22,28 +31,33 @@
2231
public 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
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
public class SheetProcessorTask implements Callable<CallableValue> {
1717

1818
private XSSFSheet sheet1, sheet2;
19-
private boolean commentFlag;
19+
private boolean remarksOnly;
2020
private CallableValue crt;
2121

2222
public SheetProcessorTask(XSSFSheet sheet1, XSSFSheet sheet2, boolean commentFlag) {
2323
this.sheet1 = sheet1;
2424
this.sheet2 = sheet2;
25-
this.commentFlag = commentFlag;
25+
this.remarksOnly = commentFlag;
2626
}
2727

2828
@Override
@@ -62,17 +62,17 @@ private void processAllColumns(XSSFRow row1, XSSFRow row2) throws Exception {
6262
if (Utility.hasNoContent(cell1)) {
6363
if (Utility.hasContent(cell2)) {
6464
crt.setDiffFlag(true);
65-
Utility.processDiffForColumn(cell1 == null ? row1.createCell(columnIndex) : cell1, commentFlag,
65+
Utility.processDiffForColumn(cell1 == null ? row1.createCell(columnIndex) : cell1, remarksOnly,
6666
Utility.getCellValue(cell2), crt.getDiffContainer());
6767
}
6868
} else if (Utility.hasNoContent(cell2)) {
6969
if (Utility.hasContent(cell1)) {
7070
crt.setDiffFlag(true);
71-
Utility.processDiffForColumn(cell1, commentFlag, cell2 == null? null : Utility.getCellValue(cell2), crt.getDiffContainer());
71+
Utility.processDiffForColumn(cell1, remarksOnly, cell2 == null? null : Utility.getCellValue(cell2), crt.getDiffContainer());
7272
}
7373
} else if (!Utility.getCellValue(cell1).equals(Utility.getCellValue(cell2))) {
7474
crt.setDiffFlag(true);
75-
Utility.processDiffForColumn(cell1, commentFlag, Utility.getCellValue(cell2), crt.getDiffContainer());
75+
Utility.processDiffForColumn(cell1, remarksOnly, Utility.getCellValue(cell2), crt.getDiffContainer());
7676
}
7777
}
7878
}
@@ -85,15 +85,15 @@ public void processNullRow(XSSFSheet sheet1, int rowIndex, XSSFRow row2) throws
8585
row1 = sheet1.createRow(rowIndex);
8686
crt.setDiffFlag(true);
8787
for (int columnIndex = 0; columnIndex <= row2.getLastCellNum(); columnIndex++) {
88-
Utility.processDiffForColumn(row1.createCell(0), commentFlag,
88+
Utility.processDiffForColumn(row1.createCell(0), remarksOnly,
8989
Utility.getCellValue(row2.getCell(columnIndex)), crt.getDiffContainer());
9090
}
9191
}
9292
} else {
9393
if (row1.getPhysicalNumberOfCells() != 0) {
9494
crt.setDiffFlag(true);
9595
XSSFCell cell1 = row1.getCell(0);
96-
Utility.processDiffForColumn(cell1 == null ? row1.createCell(0) : cell1, commentFlag, "Null row", crt.getDiffContainer());
96+
Utility.processDiffForColumn(cell1 == null ? row1.createCell(0) : cell1, remarksOnly, "Null row", crt.getDiffContainer());
9797
}
9898
}
9999
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ public static boolean hasContent(XSSFCell cell) {
2929
}
3030

3131
@SuppressWarnings("rawtypes")
32-
public static void processDiffForColumn(XSSFCell cell1, boolean commentFlag, String note, StringBuilder sb) throws Exception {
32+
public static void processDiffForColumn(XSSFCell cell1, boolean remarksOnly, String note, StringBuilder sb) throws Exception {
3333

3434
Sheet sheet = cell1.getSheet();
3535

3636
sb.append(String.format("Diff at cell[%s] of sheet[%s]\n", cell1.getReference(), sheet.getSheetName()));
3737

38-
if(!commentFlag) {
38+
if(remarksOnly) {
3939
sb.append(String.format("Expected: [%s], Found: [%s]\n", getCellValue(cell1), note));
4040
return;
4141
}

0 commit comments

Comments
 (0)