使用HSSF从excel读取第三个数据数据

时间:2017-06-27 13:40:13

标签: selenium apache-poi

我正在尝试读取excel数据,excel中存在2个数据,但我的程序正在尝试读取第3个空数据。有人可以帮我这个。下面是我的代码。

public static Object[][] readExcel(String filePath, String sheetName)
        throws IOException {
    String[][] sheetData = null;
    FileInputStream inputStream = new FileInputStream(filePath);
    workBook = new HSSFWorkbook(inputStream);
    sheet = workBook.getSheet(sheetName);
    int k, l;
    int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
    System.out.println(rowCount);
    int readRowCount = sheet.getFirstRowNum();
    Row r = sheet.getRow(1);
    int totalCol = r.getLastCellNum();
    System.out.println(totalCol);
    sheetData = new String[rowCount + 1][totalCol];
    k = 0;
    for (int i = readRowCount + 1; i <= rowCount; i++, k++) {
        l = 0;
        for (int j = 0; j < totalCol; j++, l++) {
            try {
                sheetData[k][l] = getCellData(i, j);
                System.out.println(sheetData[k][l]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return (sheetData);
}

public static String getCellData(int RowNum, int ColNum) throws Exception {
    try {
        Cell = sheet.getRow(RowNum).getCell(ColNum);
        String CellData = Cell.getStringCellValue();
        return CellData;
    } catch (Exception e) {
        return "";
    }
}

enter image description here

0 个答案:

没有答案