首页 > 解决方案 > 为什么最后一个空行已被拉出作为 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();
            }
        }
    }
    

标签: seleniumselenium-webdriverapache-poi

解决方案


尝试使用以下代码,例如检查空条件

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;
    }
}

推荐阅读