selenium - 为什么最后一个空行已被拉出作为 selenium apache POI 中具有空值的附加测试
问题描述
我在excel中有四行,第一行是标题,其余三行都有值。我以某种方式输入代码以避免标题并读取仅包含值的行。但是,它不是只获取三行,而是带有一个额外的空值行,如下所示,为什么它要获取空值我错过了什么吗?找到代码和错误消息。
信息
通过:testShipment(“孟买”、“纽约”、“18000”、“10000”、“20000”)
通过:testShipment(“孟买”,“科钦”,“2000”,“30000”,“5000”)
通过:testShipment("Cochin", "Farah", "16000", "18000", "19000")
失败:testShipment(空,空,空,空,空)
代码
int TotalCol = sh.getRow(0).getLastCellNum();
int Totalrows = sh.getLastRowNum()+1;
String[][] data = new String[Totalrows][TotalCol];
DataFormatter formatter = new DataFormatter(); // creating formatter using the default locale
for (int i = 1; i < Totalrows; i++) {
Row r = sh.getRow(i);
for (int j = 0; j < TotalCol; j++) {
Cell c = r.getCell(j);
try {
if (c.getCellType() == Cell.CELL_TYPE_STRING) {
String j_username = formatter.formatCellValue(c);
data[i][j] = j_username;
System.out.println("data[i][j]" + data[i][j]);
} else {
data[i][j] = String.valueOf(c.getNumericCellValue());
String j_username = formatter.formatCellValue(c);
data[i][j] = j_username;
System.out.println("data[i][j] numeric val" + data[i][j]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
解决方案
尝试使用以下代码,例如检查空条件
for (int k = 1; k <= totalRows; k++) {
String testCaseID = sheet.getRow(k).getCell(0).getStringCellValue();
if (testCaseID.equalsIgnoreCase(tcID)) {
for (int l = 1; l < totalCols; l++) {
String testData_FieldName = sheet.getRow(0).getCell(l).getStringCellValue();
if (testData_FieldName.equalsIgnoreCase(header)) {
cell = sheet.getRow(k).getCell(l);
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:// numeric value in excel
result = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_STRING: // string value in excel
result = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN: // boolean value in excel
result = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK: // blank value in excel
result = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_ERROR: // Error value in excel
result = cell.getErrorCellValue() + "";
break;
default:
throw new CustomException("The cell data type is invalid");
}
}
}
}
k = totalRows + 1;
}
}
推荐阅读
- python - 使用带有 HTML 模式的 Telegram Messenger API 发送在线照片
- pandas - 如何在连接不同 df 的同时在 pandas 中制作 3 级 multicoloum
- go - GetWindowThreadProcessId 调用返回的 PID 与 TaskManager 的 PID 不匹配
- javascript - 有没有办法让 reactJS 在进入下一行时隐藏文本?
- c# - 使用 COM 互操作将对象从 C# 解决方案的多个项目传递到 Word VBA
- azure-blob-storage - 无法在创建 Blob 时触发 Azure 数据工厂管道
- java - 将 json 数组值反序列化为特定的 Java 类字段
- php - 更新 SQL 变量的问题
- scala - 将 intellij 升级到 2020.1 后,sbt 运行/调试配置损坏显示未知
- ckeditor - 在 CKEditor 5 中获取所有类型的元素