Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between HSSFWorkbook and Workbook in apache POI

Tags:

apache-poi

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

like image 452
Shetty's Avatar asked Sep 12 '13 09:09

Shetty's


People also ask

What is the difference between HSSF and XSSF workbook?

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.

What is workbook in Apache POI?

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.

Which is better XSSF or HSSF?

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.

What is the difference between JXL and Apache POI jar file?

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.


3 Answers

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

like image 65
Gagravarr Avatar answered Oct 19 '22 19:10

Gagravarr


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

like image 24
Sankumarsingh Avatar answered Oct 19 '22 17:10

Sankumarsingh


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.

like image 42
Chetan Bhagat Avatar answered Oct 19 '22 19:10

Chetan Bhagat