I want to read an excel and fetch 10 records at a time and concatenate them too using data-provider in selenium web driver and testNG. For example, Excel sheet having two columns i.e, col 1, col 2 and 10 rows. Now I want to pass the fetched result from an excel as value into the text-box using selenium web-driver i.e, "A,B,C,D,E,F,G,H,J,I," as value. Here, row1 col1 contains "A" and row 1 col 2 contains "," . Similarly, it is like this for rest of the rows and cols. Can you please provide me the code for this logic.
Asked
Active
Viewed 6,729 times
2

Barett
- 5,826
- 6
- 51
- 55

Saurabh Gupta
- 107
- 1
- 3
- 10
-
1You can use apache POI library to fetch records. Until and unless have not tried something on your own, we won't be able to help you. you can get blocks of code over google too. try searching. – Mrunal Gosar Jan 18 '15 at 13:58
-
@ Mrunal & Nilesh..I have tried this thing using apache POI library before and I was just thinking if someone has already built the logic for above stated problem but no issues, I have built the logic by myself..Thanks for your response. – Saurabh Gupta Jan 19 '15 at 13:06
2 Answers
0
read and write data from excel
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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 ExcelReader {
public FileInputStream fis = null;
public FileOutputStream fout = null;
private XSSFWorkbook workbook = null;
private XSSFSheet sheet = null;
private XSSFRow row = null;
private XSSFCell cell = null;
String path = null;
public ExcelReader() throws IOException {
path = System.getProperty("user.dir") + "\\testdata\\testdata.xlsx";
fis = new FileInputStream(path);//FileInputStream is used to read excel.
workbook = new XSSFWorkbook(fis);//workbook is excel file include multiple sheet
sheet = workbook.getSheetAt(0);//focusing on particular sheet in workbook.
}
//get number of rows in selected sheet.
public int getSheetRows(String sheetName) {
int index = workbook.getSheetIndex(sheetName);
workbook.getSheetAt(index);
return (sheet.getLastRowNum() + 1);
}
//get number of columns in selected sheet
public int getSheetColumn(String sheetName) {
int index = workbook.getSheetIndex(sheetName);
workbook.getSheetAt(index);
row = sheet.getRow(0);
return (row.getLastCellNum());
}
//get the values in cell by using sheet name and row number and column number
public String getCellData(String sheetName, int rowNum, int colNum) {
int index = workbook.getSheetIndex(sheetName);
sheet = workbook.getSheetAt(index);
row = sheet.getRow(rowNum);
cell = row.getCell(colNum);
return (cell.getStringCellValue());
}
//get the values form cell by using the sheetname and row number and column name
public String getCellData(String sheetName, int rowNum, String ColName) {
int CellNum = -1;
int index = workbook.getSheetIndex(sheetName);
sheet = workbook.getSheetAt(index);
for (int i = 0; i < getSheetColumn(sheetName); i++) {
row = sheet.getRow(0);
cell = row.getCell(i);
if (cell.getStringCellValue().equals(ColName)) {
CellNum = cell.getColumnIndex();
break;
}
}
row = sheet.getRow(rowNum);
cell = row.getCell(CellNum);
return (cell.getStringCellValue());
}
//write the data into sheet.
public void setData(String sheetName, int ColNum, int rowNum, String str) {
int index = workbook.getSheetIndex(sheetName);
sheet = workbook.getSheetAt(index);
row = sheet.getRow(rowNum);
cell = row.createCell(ColNum);
cell.setCellValue(str);
try {
fout = new FileOutputStream(path);
try {
workbook.write(fout);
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
ExcelReader reader = new ExcelReader();
System.out.println(reader.getSheetRows("LoginTest"));
System.out.println(reader.getSheetColumn("LoginTest"));
System.out.println(reader.getSheetRows("SignUPTest"));
System.out.println(reader.getSheetColumn("SignUpTest"));
System.out.println(reader.getCellData("LoginTest", 1, 0));
System.out.println(reader.getCellData("LoginTest", 2, "password"));
reader.setData("LoginTest", 1, 1, "Hello");
}
}

Vinayak
- 439
- 4
- 10