I was studying read/write of excel using apachePOI library, i found two types of solution, i.e., one achieved using using HSSFWorkbook and other one with Workbook. Now i have doubt why there is two solution to achieve single functionality.
My Code:
FileInputStream fis=new FileInputStream("D:\\Extras\\SeleniumPractice\\TestData.xlsx");
Workbook workbook=WorkbookFactory.create(fis);
Sheet sheet=workbook.getSheet("TestData");
When i searched:
FileInputStream file = new FileInputStream(new File("C:\\test.xls"));
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
Thanks in advance.. :)
Thanks Mahesh
HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (. xlsx) file format. HSSF and XSSF provides ways to read spreadsheets create, modify, read and write XLS spreadsheets.
Apache POI terminologies Workbook: A workbook is the high-level representation of a Spreadsheet. Row: As the name suggests, It represents a row in the spreadsheet. Cell: A cell represents a column in the spreadsheet.
Hence HSSF will work just fine with xls files, as the limit is xls and not HSSF. xlsx files support up to 1M+ rows, and XSSF will handle them correctly.
Difference between JXL and POI is that Java JXL does not support the Excel 2007+ ". xlsx" format; it only supports ". xls" format. Apache POI supports both with a common design.
Workbook is the common interface, which works for both HSSF (.xls) and XSSF (.xlsx). It was introduced in POI 3.5, if my memory is correct.
If you use the common interfaces like Workbook, you can have the same code transparently work with both HSSF and XSSF
If you code for just HSSF via HSSFWorkbook, you can only work with .xls files. I'd suggest you go for the common ones wherever possible
Your loading code should be something like:
Workbook wb = WorkbookFactory.create(new File("test.xls"));
Sheet s = wb.getSheetAt(0);
....
That will auto-detect the type of the file, and give you back a working object for either .xls or .xlsx based on what it finds
The Major difference I know is
Workbook is an interface, while HSSFWorkbook, SXSSFWorkbook, XSSFWorkbook are the clases that are implementing the Workbook interface.
public interface Workbook High level representation of a Excel workbook. This is the first object most users will construct whether they are reading or writing a workbook.
public final class HSSFWorkbook extends POIDocument implements Workbook High level representation of a .xls workbook. This is the first object most users will construct whether they are reading or writing a .xls workbook.
for details refer POI api docs
What is Apache POI?
Apache POI is a popular API that allows programmers to create, modify,
and display MS Office files using Java programs.
Apache POI is a 100% open source library provided by Apache Software Foundation.
Workbook
This is the super-interface of all classes that create or maintain Excel workbooks. It belongs to the org.apache.poi.ss.usermodel package. The two classes that implement this interface are as follows:
(1). HSSFWorkbook: This class has methods to read and write Microsoft Excel files in .xls format.
(2).XSSFWorkbook: This class has methods to read and write Microsoft Excel and OpenOffice xml files in .xls or .xlsx format.
HSSFWorkbook
It is a high-level class under the org.apache.poi.hssf.usermodel package. It implements the Workbook interface and is used for Excel files in .xls format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With