Can read data from any row in Excel
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Org.apache.poi.ss.usermodel.Cell;
Import Org.apache.poi.xssf.usermodel.XSSFSheet;
Import Org.apache.poi.xssf.usermodel.XSSFWorkbook;
Import Org.testng.Assert;
Import Org.testng.annotations.DataProvider;
Import Org.testng.annotations.Test;
public class Apachepoidemo {
@Test (dataprovider= "Datadriven")
public void Testadd (double a,double b,double c) {
Assert.assertequals (A+b, C);
//
//}
//
@DataProvider (name= "Datadriven")
Public object[][] Generatedata () throws IOException
{
Return Gettestdata ("data/data3.xlsx");
}
public static object[][] Gettestdata (String filelocation) throws ioexception{
Read file
File File=new file (filelocation);
FileInputStream fis=new fileinputstream (file);
Reading workbooks
Xssfworkbook workbook=new Xssfworkbook (FIS);
Visit Sheet Page
Xssfsheet sheet=workbook.getsheetat (0);
Get the number of columns in a table
int Colcount=sheet.getrow (0). Getphysicalnumberofcells ();
Get the number of rows in a table
int rowcount=sheet.getphysicalnumberofrows ();
Creating a two-dimensional array
Object[][] Ret=new Object[rowcount-1][colcount];
Reading tabular data and assigning values to two-dimensional arrays
for (int i=0;i<rowcount-1;i++) {
for (int j=0;j<colcount;j++) {
Get cell data
Cell Cell=sheet.getrow (i+1). Getcell (j);
Switch (Cell.getcelltype ()) {
Case Cell.cell_type_numeric:
Ret[i][j]=cell.getnumericcellvalue (); break;
Case cell.cell_type_string:
Ret[i][j]=cell.getstringcellvalue (); break;
Default
System.out.println ("Wrong cell type");
}
}
}
Close file stream
Fis.close ();
return ret;
}
}
Selenium code Exercise (APACHEPOI)