Read data from Excel Sheet using Java Code

In this post lets write code to read data from excel sheet

my excel sheet looks like below







Output:









Jars Required:






Java code:

package com.ghr;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelBook {

public static void main(String[] args) {

//file path and sheetname
ArrayList<HashMap<String,String>> data = readExcelSheet("C:\\Users\\ravi.kumar.honnappa\\Desktop\\IdM AMS Monitoring Code\\myExcelFile.xlsx","userInfo");
System.out.println("Data in the Excel sheet is: "+data);

}

public static ArrayList<HashMap<String,String>> readExcelSheet(String filePath, String sheetName){

ArrayList<HashMap<String,String>> excelData = new ArrayList<HashMap<String,String>>();


InputStream ExcelFileToRead = null;
try {
ExcelFileToRead = new FileInputStream(filePath);
} catch (Exception e) {
e.printStackTrace();
}

XSSFWorkbook wb = null;
try {
wb = new XSSFWorkbook(ExcelFileToRead);
} catch (Exception e) {
e.printStackTrace();
}

XSSFSheet sheet = wb.getSheet(sheetName);
int rowsCount = sheet.getPhysicalNumberOfRows();
System.out.println("Number of rows in the sheet: "+rowsCount);
int numberofColumns = 2;

//iterate rows
for(int k=0;k<rowsCount;k++){

HashMap<String,String> map = null;
//iterate columns
for(int j=0;j<numberofColumns;j++){


if (sheet.getRow(k) != null){
map =  new HashMap<String,String>();
String cellValue = sheet.getRow(k).getCell(j).toString();
System.out.println("Row: "+k+", Column: "+j+" : "+cellValue);
map.put("Column"+j, cellValue);
excelData.add(map);
}


}

}

return  excelData;


}

}

Comments

Post a Comment

Popular posts from this blog

Rules in Sailpoint

Sailpoint Installation Steps (IdentityIQ 7.3 Installation in Windows Machine)

How to create provisioning plan in Sailpoint