Selenium With Java and Python For Mobile Apps & Web Apps......!

Tuesday, 21 April 2015

How to connect Excel Sheet Using Selenium Web driver.

To get data from excel sheet or write data in excel sheet we require some other  api which available  on below link:
Steps :
1. Download apache poi form https://poi.apache.org/download.html link.
2. Unzip and import jar your project.
3.Use given API to connect to excel sheet



package genricLib;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;



import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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 ExcelLib {
String path="C:\\Users\\ADMIN\\Desktop\\EmployeeDetails.xls";
/**
* Get Data From Excel File.....
* @param sheet
* @param rowno
* @param colno
* @return data
*/
public String getData(String sheet,int rowno,int colno) throws InvalidFormatException, IOException {

FileInputStream fis=new FileInputStream(path);
Workbook wb=WorkbookFactory.create(fis);
Sheet sh=wb.getSheet(sheet);
Row rw=sh.getRow(rowno);
String data=rw.getCell(colno).getStringCellValue();


return data;
}
/**
* Save data in Excel Sheet
* @param sheet Name
* @param rowno
* @param colno
* @param data
*/
public void setData(String sheet,int rowno,int colno,String data) throws InvalidFormatException, IOException {

FileInputStream fis=new FileInputStream(path);
Workbook wb=WorkbookFactory.create(fis);
Sheet sh=wb.getSheet(sheet);
Row rw=sh.getRow(rowno);
Cell cel=rw.getCell(colno);
cel.setCellType(cel.CELL_TYPE_STRING);
cel.setCellValue(data);
FileOutputStream fos=new FileOutputStream("C:\\Documents and Settings\\PavanD\\Desktop\\pardeep.xlsx");
wb.write(fos);




}
/**
* To get Total no row in Excel File
* @param sheet
* @return No of Row
*/

public int getRowCount(String sheet) throws InvalidFormatException, IOException
{

FileInputStream fis=new FileInputStream(path);
Workbook wb=WorkbookFactory.create(fis);
Sheet sh=wb.getSheet(sheet);
int data=sh.getLastRowNum();

return data;
}
/**
* To Get Total No Of Column in Row
* @param sheet
* @param rowNo
*/
public int getColomnCount(String sheet,int rowNo) throws InvalidFormatException, IOException
{

FileInputStream fis=new FileInputStream(path);
Workbook wb=WorkbookFactory.create(fis);
Sheet sh=wb.getSheet(sheet);
Row rw=sh.getRow(rowNo);
int data=rw.getLastCellNum();

return data;

}

}

0 comments:

Post a Comment

Translate

Popular Posts

Total Pageviews