How to Read Excel file Using Java

In this section, you will learn how to read excel file data and display on the console.


Share on Google+Share on Google+

How to Read Excel file Using Java


In this section, you will learn how to read excel file data and display on the console.

How to Read Excel file

In this section,you will learn how to read excel file data and display on the console.We have used Jakarta POI api for this purpose.With the use of HSSFRow and HSSFCell, the Iterator class iterates every row and every cell and stored the excel file values into the vector.This Vector data is then used to display file values on the console.

Here is the code:

import java.util.Iterator;
import java.util.Vector;

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;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ReadExcelFile {

	public static void main(String[] args) {

		String fileName = "C:\\excelFile.xls";
		Vector dataHolder = ReadCSV(fileName);

	public static Vector ReadCSV(String fileName) {
		Vector cellVectorHolder = new Vector();

		try {
			FileInputStream myInput = new FileInputStream(fileName);

			POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

			HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

			HSSFSheet mySheet = myWorkBook.getSheetAt(0);

			Iterator rowIter = mySheet.rowIterator();

			while (rowIter.hasNext()) {
				HSSFRow myRow = (HSSFRow);
				Iterator cellIter = myRow.cellIterator();
				Vector cellStoreVector = new Vector();
				while (cellIter.hasNext()) {
					HSSFCell myCell = (HSSFCell);
		} catch (Exception e) {
		return cellVectorHolder;

	private static void printCellDataToConsole(Vector dataHolder) {

		for (int i = 0; i < dataHolder.size(); i++) {
			Vector cellStoreVector = (Vector) dataHolder.elementAt(i);
			for (int j = 0; j < cellStoreVector.size(); j++) {
				HSSFCell myCell = (HSSFCell) cellStoreVector.elementAt(j);
				String stringCellValue = myCell.toString();
				System.out.print(stringCellValue + "\t");


Posted on: November 2, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles