最简单的方法来比较Java中的两个Excel文件?

问题描述:

我正在为生成Excel文件(这是二进制)的一些代码写一个JUnit测试。我有另一个Excel文件,包含我的预期输出。什么是最简单的方法来比较实际文件和预期的文件?

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?

确实我可以自己写代码,但我想知道是否有一个现有的方法在可信第三(例如Spring或Apache Commons)。

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.

这里是我最后做的由 DBUnit 完成):

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.