以下是我所指的用于在selenium中使用excel进行数据驱动的代码。使用此代码我得到异常java.lang.NumberFormatException:对于输入字符串:“3.0”
public String[][] getDataFromSheet(String excelfilename,String sheetName) {
String datasets[][] = null;
try {
XSSFSheet sheet = workbook.getSheet(sheetName);
int totalRow = sheet.getLastRowNum() + 1;
int totalCol = sheet.getRow(0).getLastCellNum();
datasets = new String[totalRow - 1][totalCol];
for (int i = 1; i < totalRow; i++) {
XSSFRow rows = sheet.getRow(i);
for (int j = 0; j < totalCol; j++) {
XSSFCell cell = rows.getCell(j);
if (cell.getCellTypeEnum() == CellType.STRING) {
datasets[i - 1][j] = cell.getStringCellValue();
}
else if (cell.getCellTypeEnum() == CellType.NUMERIC) {
String cellText = String.valueOf(cell.getNumericCellValue());
datasets[i - 1][j] = cellText;
} else {
datasets[i - 1][j] = String.valueOf(cell.getBooleanCellValue());
}
}
}
return datasets;
} catch (Exception e) {
return datasets;
}
}
答案 0 :(得分:-1)
测试此代码以读取excel文件并根据您的要求进行设置。
public class Test {
public static void main(String[] args) {
try {
//Path Of your File Enter here
InputStream ExcelFileToRead = new FileInputStream("/home/chavdaan01/Desktop/Aniruddh.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(ExcelFileToRead);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row;
XSSFCell cell;
//int break_down=sheet.getPhysicalNumberOfRows();
Iterator rows = sheet.rowIterator();
int i = 0;
while (rows.hasNext())
{
row=(XSSFRow) rows.next();
Iterator cells = row.cellIterator();
int j = 1;
while (cells.hasNext())
{
cell=(XSSFCell) cells.next();
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
System.out.println(cell.getStringCellValue());
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
System.out.println(cell.getNumericCellValue());
}else if(cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
System.out.println(cell.getBooleanCellValue());
}
}
}
} catch (Exception e) {
}
}
}