How to read data from excel cell using Apache POI in java

In this post, I’m going to show you how to get data from a single cell in a spreadsheet. I build a customized method which fetches data from a cell based on the Column Index number, Row Number, Sheet Number and Path of the excel sheet.

 Let’s see how to do it

Create a new project in Eclipse and name it. Create a new main Class under this project and give a suitable name to it. Add apache POI jars to the current project. If you don’t know how to include Apache POI jars the follow the below steps:

 Add Apache POI jars to project

 1) Go to the URL as http://poi.apache.org/download.html

2) Click on the “poi-bin-<version and date>..zip” or poi-bin-3.14-<Version and date>.tar.gz(Choose this only if you have gz extractor) and download the file

3) Extract the zip file

4) Go to Eclipse and select the project displayed in the Package Explorer.

5) Right, click the Project name and select Build Path->Configure Build Path. Now you can see Java Build path dialog box

6) Go to the Libraries tab and select “Add External Jars”. Open file dialog box will be displayed

7) Navigate to the Apache POI extracted folder and add all the jar files inside that folders and subfolders. Click Ok

I’ve created a method with four parameters public Cell getvalue(int colno,int rowno,int shtno,String path) throws IOException and return type as Cell.

 int colno– Give the column number where your cell is present

int rowno– Give the row number where your Cell is present

int shtno- Give the position of the sheet (0,1,2) not the sheet name

String path– Give the location of your Excel workbook

Note: Be aware that apache POI indexed by zero which means the first row starts with zero, the first column starts with zero and the first sheet starts with zero.

Example

Let see how to get the data “I’m here”(shown below) present in sheet 1, first row, second column and exist in the path “D:testtest.xlsx”

public static void main(String[] args) throws IOException

 {

         writeexcel t1=new writeexcel();

         String path=”D:testtest.xlsx”;

         System.out.println(t1.getvalue(2,1, 0, path));

  }

Source Code

One Comment

Let me know your thoughts

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.