Easiest way to compare two Excel files in Java?

前端 未结 11 890
面向向阳花
面向向阳花 2021-02-02 14:23

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 com

11条回答
  •  猫巷女王i
    2021-02-02 14:32

    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 null
     * @param actual can't be null
     * @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.

提交回复
热议问题