Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to compare two Excel files in Java?

I'm writing a JUnit test for some code that produces an Excel file (which is binary). I have another Excel file that contains my expected output. What's the easiest way to compare the actual file to the expected file?

Sure I could write the code myself, but I was wondering if there's an existing method in a trusted third-party library (e.g. Spring or Apache Commons) that already does this.

like image 262
Andrew Swan Avatar asked May 14 '09 23:05

Andrew Swan


People also ask

How can I compare two Excel documents quickly?

Compare 2 Excel workbooks To open two Excel files side by side, do the following: Open the workbooks you want to compare. Go to the View tab, Window group, and click the View Side by Side button. That's it!

How do you compare cells in Excel in Java?

Step 1: Open an Excel sheet and select a cell where you want to paste the comparison result. Step 2: Now, go to the formula bar, where write the formula to compare the column row-by-row for the 7th row. E.g., =IF(A7=B7, "Match", "Mismatch") and "press the Enter key".

How do I compare two spreadsheets for matching data?

With the 'View Side by Side' option, you can only compare two Excel file at one go. In case you have multiple Excel files open, when you click on the View Side by Side option, it will show you a 'Compare Side by Side' dialog box, where you can choose which file you want to compare with the active workbook.


1 Answers

You might consider using my project simple-excel which provides a bunch of Hamcrest Matchers to do the job.

When you do something like the following,

assertThat(actual, WorkbookMatcher.sameWorkbook(expected));

You'd see, for example,

java.lang.AssertionError:
Expected: entire workbook to be equal
     but: cell at "C14" contained <"bananas"> expected <nothing>,
          cell at "C15" contained <"1,850,000 EUR"> expected <"1,850,000.00 EUR">,
          cell at "D16" contained <nothing> expected <"Tue Sep 04 06:30:00">
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)

That way, you can run it from your automatted tests and get meaningful feedback whilst you're developing.

You can read more about it at this article on my site

like image 152
Toby Avatar answered Oct 23 '22 10:10

Toby