【发布时间】:2018-05-30 06:03:42
【问题描述】:
下面是我在 selenium 中使用 excel 驱动的数据的代码。使用此代码,我得到异常 java.lang.NumberFormatException: For input string: "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;
}
}
【问题讨论】:
-
你在哪一行得到这个?如果您尝试将其转换为 int,这很正常,因为 int 和 Integer 都允许使用小数。
-
在您的异常处理程序中添加
e.printStackTrace() -
但老实说,我并没有在这段代码中看到任何我希望抛出这样一个异常的东西。你确定这段代码有问题吗?
-
您确定问题来自这段代码吗?你能提供异常的堆栈跟踪吗?
-
@debugger89;你是对的。问题不在于代码。这通过将值解析为 Double.intValue() 来解决。
标签: java selenium apache-poi data-driven