How To Read / Write Excel Spreadsheet From Java

By Angsuman Chakraborty, Gaea News Network
Thursday, May 24, 2007


There are two good choices for reading & writing Microsoft Excel Spreadsheet files from Java, in a platform independent way, - jexcelapi and Jakarta POI (HSSF). Both of them provide nice interface to access Excel data structure and even generate new spreadsheet. I have done extensive tests with both of them for a high-profile project for a Fortune 500 company. Previously also I had successfully used HSSF for another high profile client. In the paragraphs below I present my conclusions and sample code for reading Excel spreadsheet from Java using both the libraries.

Comparison of JExcelAPI with Jakarta-POI (HSSF)

1. JExcelAPI is clearly not suitable for important data. It fails to read several files. Even when it reads it fails on cells for unknown reasons. In short JExcelAPI isn’t suitable for enterprise use.

2. HSSF is the POI Project’s pure Java implementation of the Excel ‘97(-2002) file format. It is a mature product and was able to correctly and effortlessly read excel data generated from various sources, including non-MS Excel products like Open Office, and for various versions of Excel. It is very robust and well featured. Highly recommended.

3. Performance was never a consideration in our tests because a) data integrity is the single most important factor and b) there didn’t appear to be any significant performance difference while running the tests; both of them were very fast. We didn’t bother to time it for the above reasons.

How to read Excel Excel Spreadsheet from Java using Jakarta POI (HSSF)

try {
    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row;
    HSSFCell cell;

    int rows; // No of rows
    rows = sheet.getPhysicalNumberOfRows();

    int cols = 0; // No of columns
    int tmp = 0;

    // This trick ensures that we get the data properly even if it doesn't start from first few rows
    for(int i = 0; i < 10 || i < rows; i++) {
        row = sheet.getRow(i);
        if(row != null) {
            tmp = sheet.getRow(i).getPhysicalNumberOfCells();
            if(tmp > cols) cols = tmp;

    for(int r = 0; r < rows; r++) {
        row = sheet.getRow(r);
        if(row != null) {
            for(int c = 0; c < cols; c++) {
                cell = row.getCell((short)c);
                if(cell != null) {
                    // Your code here
} catch(Exception ioe) {

This sample should get you started. Don’t forget to import appropriately.

Gotchas while using Jakarta POI (HSSF)

  • getPhysicalNumberOfRows() returns the physical number of rows which may be more than the actual (logical) number of rows. The same goes for getPhysicalNumberOfCells().
  • You should check for nulls when fetching the HSSFRow and HSSFCell objects as shown.
  • Remember that Excel tables are often sparsely populated. So choose your data structures accordingly.
  • POI accesses the data by sheet. In JExcelAPI you can directly access the data in any row and column.

How to access Excel Spreadsheet using JExcelAPI

File fp = new File(file);
try {
    Workbook wb = Workbook.getWorkbook(fp);
    Sheet sheet = wb.getSheet(0);
    int columns = sheet.getColumns();
    int rows = sheet.getRows();

    String data;
    for(int col = 0;col < columns;col++) {
        for(int row = 0;row < rows;row++) {
            data = sheet.getCell(col, row).getContents();
            // Your code here
} catch(Exception ioe) {
    System.out.println("Error: " + ioe);

Gotchas while using JExcelAPI

  • JExcelAPI may often fail to fetch the data from certain cells or even the whole sheet. Unfortunately it gives a warning instead of an error to indicate the problem.
  • JExcelAPI doesn’t expose the full meta-data of the spreadsheet like POI does.
  • JExcelAPI doesn’t properly recognize the data type in cells. In all cases it indicated String data in our tests even when there were numeric or date fields.

Concluding thoughts on accessing Excel spreadsheets from Java

Both JExcelAPI and Jakarta POI (HSSF) are open source software to read & write data from / to Excel spreadsheet even on non-Microsoft platforms. In my tests HSSF came out to be the clear leader and recommended solution because of robustness and features.

June 8, 2010: 4:00 am

Hi Angsuman and all visitors/readers!

Your post is all about reading Excel spreadsheets and in one of your comments you wrote that you’ve only tested these two solutions on client supplied example files.

You also wrote that “in my tests HSSF came out to be the clear leader and recommended solution”. That’s the point that many readers/commenters obviously missed! Most readers (as I got it from their comments) got to the conclusion that POI is the generally superior to JExcelAPI, which is not the case. POI is known to be a memory hog (even recent versions) and creating XLS files (from scratch) of 20k or more records (with 20-30 columns) can eat up several hundred MB of heap memory. This is not really an option in case of a webapp where several XLS generations can be running in parallel at the same time. JExcelAPI eats a lot less memory.

On the other hand, POI is richer featurewise and maybe more robust.

So the right conclusion is: there’s no clearly “better” choice. Each has it’s strengths and weaknesses. You’ve to consider your goal and pick the right solution accordingly. I’m afraid that there’s no shortcut here: the developer has to test both methods for her/his particular application and see which one suits the particular requirements better.

May 19, 2010: 10:44 am

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename));
HSSFWorkbook wb = new HSSFWorkbook(fs);

can’t be execute in netbeans(and use POI 3.6 20091214)

May 7, 2010: 1:01 am

Hi Frdz,
I am facing problem when i try to get the data from the excel file not able to accept the data from the excel file i am using an automation tool Eclipse to do this if anybody have worked or have solution plz let the know. Thanks in advance

madhu k
April 26, 2010: 9:14 pm

how to take status of employees (30) on excel sheet and update into another main excel sheet.
please send me the results

March 2, 2010: 6:43 am

Pls, how can i add two more interfaces(forms)to my java program that is connected to a Microsoft access database. The program has a form that is retrieving information from the MS Access database but my supervisor wants me to write code that would create a form to display flash/animation about the title of the project on which when clicked leads to the another form that displays password and username which leads to the database retrieval form. Pls help me urgently. Am a student and new learner

February 17, 2010: 6:49 am

iam desining a web site for a company iam stuck up at a point can any one tell me code where i should get details from mdb file where produts updates are stored as soon as the user enter the name of product it shoud get details from mdb and should get displayed as html output pls any one notify me by mail my mail id is

December 22, 2009: 4:15 am

Can we read/write excel sheet using Connection class in sql package
get connect with odbc bridge,similar to access..
is it possible?pls give me some samples
Thank u n advance:-)

December 22, 2009: 4:10 am


December 8, 2009: 8:57 am

I want read and write into another xls file.
Please share me how to write excel.


November 29, 2009: 1:19 pm

Thank you very much for the crisp explanation.

October 24, 2009: 7:03 am

hi iam trying to write excel sheet using jxl
hwo do i write ?

October 10, 2009: 7:18 am

I’ve lot dubts from creating excel sheet from java source code

August 12, 2009: 8:42 am

Sir, can u tell me how to connect Jakarta POI (HSSF)
to java programm and how to buy it

nageswararao buddana
August 8, 2009: 7:31 pm

thanks alot….my work made very easy,really nice way of explanation,null knowledge in java was able to understand the above example

August 6, 2009: 10:26 am

sir,i need to search a given searching value in excel spreadsheet using java.can it b done with this code?

August 5, 2009: 8:05 am

Thanks it’s work fine.

July 20, 2009: 4:05 pm

which classes should be imported for using Jakarta (POI)

April 23, 2009: 1:28 pm

I got the logic for reading and saving of excel to database, but out of 5 rows if I delete 2 rows from excel file, then my logic fails because after reading 3 rows from 4th row onwards the value is coming as null and it is throwing null pointer exception. Can u please give me the reply for this question.
Thanking you,

March 21, 2009: 12:34 pm

Have you used it yourself? How is it superior to the solution provided?

March 18, 2009: 5:04 pm

How to fetch columns from excel sheet with out using DSN

March 13, 2009: 11:17 pm

Check these other open source solutions out:

openxls java spreadsheet SDK:

sheetster programmable web spreadsheet:

March 5, 2009: 1:52 pm

Hi Thanks
I am a leymann in java programing and i need to fetch the specified data from the excel sheet and i need to convert into csv format.
Can you please suggest me which is the best way to achive this.

Thanks in Advance

February 9, 2009: 3:31 pm

How to Link Excel to Java codes?

January 23, 2009: 2:56 am

is POIFSFileSystem located in some .jar file? I need to analyze a chart and write data to an excel document, given each y axes value will be written to a separate sheet. When i tried to run the code above im getting errors for almost all the classes used here.please let me know where can i find all these classes?


ketung hsiao
January 16, 2009: 8:01 pm


I meant to find a web-based spredsheet Java Application that can be installed on one’s own server.
Privacy is the issue.
I know there are many commercial web-based spreadsheet software.

ketung hsiao
January 16, 2009: 7:33 pm


Is there web based Java tool for Excel
similar to POI HSSF ?

( I had a feeling it’s a wishful thinking,
just asking ..)

Thank you in advance.

October 31, 2008: 1:22 pm


I am trying to read and excel file and find some particular string in it. It is happeneing extremely slow and mu application gets timed out. Is there a fast method to Search for a String in excel using POI?

Thanks in advance

October 27, 2008: 10:58 pm

I am trying to read data from excel sheets using jxl but with large size files like more than 7 mb giving out of memory error.Suggestions are most welcome .It is urgent.Please help

June 10, 2008: 3:32 am

hello everybody,

i have a small problem and need your help on it
i need to access my .mpp (Microsoft Project) File
From java…..How can i do that?
knowing that using ready made components is not allowed.

will not be displayed