2
votes

I need to compare Sheets and I just noticed there is no equals() method in the class. It extends Iterable but if I have a custom made Pojo with a sheet inside, and make/override an equals() method, it will probably fail.

Does this mean I need to make a custom made "equals" that iterates over all the Rows/Cells?

Why doesn't Sheet have a equals()? Thanks

(source: https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Sheet.html)

2
Just guessing here: probably that comparison is pretty expensive and people didn't want to default to that. - GhostCat
Just to add +info, neither Sheet, Row or Cell have any equals(). I cannot believe Cell.equals() would be expensive. - JonyD
Sheet is an interface. Usually, interfaces do not declare standard methods (like equals, hashCode, toString etc.) Perhaps, you want to ask why its implementations do not override Object.equals - default locale
@defaultlocale you are right. All are interfaces. - JonyD

2 Answers

2
votes

You have to implement it yourself.

My guess is that the equlas method was not implemented because:

  • It might be expensive (as @GhostCat suggests) and
  • It is not 100% clear how it should be implemented:
    • Someone just wants to compare the Sheets' names (for map lookups etc), someone wants to compare all cells.
    • And when comparing cells, someone wants to separate between plain values and values derived from formulas while someone doesn't care.
    • Someone considers an empty cell and a non-defined cell equal, while someone else doesn't.
    • Etc, etc.
1
votes

You can use simple-excel to do that (please, find the GIT project). Please, find an example in their blog:

Workbook actual = new HSSFWorkbook(...);
Workbook expected = new HSSFWorkbook(...);
assertThat(actual, sameWorkbook(expected));

Using that, you will have useful information in case both sheets are not equal:

java.lang.AssertionError:
Expected: entire workbook to be equal
     but: cell at "C14" contained <"bananas"> expected <nothing>,
          cell at "C15" contained <"£1,850,000"> expected <"£1,850,000.00">,
          cell at "D16" contained <nothing> expected <"Tue Sep 04 06:30:00">