Share on Google+Share on Google+

Read Simple Excel(.xls) document using Apache POI

In this section, you will learn how to read Excel file having .xls extension using Apache POI library.

Read Simple Excel(.xls) document using Apache POI

In this section, you will learn how to read Excel file having .xls extension using Apache POI library.

In the below example, we will read excel document having one sheet named as "new sheet" which have values on the first row. The row contains numeric, boolean  as well as text cells.

In the below example, i have used Apache POI version 3.7. For downloading the above library click here.

The code is given below :

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class SimpleExcelReadExample {
public static void main(String[] args) throws Exception {
// An excel file name. You can create a file name with a full
// path information.
String filename = "xls/test.xls";

// Create an ArrayList to store the data read from excel sheet.
List sheetData = new ArrayList();

FileInputStream fis = null;
try {
// Create a FileInputStream that will be use to read the
// excel file.
fis = new FileInputStream(filename);

// Create an excel workbook from the file system.
HSSFWorkbook workbook = new HSSFWorkbook(fis);
// Get the first sheet on the workbook.
HSSFSheet sheet = workbook.getSheetAt(0);

// When we have a sheet object in hand we can iterator on
// each sheet's rows and on each row's cells. We store the
// data read on an ArrayList so that we can printed the
// content of the excel to the console.
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow);
Iterator cells = row.cellIterator();

List data = new ArrayList();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell);

} catch (IOException e) {
} finally {
if (fis != null) {


private static void showExelData(List sheetData) {
// Iterates the data and print it out to the console.
for (int i = 0; i < sheetData.size(); i++) {
List list = (List) sheetData.get(i);
for (int j = 0; j < list.size(); j++) {
Cell cell = (Cell) list.get(j);
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
if (j < list.size() - 1) {
System.out.print(", ");


This will show the content of the file "test.xls" as follows :

EmpId, Name, Designation
1.0, ABC , Software Engineer
2.0, DFG , Sr Software Engineer
3.0, LOI , Team Leader
4.0, LKJ , Project Manager                                         

Download Source Code



Posted on: August 9, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Read Simple Excel(.xls) document using Apache POI  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
K.Irsath Ahamed
April 24, 2012
GWT with Excel

hi , i want to know how to fetch the empty cell data from xls sheet into GWt Application? if u knw send ans to me