Now we will access the cell in a rows and column to control our Data driven tests
Code
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
public class dataDriven {
public static void main(String args[]) throws IOException {
FileInputStream fis = new FileInputStream("D://Apache_poi_data_01.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
int sheet = workbook.getNumberOfSheets();
for (int i = 0; i<sheet;i++) {
if(workbook.getSheetName(i).equalsIgnoreCase("Sheet1")) {
XSSFSheet sheets = workbook.getSheetAt(i);
Iterator<Row> rows = sheets.iterator();
Row firstrow = rows.next();
Iterator<Cell> ce = firstrow.cellIterator();
int k = 0;
int column = 0;
while(ce.hasNext())
{
Cell value =ce.next();
if(value.getStringCellValue().equalsIgnoreCase("Data3"));
{
column = k;
}
k++;
}
System.out.print(column);
}
}
}
}
Explanation