Search code examples
javafilebinarycompare

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.


Solution

  • Here's what I ended up doing (with the heavy lifting being done by DBUnit):

    /**
     * Compares the data in the two Excel files represented by the given input
     * streams, closing them on completion
     * 
     * @param expected can't be <code>null</code>
     * @param actual can't be <code>null</code>
     * @throws Exception
     */
    private void compareExcelFiles(InputStream expected, InputStream actual)
      throws Exception
    {
      try {
        Assertion.assertEquals(new XlsDataSet(expected), new XlsDataSet(actual));
      }
      finally {
        IOUtils.closeQuietly(expected);
        IOUtils.closeQuietly(actual);
      }
    }
    

    This compares the data in the two files, with no risk of false negatives from any irrelevant metadata that might be different. Hope this helps someone.