Difference between HSSFWorkbook and Workbook in apache POI

I studied reading / writing excel using apachePOI library, I found two types of solutions, that is, one of them was achieved using HSSFWorkbook and the other with Workbook. Now, I doubt why there are two solutions to achieve a single functionality.

My code is:

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

+7
apache-poi
source share
3 answers

Workbook is a common interface that works for both HSSF (.xls) and XSSF (.xlsx). It was introduced in POI 3.5 if my memory is correct.

If you use common interfaces, such as Workbook, you can have the same code working transparently with both HSSF and XSSF

If you encode only HSSF through the HSSFWorkbook , you can only work with .xls files. I would advise you to go on general where possible.

Your download code should look something like this:

  Workbook wb = WorkbookFactory.create(new File("test.xls")); Sheet s = wb.getSheetAt(0); .... 

This will automatically detect the file type and return a work object for .xls or .xlsx based on the found

+23
source share

The significant difference that I know

A workbook is an interface, and HSSFWorkbook, SXSSFWorkbook, XSSFWorkbook are classes that implement the workbook interface.

General workbook interface Excel workbook presentation at a high level. This is the first object that most users will create, whether they will read or write a book.

public end class HSSFWorkbook extends POIDocument implements Workbook Presentation of high-level workbook .xls. This is the first object that most users will create, whether they will read or write the .xls workbook.

For more details see POI api docs

+6
source share

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 the Apache Software Foundation.

Workbook

This is the super-interface of all classes that create or support Excel workbooks. This belongs to the org.apache.poi.ss.usermodel package. Two classes that implement this interface:

(one). HSSFWorkbook: this class has methods for reading and writing Microsoft Excel .xls files.

(2) .XSSFWorkbook: this class has Microsoft Excel read and write methods and OpenOffice xml files in .xls or .xlsx format.

HSSFWorkbook

This is the high level class in the org.apache.poi.hssf.usermodel package. This implements the workbook interface and is used for Excel .xls files.

-one
source share

All Articles