You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExcelComparator.java 24KB

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