How To Read / Write Excel Spreadsheet From Java
By Angsuman Chakraborty, Gaea News NetworkThursday, May 24, 2007
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.
Tags: Cases, Jakarta, Open office, Open Source, Warning
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. |
oskdin |
May 19, 2010: 10:44 am
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename)); can’t be execute in netbeans(and use POI 3.6 20091214) |
$Dilip |
May 7, 2010: 1:01 am
Hi Frdz, |
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. |
Toyin |
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 |
sandeep |
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 sandeep_reddy@indiatimes.com |
vinu |
December 22, 2009: 4:15 am
hai, |
Vinu |
December 22, 2009: 4:10 am
HAI, |
ajay234 |
December 8, 2009: 8:57 am
I want read and write into another xls file. Thanks, |
Rakesh |
Ram |
sathihkumar.G. |
Nitin |
August 12, 2009: 8:42 am
Sir, can u tell me how to connect Jakarta POI (HSSF) |
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 |
sonal |
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? |
Nitin |
Balagangadhar |
April 23, 2009: 1:28 pm
Hi, |
saravanan |
March 13, 2009: 11:17 pm
Check these other open source solutions out: openxls java spreadsheet SDK: sheetster programmable web spreadsheet: |
Rahul |
March 5, 2009: 1:52 pm
Hi Thanks Thanks in Advance |
Manasa |
Teena |
January 23, 2009: 2:56 am
Hi, Thanks |
ketung hsiao |
January 16, 2009: 8:01 pm
Hi, I meant to find a web-based spredsheet Java Application that can be installed on one’s own server. |
ketung hsiao |
January 16, 2009: 7:33 pm
Hi, Is there web based Java tool for Excel ( I had a feeling it’s a wishful thinking, Thank you in advance. |
Parul |
October 31, 2008: 1:22 pm
Hi, 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 |
rama |
October 27, 2008: 10:58 pm
Hi, |
Samer |
June 10, 2008: 3:32 am
hello everybody, i have a small problem and need your help on it |
müzso