1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.examples.ss;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* Utility to compare Excel File Contents cell by cell for all sheets.
*
* <p>This utility will be used to compare Excel File Contents cell by cell for all sheets programmatically.</p>
*
* <p>Below are the list of Attribute comparison supported in this version.</p>
*
* <ul>
* <li>Cell Alignment</li>
* <li>Cell Border Attributes</li>
* <li>Cell Data</li>
* <li>Cell Data-Type</li>
* <li>Cell Fill Color</li>
* <li>Cell Fill pattern</li>
* <li>Cell Font Attributes</li>
* <li>Cell Font Family</li>
* <li>Cell Font Size</li>
* <li>Cell Protection</li>
* <li>Name of the sheets</li>
* <li>Number of Columns</li>
* <li>Number of Rows</li>
* <li>Number of Sheet</li>
* </ul>
*
* <p>(Some of the above attribute comparison only work for *.xlsx format currently. In future it can be enhanced.)</p>
*
* <p><b>Usage:</b></p>
*
* <pre>
* {@code
* Workbook wb1 = WorkbookFactory.create(new File("workBook1.xls"));
* Workbook wb2 = WorkbookFactory.create(new File("workBook2.xls"));
* List<String> listOfDifferences = ExcelComparator.compare(wb1, wb2);
* for (String differences : listOfDifferences)
* System.out.println(differences);
* System.out.println("DifferenceFound = "+ excelFileDifference.isDifferenceFound);
* }
* </pre>
*/
@SuppressWarnings({"java:S106","java:S4823","java:S1192"})
public class ExcelComparator {
private static final String CELL_DATA_DOES_NOT_MATCH = "Cell Data does not Match ::";
private static final String CELL_FONT_ATTRIBUTES_DOES_NOT_MATCH = "Cell Font Attributes does not Match ::";
private static class Locator {
Workbook workbook;
Sheet sheet;
Row row;
Cell cell;
}
private final List<String> listOfDifferences = new ArrayList<>();
private final DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);
public static void main(String[] args) throws Exception {
if (args.length != 2 || !(new File(args[0]).exists()) || !(new File(args[1]).exists())) {
System.err.println("java -cp <classpath> "+ExcelComparator.class.getCanonicalName()+" <workbook1.xls/x> <workbook2.xls/x");
System.exit(-1);
}
try (Workbook wb1 = WorkbookFactory.create(new File(args[0]), null, true)) {
try (Workbook wb2 = WorkbookFactory.create(new File(args[1]), null, true)) {
for (String d : ExcelComparator.compare(wb1, wb2)) {
System.out.println(d);
}
}
}
}
/**
* Utility to compare Excel File Contents cell by cell for all sheets.
*
* @param wb1 the workbook1
* @param wb2 the workbook2
* @return the Excel file difference containing a flag and a list of differences
*/
public static List<String> compare(Workbook wb1, Workbook wb2) {
Locator loc1 = new Locator();
Locator loc2 = new Locator();
loc1.workbook = wb1;
loc2.workbook = wb2;
ExcelComparator excelComparator = new ExcelComparator();
excelComparator.compareNumberOfSheets(loc1, loc2 );
excelComparator.compareSheetNames(loc1, loc2);
excelComparator.compareSheetData(loc1, loc2);
return Collections.unmodifiableList(excelComparator.listOfDifferences);
}
/**
* Compare data in all sheets.
*/
private void compareDataInAllSheets(Locator loc1, Locator loc2) {
for (int i = 0; i < loc1.workbook.getNumberOfSheets(); i++) {
if (loc2.workbook.getNumberOfSheets() <= i) {
return;
}
loc1.sheet = loc1.workbook.getSheetAt(i);
loc2.sheet = loc2.workbook.getSheetAt(i);
compareDataInSheet(loc1, loc2);
}
}
private void compareDataInSheet(Locator loc1, Locator loc2) {
for (int j = 0; j <= loc1.sheet.getLastRowNum(); j++) {
if (loc2.sheet.getLastRowNum() <= j) {
return;
}
loc1.row = loc1.sheet.getRow(j);
loc2.row = loc2.sheet.getRow(j);
if ((loc1.row == null) || (loc2.row == null)) {
continue;
}
compareDataInRow(loc1, loc2);
}
}
private void compareDataInRow(Locator loc1, Locator loc2) {
for (int k = 0; k <= loc1.row.getLastCellNum(); k++) {
if (loc2.row.getLastCellNum() <= k) {
return;
}
loc1.cell = loc1.row.getCell(k);
loc2.cell = loc2.row.getCell(k);
if ((loc1.cell == null) || (loc2.cell == null)) {
continue;
}
compareDataInCell(loc1, loc2);
}
}
private void compareDataInCell(Locator loc1, Locator loc2) {
if (isCellTypeMatches(loc1, loc2)) {
final CellType loc1cellType = loc1.cell.getCellType();
switch(loc1cellType) {
case BLANK:
case STRING:
case ERROR:
isCellContentMatches(loc1,loc2);
break;
case BOOLEAN:
isCellContentMatchesForBoolean(loc1,loc2);
break;
case FORMULA:
isCellContentMatchesForFormula(loc1,loc2);
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(loc1.cell)) {
isCellContentMatchesForDate(loc1,loc2);
} else {
isCellContentMatchesForNumeric(loc1,loc2);
}
break;
default:
throw new IllegalStateException("Unexpected cell type: " + loc1cellType);
}
}
isCellFillPatternMatches(loc1,loc2);
isCellAlignmentMatches(loc1,loc2);
isCellHiddenMatches(loc1,loc2);
isCellLockedMatches(loc1,loc2);
isCellFontFamilyMatches(loc1,loc2);
isCellFontSizeMatches(loc1,loc2);
isCellFontBoldMatches(loc1,loc2);
isCellUnderLineMatches(loc1,loc2);
isCellFontItalicsMatches(loc1,loc2);
isCellBorderMatches(loc1,loc2,'t');
isCellBorderMatches(loc1,loc2,'l');
isCellBorderMatches(loc1,loc2,'b');
isCellBorderMatches(loc1,loc2,'r');
isCellFillBackGroundMatches(loc1,loc2);
}
/**
* Compare number of columns in sheets.
*/
private void compareNumberOfColumnsInSheets(Locator loc1, Locator loc2) {
for (int i = 0; i < loc1.workbook.getNumberOfSheets(); i++) {
if (loc2.workbook.getNumberOfSheets() <= i) {
return;
}
loc1.sheet = loc1.workbook.getSheetAt(i);
loc2.sheet = loc2.workbook.getSheetAt(i);
Iterator<Row> ri1 = loc1.sheet.rowIterator();
Iterator<Row> ri2 = loc2.sheet.rowIterator();
int num1 = (ri1.hasNext()) ? ri1.next().getPhysicalNumberOfCells() : 0;
int num2 = (ri2.hasNext()) ? ri2.next().getPhysicalNumberOfCells() : 0;
if (num1 != num2) {
String str = String.format(Locale.ROOT, "%s%nworkbook1 -> %s [%d] != workbook2 -> %s [%d]",
"Number Of Columns does not Match ::",
loc1.sheet.getSheetName(), num1,
loc2.sheet.getSheetName(), num2
);
listOfDifferences.add(str);
}
}
}
/**
* Compare number of rows in sheets.
*/
private void compareNumberOfRowsInSheets(Locator loc1, Locator loc2) {
for (int i = 0; i < loc1.workbook.getNumberOfSheets(); i++) {
if (loc2.workbook.getNumberOfSheets() <= i) {
return;
}
loc1.sheet = loc1.workbook.getSheetAt(i);
loc2.sheet = loc2.workbook.getSheetAt(i);
int num1 = loc1.sheet.getPhysicalNumberOfRows();
int num2 = loc2.sheet.getPhysicalNumberOfRows();
if (num1 != num2) {
String str = String.format(Locale.ROOT, "%s%nworkbook1 -> %s [%d] != workbook2 -> %s [%d]",
"Number Of Rows does not Match ::",
loc1.sheet.getSheetName(), num1,
loc2.sheet.getSheetName(), num2
);
listOfDifferences.add(str);
}
}
}
/**
* Compare number of sheets.
*/
private void compareNumberOfSheets(Locator loc1, Locator loc2) {
int num1 = loc1.workbook.getNumberOfSheets();
int num2 = loc2.workbook.getNumberOfSheets();
if (num1 != num2) {
String str = String.format(Locale.ROOT, "%s%nworkbook1 [%d] != workbook2 [%d]",
"Number of Sheets do not match ::",
num1, num2
);
listOfDifferences.add(str);
}
}
/**
* Compare sheet data.
*/
private void compareSheetData(Locator loc1, Locator loc2) {
compareNumberOfRowsInSheets(loc1, loc2);
compareNumberOfColumnsInSheets(loc1, loc2);
compareDataInAllSheets(loc1, loc2);
}
/**
* Compare sheet names.
*/
private void compareSheetNames(Locator loc1, Locator loc2) {
for (int i = 0; i < loc1.workbook.getNumberOfSheets(); i++) {
String name1 = loc1.workbook.getSheetName(i);
String name2 = (loc2.workbook.getNumberOfSheets() > i) ? loc2.workbook.getSheetName(i) : "";
if (!name1.equals(name2)) {
String str = String.format(Locale.ROOT, "%s%nworkbook1 -> %s [%d] != workbook2 -> %s [%d]",
"Name of the sheets do not match ::", name1, i+1, name2, i+1
);
listOfDifferences.add(str);
}
}
}
/**
* Formats the message.
*/
private void addMessage(Locator loc1, Locator loc2, String messageStart, String value1, String value2) {
String str =
String.format(Locale.ROOT, "%s%nworkbook1 -> %s -> %s [%s] != workbook2 -> %s -> %s [%s]",
messageStart,
loc1.sheet.getSheetName(), new CellReference(loc1.cell).formatAsString(), value1,
loc2.sheet.getSheetName(), new CellReference(loc2.cell).formatAsString(), value2
);
listOfDifferences.add(str);
}
/**
* Checks if cell alignment matches.
*/
private void isCellAlignmentMatches(Locator loc1, Locator loc2) {
CellStyle style1 = loc1.cell.getCellStyle();
if (style1 == null) {
return;
}
CellStyle style2 = loc2.cell.getCellStyle();
if (style2 == null) {
return;
}
HorizontalAlignment align1 = style1.getAlignment();
HorizontalAlignment align2 = style2.getAlignment();
if (align1 != align2) {
addMessage(loc1, loc2,
"Cell Alignment does not Match ::",
align1.name(),
align2.name()
);
}
}
/**
* Checks if cell border bottom matches.
*/
private void isCellBorderMatches(Locator loc1, Locator loc2, char borderSide) {
if (!(loc1.cell instanceof XSSFCell)) {
return;
}
CellStyle style1 = loc1.cell.getCellStyle();
if (style1 == null) {
return;
}
CellStyle style2 = loc2.cell.getCellStyle();
if (style2 == null) {
return;
}
boolean b1, b2;
String borderName;
switch (borderSide) {
case 'b':
b1 = style1.getBorderBottom() == BorderStyle.THIN;
b2 = style2.getBorderBottom() == BorderStyle.THIN;
borderName = "BOTTOM";
break;
case 'l':
b1 = style1.getBorderLeft() == BorderStyle.THIN;
b2 = style2.getBorderLeft() == BorderStyle.THIN;
borderName = "LEFT";
break;
case 'r':
b1 = style1.getBorderRight() == BorderStyle.THIN;
b2 = style2.getBorderRight() == BorderStyle.THIN;
borderName = "RIGHT";
break;
case 't':
default:
b1 = style1.getBorderTop() == BorderStyle.THIN;
b2 = style2.getBorderTop() == BorderStyle.THIN;
borderName = "TOP";
break;
}
if (b1 != b2) {
addMessage(loc1, loc2,
"Cell Border Attributes does not Match ::",
(b1 ? "" : "NOT ")+borderName+" BORDER",
(b2 ? "" : "NOT ")+borderName+" BORDER"
);
}
}
/**
* Checks if cell content matches.
*/
private void isCellContentMatches(Locator loc1, Locator loc2) {
String str1 = loc1.cell.toString();
String str2 = loc2.cell.toString();
if (!str1.equals(str2)) {
addMessage(loc1,loc2,CELL_DATA_DOES_NOT_MATCH,str1,str2);
}
}
/**
* Checks if cell content matches for boolean.
*/
private void isCellContentMatchesForBoolean(Locator loc1, Locator loc2) {
boolean b1 = loc1.cell.getBooleanCellValue();
boolean b2 = loc2.cell.getBooleanCellValue();
if (b1 != b2) {
addMessage(loc1,loc2,CELL_DATA_DOES_NOT_MATCH,Boolean.toString(b1),Boolean.toString(b2));
}
}
/**
* Checks if cell content matches for date.
*/
private void isCellContentMatchesForDate(Locator loc1, Locator loc2) {
Date date1 = loc1.cell.getDateCellValue();
Date date2 = loc2.cell.getDateCellValue();
if (!date1.equals(date2)) {
addMessage(loc1, loc2, CELL_DATA_DOES_NOT_MATCH, dateFormat.format(date1), dateFormat.format(date2));
}
}
/**
* Checks if cell content matches for formula.
*/
private void isCellContentMatchesForFormula(Locator loc1, Locator loc2) {
// TODO: actually evaluate the formula / NPE checks
String form1 = loc1.cell.getCellFormula();
String form2 = loc2.cell.getCellFormula();
if (!form1.equals(form2)) {
addMessage(loc1, loc2, CELL_DATA_DOES_NOT_MATCH, form1, form2);
}
}
/**
* Checks if cell content matches for numeric.
*/
private void isCellContentMatchesForNumeric(Locator loc1, Locator loc2) {
// TODO: Check for NaN
double num1 = loc1.cell.getNumericCellValue();
double num2 = loc2.cell.getNumericCellValue();
if (num1 != num2) {
addMessage(loc1, loc2, CELL_DATA_DOES_NOT_MATCH, Double.toString(num1), Double.toString(num2));
}
}
private String getCellFillBackground(Locator loc) {
Color col = loc.cell.getCellStyle().getFillForegroundColorColor();
return (col instanceof XSSFColor) ? ((XSSFColor)col).getARGBHex() : "NO COLOR";
}
/**
* Checks if cell file back ground matches.
*/
private void isCellFillBackGroundMatches(Locator loc1, Locator loc2) {
if(loc1.cell.getCellStyle() == null || loc2.cell.getCellStyle() == null) {
return;
}
String col1 = getCellFillBackground(loc1);
String col2 = getCellFillBackground(loc2);
if (!Objects.equals(col1, col2)) {
addMessage(loc1, loc2, "Cell Fill Color does not Match ::", col1, col2);
}
}
/**
* Checks if cell fill pattern matches.
*/
private void isCellFillPatternMatches(Locator loc1, Locator loc2) {
CellStyle style1 = loc1.cell.getCellStyle();
if (style1 == null) {
return;
}
CellStyle style2 = loc2.cell.getCellStyle();
if (style2 == null) {
return;
}
FillPatternType fill1 = style1.getFillPattern();
FillPatternType fill2 = style2.getFillPattern();
if (fill1 != fill2) {
addMessage(loc1, loc2,
"Cell Fill pattern does not Match ::",
fill1.name(),
fill2.name()
);
}
}
/**
* Checks if cell font bold matches.
*/
private void isCellFontBoldMatches(Locator loc1, Locator loc2) {
if (!(loc1.cell instanceof XSSFCell)) {
return;
}
CellStyle style1 = loc1.cell.getCellStyle();
if (style1 == null) {
return;
}
CellStyle style2 = loc2.cell.getCellStyle();
if (style2 == null) {
return;
}
if(hasInvalidFontIndex(loc1, loc2)) {
return;
}
boolean b1 = ((XSSFCellStyle)style1).getFont().getBold();
boolean b2 = ((XSSFCellStyle)style2).getFont().getBold();
if (b1 != b2) {
addMessage(loc1, loc2,
CELL_FONT_ATTRIBUTES_DOES_NOT_MATCH,
(b1 ? "" : "NOT ")+"BOLD",
(b2 ? "" : "NOT ")+"BOLD"
);
}
}
/**
* Checks if cell font family matches.
*/
private void isCellFontFamilyMatches(Locator loc1, Locator loc2) {
if (!(loc1.cell instanceof XSSFCell)) {
return;
}
CellStyle style1 = loc1.cell.getCellStyle();
if (style1 == null) {
return;
}
CellStyle style2 = loc2.cell.getCellStyle();
if (style2 == null) {
return;
}
if(hasInvalidFontIndex(loc1, loc2)) {
return;
}
String family1 = ((XSSFCellStyle)style1).getFont().getFontName();
String family2 = ((XSSFCellStyle)style2).getFont().getFontName();
if (!family1.equals(family2)) {
addMessage(loc1, loc2, "Cell Font Family does not Match ::", family1, family2);
}
}
private boolean hasInvalidFontIndex(Locator loc1, Locator loc2) {
int fontIdx1 = loc1.cell.getCellStyle().getFontIndex();
int fontCount1 = ((XSSFWorkbook)loc1.workbook).getStylesSource().getFonts().size();
int fontIdx2 = loc2.cell.getCellStyle().getFontIndex();
int fontCount2 = ((XSSFWorkbook)loc2.workbook).getStylesSource().getFonts().size();
if(fontIdx1 >= fontCount1 || fontIdx2 >= fontCount2) {
addMessage(loc1, loc2, "Corrupted file, cell style references a font which is not defined", Integer.toString(fontIdx1), Integer.toString(fontIdx2));
return true;
}
return false;
}
/**
* Checks if cell font italics matches.
*/
private void isCellFontItalicsMatches(Locator loc1, Locator loc2) {
if (!(loc1.cell instanceof XSSFCell)) {
return;
}
CellStyle style1 = loc1.cell.getCellStyle();
if (style1 == null) {
return;
}
CellStyle style2 = loc2.cell.getCellStyle();
if (style2 == null) {
return;
}
if(hasInvalidFontIndex(loc1, loc2)) {
return;
}
boolean b1 = ((XSSFCellStyle)style1).getFont().getItalic();
boolean b2 = ((XSSFCellStyle)style2).getFont().getItalic();
if (b1 != b2) {
addMessage(loc1, loc2,
CELL_FONT_ATTRIBUTES_DOES_NOT_MATCH,
(b1 ? "" : "NOT ")+"ITALICS",
(b2 ? "" : "NOT ")+"ITALICS"
);
}
}
/**
* Checks if cell font size matches.
*/
private void isCellFontSizeMatches(Locator loc1, Locator loc2) {
if (!(loc1.cell instanceof XSSFCell)) {
return;
}
CellStyle style1 = loc1.cell.getCellStyle();
if (style1 == null) {
return;
}
CellStyle style2 = loc2.cell.getCellStyle();
if (style2 == null) {
return;
}
if(hasInvalidFontIndex(loc1, loc2)) {
return;
}
short size1 = ((XSSFCellStyle)style1).getFont().getFontHeightInPoints();
short size2 = ((XSSFCellStyle)style2).getFont().getFontHeightInPoints();
if (size1 != size2) {
addMessage(loc1, loc2,
"Cell Font Size does not Match ::",
Short.toString(size1),
Short.toString(size2)
);
}
}
/**
* Checks if cell hidden matches.
*/
private void isCellHiddenMatches(Locator loc1, Locator loc2) {
CellStyle style1 = loc1.cell.getCellStyle();
if (style1 == null) {
return;
}
CellStyle style2 = loc2.cell.getCellStyle();
if (style2 == null) {
return;
}
boolean b1 = style1.getHidden();
boolean b2 = style2.getHidden();
if (b1 != b2) {
addMessage(loc1, loc2,
"Cell Visibility does not Match ::",
(b1 ? "" : "NOT ")+"HIDDEN",
(b2 ? "" : "NOT ")+"HIDDEN"
);
}
}
/**
* Checks if cell locked matches.
*/
private void isCellLockedMatches(Locator loc1, Locator loc2) {
CellStyle style1 = loc1.cell.getCellStyle();
if (style1 == null) {
return;
}
CellStyle style2 = loc2.cell.getCellStyle();
if (style2 == null) {
return;
}
boolean b1 = style1.getLocked();
boolean b2 = style2.getLocked();
if (b1 != b2) {
addMessage(loc1, loc2,
"Cell Protection does not Match ::",
(b1 ? "" : "NOT ")+"LOCKED",
(b2 ? "" : "NOT ")+"LOCKED"
);
}
}
/**
* Checks if cell type matches.
*/
private boolean isCellTypeMatches(Locator loc1, Locator loc2) {
CellType type1 = loc1.cell.getCellType();
CellType type2 = loc2.cell.getCellType();
if (type1 == type2) {
return true;
}
addMessage(loc1, loc2,
"Cell Data-Type does not Match in :: ",
type1.name(), type2.name()
);
return false;
}
/**
* Checks if cell under line matches.
*/
private void isCellUnderLineMatches(Locator loc1, Locator loc2) {
// TODO: distinguish underline type
if (!(loc1.cell instanceof XSSFCell)) {
return;
}
CellStyle style1 = loc1.cell.getCellStyle();
if (style1 == null) {
return;
}
CellStyle style2 = loc2.cell.getCellStyle();
if (style2 == null) {
return;
}
if(hasInvalidFontIndex(loc1, loc2)) {
return;
}
byte b1 = ((XSSFCellStyle)style1).getFont().getUnderline();
byte b2 = ((XSSFCellStyle)style2).getFont().getUnderline();
if (b1 != b2) {
addMessage(loc1, loc2,
CELL_FONT_ATTRIBUTES_DOES_NOT_MATCH,
(b1 == 1 ? "" : "NOT ")+"UNDERLINE",
(b2 == 1 ? "" : "NOT ")+"UNDERLINE"
);
}
}
}
|