我有2个excel文件,我想比较内容并突出显示差异。 例如:比较2个excel文件的内容
第一个文件...
name|age
abc|123
def|456
second file...
name|age
abc|123
def|456
ghi|789 - this being the differece
没有任何第三方库这样做呢? 或者最好的办法是什么?
我有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就是你要找的东西。您可以从this page下载。请注意,POI项目不完全独立,您可能需要下载一些额外的库。按照Apache POI网站上的说明进行操作。这是你如何使用它:
InputStream myxls = new FileInputStream("workbook.xls"));
HSSFWorkbook wb = new HSSFWorkbook(myxls); // for *.xlsx use XSSFWorkbook
如果它是你可能需要继续之前创建表一个新的文件,但在这种情况下,已经创建的文件。
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();
然而,如果储存在细胞类型是数字,你会得到一个错误。在数字的情况下使用:
Int value = cell.getCellValue();
这是我写的处理不同细胞类型的数据的方法:
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将帮助您与您的项目:)
非常感谢你 – user1646537
我会使用epplus将两个文档加载到数据表中,然后遍历它们以找出差异。 根据你想如何突出差异,你可以简单地使用epplus对单元格进行着色并将它们保存回文件中。
EPPlus也可用于Java?看起来像.net项目给我。 –
对不起,没有看到Java标签:( –
我想这个epplus提供了一个更快的访问excel文件,然后任何Java库 – user1581900
从this question,我的答案部分重复以下。
我的项目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)
如果您正在寻找读/写Excel文件中, Apache Poi(http://poi.apache.org/)库是(至少是我的)首选。 –
http://stackoverflow.com/questions/866346/easiest-way-to-compare-two-excel-files-in-java – titogeo
由gTito发布的链接可能对你非常有用,如果你需要知道2个文件完全一样。请注意,即使内容相同,不同的字体大小也意味着文件完全不同。 – user1581900