Easiest way to compare two Excel files in Java?

前端 未结 11 885
面向向阳花
面向向阳花 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条回答
  •  感情败类
    2021-02-02 14:52

    I needed to do something similar and was already using the Apache POI library in my project to create Excel files. So I opted to use the included ExcelExtractor interface to export both workbooks as a string of text and asserted that the strings were equal. There are implementations for both HSSF for .xls as well as XSSF for .xlsx.

    Dump to string:

    XSSFWorkbook xssfWorkbookA = ...;
    String workbookA = new XSSFExcelExtractor(xssfWorkbookA).getText();
    

    ExcelExtractor has some options for what all should be included in the string dump. I found it to have useful defaults of including sheet names. In addition it includes the text contents of the cells.

提交回复
热议问题