Reading Excel File Using Java And Apache POI

In this Java Tutorial, I will show you how to read the content of an Excel File. The tutorial will read a sample excel file and then print the content to the console. Making tutorials simpler to understand is one of my objectives in this blog. Hopefully you will find this tutorial extremely simple to understand how to use Apache POI to read any Excel File [.xls format].

I used the following apache POI API and is available for download at the publisher’s website.

  • poi-3.2-FINAL-20081019.jar

 * @author Kushal Paudyal
 * Last Modified on 02/16/2008
package com.kushal.util;

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:\temp\testPOI.xls";
    	//Read an Excel File and Store in a Vector
    	Vector dataHolder=readExcelFile(fileName);
    	//Print the data read
	public static Vector readExcelFile(String fileName)
    	/** --Define a Vector
    	 	--Holds Vectors Of Cells
    	Vector cellVectorHolder = new Vector();

    	/** Creating Input Stream**/
    	//InputStream myInput= ReadExcelFile.class.getResourceAsStream( fileName );
    	FileInputStream myInput = new FileInputStream(fileName);

    	/** Create a POIFSFileSystem object**/
    	POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

    	/** Create a workbook using the File System**/
         HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

         /** Get the first sheet from workbook**/
        HSSFSheet mySheet = myWorkBook.getSheetAt(0);

        /** We now need something to iterate through the cells.**/
          Iterator rowIter = mySheet.rowIterator();

        	  HSSFRow myRow = (HSSFRow);
        	  Iterator cellIter = myRow.cellIterator();
        	  Vector cellStoreVector=new Vector();
        		  HSSFCell myCell = (HSSFCell);
    	}catch (Exception e){e.printStackTrace(); }
    	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();

  1. How to read / write UTF8 and Non-UTF8 files in Java
  2. How To Print A Text File In Java
  3. How to read a tab separated or tab delimited file in Java program and print the content to console
  4. Computing the total, free and usable disk space easily using JDK 1.6
  5. Reading the Resource or Property files in Java as a file stream and outputting the content to console
  6. File Copy From Local Folder or Network Folder Using Java With Capability To Limit The Number
  7. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  8. Reading / Writing File in Java and String Manipulation
  9. How To Read DOC file Using Java and Apache POI
  10. Writing To Excel File Using Apache POI
  11. Reading Excel File Using Java And Apache POI
  12. Calculating Folder Size In Java
  13. How To Rename A File In Java
  14. Calculating Folder Size Graphically and Generating Directory Size Chart In Java
Tagged , , , , , , , . Bookmark the permalink.

One Response to Calculating Folder Size Graphically and Generating Directory Size Chart In Java

  1. nocdib says:

    Thank you for this! It is just what I need

Leave a Reply

Your email address will not be published. Required fields are marked *