How To Read / Write Excel Spreadsheet From Java
Overview
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) {
ioe.printStackTrace();
}
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);
}
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.
Filed under Enterprise Software, Headline News, How To, J2EE, Java Software, Open Source Software, Programming |
|
RSS 2.0 |
Trackback this Article
|
Email this Article
You may also like to read |





































May 24th, 2007 at 1:32 pm
Thanks,
4 the script.
Peter
May 25th, 2007 at 2:12 am
What do you think of jxls (http://jxls.sourceforge.net) ?
May 26th, 2007 at 5:41 pm
jxls is simply a wrapper on top of poi to make it easier to create complex excel reports. It is not a substitute for poi-hssf or jexcelapi.
May 31st, 2007 at 9:50 pm
please tell me how to create a dropdownlist in excel
with POI. Thanks alot.
June 4th, 2007 at 12:16 pm
Interesting comparison of ExcelAPI with Jakarta-POI. Very useful, thanks for the article.
June 20th, 2007 at 7:23 pm
Sin ningun lugar a dudas poi es un excelente framework para el manejo de hojas de calculo, lo uso desde hace bastante tiempo y nunca tuve mayoer problemas.
July 20th, 2007 at 4:29 am
How did you conclude JExcel API is not suitable for Enterprise?
Can you list the problems faced using JExcel API and how did you overcome them in Jakarta POI?
July 26th, 2007 at 1:11 am
There is another choice.
Jxcell spreadsheet component
August 14th, 2007 at 3:56 pm
En que idioma esta esto
August 21st, 2007 at 8:22 am
plz tell me how i can fetch the data from excel sheet in c code::::::plzzzzz……….
thanks:::::
August 31st, 2007 at 1:42 pm
Did you test POI for newer versions of Excel than 2002? Will POI read an Excel 2005 spreadsheet with just basic data?
September 1st, 2007 at 6:13 am
@Neelam,
The best I can suggest is look into the Excel dll files for their interface. I haven’t read Excel speradsheet in C / C++.
@John,
I have been using it on client supplied Excel files. I don’t know their versions as I use Open Office or Gnumeric to open them on Linux.
October 23rd, 2007 at 7:26 pm
very helpful script! i have another question, though. have you tried reading charts from an excel file? do you have a sample code for that? thank you in advance!
October 25th, 2007 at 6:23 am
I want to know how to append the spreadsheet using java .Please let me know soon . thanks
October 25th, 2007 at 8:31 am
Senthil,
Use Jakarta-POI (HSSF) for that.
October 31st, 2007 at 1:42 pm
You have given example for reading the excel file. Do you have code snippet to write data to excel template with POI?
November 3rd, 2007 at 7:32 am
HI..
i want to automate the scripting,, so i want the to read the data from excel sheet & with that date it has to write it in to the another text files,, so i can i have source code for this single case..
November 13th, 2007 at 6:13 am
Hi..
I need to write data from a microsoft access database onto an excel sheet and any changes or updations in the database should also be reflected on the excel sheet.. Can anyone help??
Same with importing data from excel to database!
Thanks..
November 13th, 2007 at 12:41 pm
You should use Java to transfer data from MS Access to MS Excel.
Use the above tutorial to read / write from MS Excel.
Read the tutorial to read / write from Microsoft Access database using Java.
November 13th, 2007 at 12:43 pm
Erwin said> have you tried reading charts from an excel file?
No.
November 14th, 2007 at 3:40 am
Thanku angs..
I did use ur code to read an excel file and got too exceptions as ArrayIndexOutOfBound and 2 more..
If u can jus help me to write whats in my acess database onto an excel file i’ll be thankful.. Will be glad if I get some code on it!!!
January 18th, 2008 at 2:45 am
thx for the info!
January 24th, 2008 at 3:47 am
I want to write the data in Excel File . (i.e) I want to append the excel file . how can i do that?
The original file\’s soft copy is in one subfolder of my project. I want to overwrite that file . Pls help . I use jxl jar file in my project .
Thanks in Advance
Ganesh
February 4th, 2008 at 1:17 am
I used the same code. In the biginning i have only
1000 rows, 26 columns then it was working properly.
Here the proble cames, now i hav 3000 rows, 29 colomns; at first, my program had worked properly but its taken 1.26 minuts to finish up and i cheacked ie.
taking more time in executing this single line “HSSFWorkbook wb = new HSSFWorkbook(fs);”. and at the next time i got this Exception in thread “main” java.lang.OutOfMemoryError: Java heap space. finally i executed the same code in some other system, again the same problem…
plz help me to solve this problem..
Thanks in Adwnc
Nandu.
February 4th, 2008 at 7:04 am
Thanks Angsuman for this introduction which helped me choose POI.
I had some difficulty understanding the difference between physical and logical rows and cells and I found out that your code is not working when there are empty rows.
You mention in already in your “gotcha’s” but it’s actually the other way around
getPhysicalNumberOfRows() may be LESS than the actual rows (retrieved by getLastRowNum() ).
In your example you mix up the getPhysicalNumberOfRows() and the sheet.getRow(int) which returns the logical row in the sheet which might be a different one if you have empty rows. The same goes for columns.
February 6th, 2008 at 5:41 pm
POI HSSF was not usable for me mainly because it didn’t know how to write out number values with the correct format - so things which were percentages in the spreadsheet like “83%” might come out of POI as “0.834444″ which doesn’t work with the rest of the pipeline.
March 6th, 2008 at 6:43 am
how to read upload Excel sheet in tamil font in java code
I would like to upload Excel sheet in tamil font using java code
help me
April 6th, 2008 at 7:11 am
ur’s mail id please, by which i mail u regardings my problems in programing
April 15th, 2008 at 8:37 am
I have one excel file which is 51MB size. i tried to open using POI but fails. I saved the file using MS Excel with different file name and opened it with POI, it works.
Is it the file problem or with POI?
Please suggest.
April 16th, 2008 at 12:37 pm
I would guess that it is the problem of the spreadsheet.
April 16th, 2008 at 12:40 pm
I have used POI with thousands of complex spreadsheets without any issues at all.
April 18th, 2008 at 4:18 am
I tried POI , with Excel 2003, it’s writing fine , but when I’m trying to open it corrupts the whole Excel file.
So I tried with JXl, here writing & opening is o.k, But it’s not appending new sheet’s. it’s always replaces old sheet with newly created sheet.Please any one can help me.
April 26th, 2008 at 2:02 am
Hi,
Sorry that I am putting question of Jxl in POI forum.
I am getting error “java.lang.OutOfMemoryError: Java heap space” while reading file with 10000 records and 95 columns. size of file is 14M.
I am testing my application through JProfiler.
Is there any restriction of file size or problem while reading throught jxl?
Can anybody help me.
Thanks
May 20th, 2008 at 3:07 pm
what do i import to use the JExcelAPI?
June 1st, 2008 at 11:24 am
but how to read excel sheets carying non english language as arabic ?
June 2nd, 2008 at 12:34 am
Mohamed,
I don’t know. I haven’t tried it.
June 10th, 2008 at 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.