Skip to content

Commit bd67f73

Browse files
Concurrent Modification exception bugfix in multi-thread
1 parent 65803f9 commit bd67f73

File tree

3 files changed

+40
-37
lines changed

3 files changed

+40
-37
lines changed

apps/excel-diff-checker.jar

-443 Bytes
Binary file not shown.

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private void processAllColumns(XSSFRow row1, XSSFRow row2) throws Exception {
5858
for (int columnIndex = 0; columnIndex <= row1.getLastCellNum(); columnIndex++) {
5959
XSSFCell cell1 = (XSSFCell) row1.getCell(columnIndex);
6060
XSSFCell cell2 = (XSSFCell) row2.getCell(columnIndex);
61-
61+
6262
if (Utility.hasNoContent(cell1)) {
6363
if (Utility.hasContent(cell2)) {
6464
crt.setDiffFlag(true);
@@ -68,7 +68,7 @@ private void processAllColumns(XSSFRow row1, XSSFRow row2) throws Exception {
6868
} else if (Utility.hasNoContent(cell2)) {
6969
if (Utility.hasContent(cell1)) {
7070
crt.setDiffFlag(true);
71-
Utility.processDiffForColumn(cell1, commentFlag, Utility.getCellValue(cell2), crt.getDiffContainer());
71+
Utility.processDiffForColumn(cell1, commentFlag, cell2 == null? null : Utility.getCellValue(cell2), crt.getDiffContainer());
7272
}
7373
} else if (!cell1.getRawValue().equals(cell2.getRawValue())) {
7474
crt.setDiffFlag(true);
@@ -83,14 +83,15 @@ public void processNullRow(XSSFSheet sheet1, int rowIndex, XSSFRow row2) throws
8383
if (row1 == null) {
8484
if (row2.getPhysicalNumberOfCells() != 0) {
8585
row1 = sheet1.createRow(rowIndex);
86-
86+
crt.setDiffFlag(true);
8787
for (int columnIndex = 0; columnIndex <= row2.getLastCellNum(); columnIndex++) {
8888
Utility.processDiffForColumn(row1.createCell(0), commentFlag,
8989
Utility.getCellValue(row2.getCell(columnIndex)), crt.getDiffContainer());
9090
}
9191
}
9292
} else {
9393
if (row1.getPhysicalNumberOfCells() != 0) {
94+
crt.setDiffFlag(true);
9495
XSSFCell cell1 = row1.getCell(0);
9596
Utility.processDiffForColumn(cell1 == null ? row1.createCell(0) : cell1, commentFlag, "Null row", crt.getDiffContainer());
9697
}

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

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,68 +30,70 @@ public static boolean hasContent(XSSFCell cell) {
3030

3131
@SuppressWarnings("rawtypes")
3232
public static void processDiffForColumn(XSSFCell cell1, boolean commentFlag, String note, StringBuilder sb) throws Exception {
33-
33+
3434
Sheet sheet = cell1.getSheet();
35-
35+
3636
sb.append(String.format("Diff at cell[%s] of sheet[%s]\n", cell1.getReference(), sheet.getSheetName()));
37-
37+
3838
if(!commentFlag) {
3939
sb.append(String.format("Expected: [%s], Found: [%s]\n", getCellValue(cell1), note));
4040
return;
4141
}
42-
43-
CreationHelper factory = sheet.getWorkbook().getCreationHelper();
44-
//get an existing cell or create it otherwise:
45-
46-
ClientAnchor anchor = factory.createClientAnchor();
47-
//i found it useful to show the comment box at the bottom right corner
48-
anchor.setCol1(cell1.getColumnIndex() + 1); //the box of the comment starts at this given column...
49-
anchor.setCol2(cell1.getColumnIndex() + 3); //...and ends at that given column
50-
anchor.setRow1(cell1.getRowIndex() + 1); //one row below the cell...
51-
anchor.setRow2(cell1.getRowIndex() + 5); //...and 4 rows high
52-
53-
Drawing drawing = sheet.createDrawingPatriarch();
54-
Comment comment = drawing.createCellComment(anchor);
55-
56-
//set the comment text and author
57-
comment.setString(factory.createRichTextString("Found " + note));
58-
comment.setAuthor("SYSTEM");
59-
60-
cell1.setCellComment(comment);
42+
43+
synchronized(sheet) {
44+
CreationHelper factory = sheet.getWorkbook().getCreationHelper();
45+
//get an existing cell or create it otherwise:
46+
47+
ClientAnchor anchor = factory.createClientAnchor();
48+
//i found it useful to show the comment box at the bottom right corner
49+
anchor.setCol1(cell1.getColumnIndex() + 1); //the box of the comment starts at this given column...
50+
anchor.setCol2(cell1.getColumnIndex() + 3); //...and ends at that given column
51+
anchor.setRow1(cell1.getRowIndex() + 1); //one row below the cell...
52+
anchor.setRow2(cell1.getRowIndex() + 5); //...and 4 rows high
53+
54+
Drawing drawing = sheet.createDrawingPatriarch();
55+
Comment comment = drawing.createCellComment(anchor);
56+
57+
//set the comment text and author
58+
comment.setString(factory.createRichTextString("Found " + note));
59+
comment.setAuthor("SYSTEM");
60+
61+
cell1.setCellComment(comment);
62+
}
6163
}
6264

6365
public static String getCellValue(XSSFCell cell) throws Exception {
6466
String content = "";
65-
67+
6668
CellType cellType = cell.getCellType();
67-
69+
6870
if(cellType == CellType.FORMULA)
6971
cellType = cell.getCachedFormulaResultType();
70-
72+
7173
switch(cellType) {
7274
case BLANK: content += null;
73-
break;
75+
break;
7476
case BOOLEAN: content += cell.getBooleanCellValue();
75-
break;
77+
break;
7678
case ERROR: content += cell.getErrorCellString();
77-
break;
79+
break;
7880
case STRING: content += cell.getRichStringCellValue();
79-
break;
81+
break;
8082
case NUMERIC: content += DateUtil.isCellDateFormatted(cell) ? cell.getDateCellValue() :
8183
cell.getNumericCellValue();
82-
break;
84+
break;
8385
case _NONE: content += null;
84-
break;
86+
break;
8587
default: throw new Exception(String.format("Unexpected Cell[%s] Type[%s] of sheet[%s]", cell.getReference(),
8688
cell.getCellType(), cell.getSheet().getSheetName()));
87-
}
89+
}
8890
return content;
8991
}
90-
92+
9193
public static boolean deleteIfExists(File file) {
9294
if(file.exists())
9395
return file.delete();
94-
96+
9597
return false;
9698
}
9799

0 commit comments

Comments
 (0)