Tuesday, March 6, 2012

Read Excel and Store Data in Array..

During my Selenium learning period i spend 1 day to get this simple code, i am sharing this with everyone. Simple code for Data Driven Automation which can be used to read each sheet and store in Separate Array which can be accessed anywhere in your Java program. I used POI for this...


1 - From my main class i am calling these lines...
HashMap sheetValues = new HashMap();
InputData.ReadSheet("C:\\TestData.xls","Driver"); // Driver is my sheet name
sheetValues.put("Driver", InputData.getValueArray()); //Read the data here

String driverValueArray[][] = sheetValues.get("Driver");




2 - Class where Excel file i am reading..
package temp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.*;
//import org.apache.poi.xssf.usermodel.XSSFCell;
//import org.apache.poi.xssf.usermodel.XSSFRow;
//import org.apache.poi.xssf.usermodel.XSSFSheet;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class InputData {

public static String valueArray[][] = null;
/**
* @throws Exception
*/
public static void ReadSheet(String filename, String sheetName) throws Exception
{
FileInputStream fis = null;
try {
fis = new FileInputStream(filename);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheet(sheetName);
int LastRow=sheet.getLastRowNum();
XSSFRow temprow = sheet.getRow(0);
int lastcol = temprow.getLastCellNum();
valueArray = new String[LastRow+1][lastcol];
Iterator rows = sheet.rowIterator();
int i = 0;
while (rows.hasNext())
{
XSSFRow row = ((XSSFRow) rows.next());
Iterator cells = row.cellIterator();
int j =0;
while(cells.hasNext())
{
XSSFCell cell = (XSSFCell) cells.next();
int type = cell.getCellType();
String value = "";
if(type == Cell.CELL_TYPE_STRING) {
value = cell.getStringCellValue();
} else if(type == Cell.CELL_TYPE_NUMERIC) {
//value = new Double(cell.getNumericCellValue()).toString();
value = cell.getRawValue().toString();
} /*else if(type == Cell.CELL_TYPE_STRING) {
value = new Double(cell.getNumericCellValue()).toString();
//value = cell.getRawValue();
} */
valueArray[i][j] = value;
j++;
}
i++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}
}
public static void main(String[] args) {
InputData object=new InputData();
try{
ReadSheet("","");
}catch(Exception e)
{
e.printStackTrace();
}
}


public static String[][] getValueArray() {
return valueArray;
}


public static void setValueArray(String[][] valueArray) {
InputData.valueArray = valueArray;
}
}


Sunday, May 8, 2011

Join Gifts to Grow now and get 100 Free Points.

From knowing exactly when they want teddy to comforting them at 3am, the list of love is endless. Gifts to Grow gives parents the rewards they deserve, simply by buying Pampers diapers and wipes. Join now and get 100 Free Points.

Sunday, August 15, 2010

वृद्ध आश्रम निधी

मित्रानो,
तुम्ही एमैल मधली सुब्जेक्त लाईन वाचूनच या एमैल पासून दूर राहण्याचा विचार केला असणार असो तुम्ही हे वाचलेत ते पण पुष्कळ आहे.
चला मी प्रमुख मुद्यावर येतो, आपण आपले आयुष्य जगताना आपल्या म्हातारपणा साठी पण साविन्ग्स करतो. काही जन विचार करता कि त्याची मुले त्यांना सांभाळतील पण विचार करा आश्या लोकांचा जे त्याच्या आयुष्यात न सेविंग करू शकले ना त्यांना कोणी सांभाळणारे आहे. खूप जन आसे आहेत कि त्यांना मुले असून सुदा काही फायदा नाही लग्न नंतर ती मुले त्यांना वारया वर सोडून देता. मी अशाच काही वृद्ध लोकांच्या मदती साठी प्रयतन करतोय आणि जर तुमचा यात सहबाघ झाला तर या सरावाच्या चेहऱ्यावरील इक हास्य भागून आपण सुखी होऊ शकतो. मुंबई पुणे इथे अशी खूप वृद्ध आश्रम आहेत आणि पुष्कळ लोक त्याला मदत करता, पण मी आशे वृद्ध आश्रम बगीतले जिते सर्व बांधकाम झाले पण पैस्या आभावी सुविधा नाहीत. हे वृद्ध आश्रम विदर्भातील खामगाव इथे आहे, मी कॉल्लेगे माडे असताना मी ते पहिले होते आणि निर्णय केला होता कि मी त्यांना जरूर मदत करेन, आता वेळ आली आहे आणि मी माझा वाट उचलतोय, आणि त्यासोबत मी आजून काही मदत जमेल तर ती पण करायचे ठरवतोय. सुरवात म्हुअनुन मी आश्रम ला २५००० देणार आहे, आशा करतो तुमच्या कडून मला काही मदत मिळेल जी मी या लोकां साठी वापरू शकेल. मी हे आश्रम ३ वर्ष झाले बघितले नाही आणि मला माहित पण नाही ते सुरु आहे कि बंद पडले पण मी विचारपूस करून तुम्हा सर्वाना कळवेन, हे आश्रम जरी सुरु नसले तरी ती रकम कुठल्या तरी आशा संस्थेला द्यायाजी आहे ज्यांना सहज देणग्या देणारे मिळत नाहीत.
तुमचे काही विचार असतील तर ते जरूर कळवा, जर तुम्हाला कुठले आसे ठिकाण माहिती आसेअल ज्यांना मदतीची आवश्यकता आहे आपण त्यांना मदत करू, पण मित्रानो आपण आपल्या कमी मधून काही तरी या लोकां साठी करू. आज १५ आगुस्त आहे आणि या दिवशी आपण आपल्या देश साठी काही तरी प्रण घेवू.

आपला
अक्षय अशोकराव रुद्राक्ष
+१-९२५-३५३-८२१०