16

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

kritzikratzi
  • 19,662
  • 1
  • 29
  • 40
Shetty's
  • 617
  • 4
  • 13
  • 20

3 Answers3

46

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

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • Thanks Lot for your valuable answer.., – Shetty's Sep 13 '13 at 09:42
  • If this has solved your issue, please click the tick mark next to it to accept it - [see here for what that means](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Gagravarr Sep 13 '13 at 10:18
  • @Gagravarr I like your answer.. I have an issue while using `XSSF` class and `.xls` file.. But I change class to `HSSF`. Hereafter will keep your answer in mind. – Pugal Oct 19 '20 at 14:47
11

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

Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
4

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.

Chetan Bhagat
  • 610
  • 6
  • 21