When I am passing numeric values into excel sheet, it is taking as double so that there was DataProvider Mismatch exception. How to handle this exception and how to pass only numeric values instead of double values?
Below is my code:
@DataProvider(name = "testdata")
public Object[][] TestDataFeed() throws Exception
{
FileInputStream fis = new
FileInputStream("C:\\Users\\vidhya.r\\Desktop\\Testdata.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sh1 = wb.getSheetAt(2);
int numrow = sh1.getLastRowNum() + 1;
int colnum = sh1.getRow(0).getLastCellNum();
System.out.println("numrow ======= " + numrow);
System.out.println("colnum ======= " + colnum);
Object[][] data = new Object[numrow - 1][colnum];
System.out.println("data.length ======= " + data.length);
for (int i = 1; i < numrow; i++)
{
Row row = sh1.getRow(i);
for (int j = 1; j < row.getLastCellNum(); j++)
{
int cellValue = sh1.getRow(i).getCell(j).getCellType();
if (cellValue == Cell.CELL_TYPE_NUMERIC)
{
Object obj =
sh1.getRow(i).getCell(j).getNumericCellValue();
data[i - 1][j-1] = obj;
System.out.println("obj =num= " + obj);
}
else if (cellValue == Cell.CELL_TYPE_STRING)
{
Object obj =
sh1.getRow(i).getCell(j).getStringCellValue();
data[i - 1][j-1] = obj;
System.out.println("obj =str= " + obj);
}
}
}
return data;
}
Kindly help me to handle this
Here is a workaround to your problem...
import java.io.File;
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.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderTest {
private static FileInputStream fileInputStream;
private static XSSFWorkbook workbook;
private static XSSFSheet sheet;
private static XSSFCell cell;
@DataProvider
public Object[][] dataMethod() {
Object[][] arrayObjects = getExcelData("C:/Eclipse/testdata.xlsx");
return arrayObjects;
}
@Test(dataProvider = "dataMethod")
public void testMethod(String username, String password) {
System.out.println("Username -> "+username+" and Password -> "+username);
}
public String[][] getExcelData(String fileName){
String[][] arrayExcelData = null;
try{
fileInputStream = new FileInputStream(fileName);
workbook = new XSSFWorkbook(fileInputStream);
sheet = workbook.getSheetAt(0);
int totalRows = sheet.getPhysicalNumberOfRows();
int totalCols = sheet.getRow(0).getPhysicalNumberOfCells();
arrayExcelData = new String[totalRows-1][totalCols];
int startRow = 1;
int startCol = 0;
for(int i = startRow; i < totalRows; i++){
for(int j = startCol; j < totalCols; j++){
arrayExcelData[i-1][j] = getCellData(i,j);
}
}
fileInputStream.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return arrayExcelData;
}
public static String getCellData(int rowNum, int colNum){
String cellData = "";
try{
cell = sheet.getRow(rowNum).getCell(colNum);
switch(cell.getCellTypeEnum()) {
case BOOLEAN:
cellData = cell.getRawValue();
break;
case NUMERIC:
cellData = cell.getRawValue();
break;
case STRING:
cellData = cell.getStringCellValue();
break;
default:
break;
}
}catch(Exception e){
e.printStackTrace();
}
return cellData;
}
}