Read excel file data using apache POI

This example shows you how to read an excel file data using Apache POI framework.

In this example we have a excel file that has one row at 4th line and some cell inside that row. Below code will read the 4th cell of 4th row of given excel file.

package com.java.connect.poi;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class POIReadExcelExample {

	public static void main(String[] args) throws Exception {
		// Get the input stream of excel file
		InputStream inputStream = new FileInputStream(
				"POIAddDifferentTypeCellsExample.xls");
		// Create a workbook object.
		Workbook wb = WorkbookFactory.create(inputStream);
		Sheet sheet = wb.getSheetAt(0);
		// You can get the Row one by one if you know which row of Excel file
		// has the data.
		Row row = sheet.getRow(3);
		if (row != null) {
			Cell cell = row.getCell(3);
			if (cell != null) {
				System.out.println(cell.toString());
			}
		}
	}
}