比较2个excel文件的内容

我有2个Excel文件,我想比较内容,突出差异。 例如:

第一档…

name|age abc|123 def|456 second file... name|age abc|123 def|456 ghi|789 - this being the differece 

有没有第三方库来做到这一点? 或者什么是最好的办法呢?

就像DaDaDom说的那样,Apache POI就是你正在寻找的东西。 你可以从这个页面下载。 请注意,POI项目并不完全独立,您可能需要下载一些额外的库。 按照Apache POI网站上的说明进行操作。 这是你如何使用它:

 InputStream myxls = new FileInputStream("workbook.xls")); HSSFWorkbook wb = new HSSFWorkbook(myxls); // for *.xlsx use XSSFWorkbook 

如果是新文件,则可能需要在继续之前创build工作表,但在这种情况下,文件已经创build。

 HSSFSheet sheet = wb.getSheetAt(0); // first sheet HSSFRow row = sheet.getRow(0); // first row HSSFCell cell = row.getCell((short)0); // first cell 

要从单元格使用中获得价值:

 String value = cell.getStringCellValue(); 

但是,如果存储在单元格中的types是数字,则会出现错误。 在使用数字的情况下:

 Int value = cell.getCellValue(); 

这是我写的处理不同单元格数据types的方法:

 public String getValue(int x, int y){ Row row = this.activeSheet.getRow(y); if(row==null) return ""; Cell cell = row.getCell(x); if(cell==null) return ""; int type = cell.getCellType(); switch(type){ case 0: return cell.getNumericCellValue() + ""; case 1: return cell.getStringCellValue(); case 2: return cell.getCellFormula(); case 3: return ""; case 4: return cell.getBooleanCellValue() + ""; case 5: return cell.getErrorCellValue() + ""; default: return ""; } } 

我希望这个快速介绍到Apache POI将帮助您的项目:)

从这个问题 ,我的答案部分重复下面。

我的项目simple-excel提供了一堆Hamcrest Matchers,并包装了Apache POI的语法。

当你做下面的事情时,

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

你会看到,例如,

 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) 

阅读关于它的博客文章

我将使用epplus将两个文档加载到数据表中,然后遍历它们以找出差异。 根据你想突出差异的方式,你可以简单地用epplus给单元格着色,然后将它们保存回文件中。