Sunday, November 5, 2017

Get excel data into data provider by using Apache POI java

Download Apache POI from here: https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-3.17-20170915.zip

package Learn;

import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ReadX
{
@DataProvider(name="data")
public Object[][] data() throws Exception
{
String filepath="C:/Users/Mutturaj.h/Desktop/Me.xlsx";

FileInputStream fis=new FileInputStream(filepath);

XSSFWorkbook wb=new XSSFWorkbook(fis);

XSSFSheet sheet=wb.getSheetAt(0);

Object[][] excelData;

int rows=sheet.getPhysicalNumberOfRows();

int cols=sheet.getRow(0).getLastCellNum();

String data=sheet.getRow(0).getCell(1).getStringCellValue();

System.out.println("Number of rows are: "+rows);

System.out.println("Number of columns are: "+cols);

System.out.println(data);

excelData=new Object[rows][cols];

for(int i=0;i {
for(int j=0;j {
excelData[i][j]=sheet.getRow(i).getCell(j).getStringCellValue();
}
}
return excelData;

}

@Test(dataProvider="data")
public void show(String name,String number){
System.out.println(name);
System.out.println(number);
}


}

No comments:

Post a Comment