当我的Excel工作表包含具有动态列数的数据时,我想获取行的最后一个列号。
例如第1行包含2列的数据,第2行包含10列的数据,第3行包含5列的数据。

FileInputStream fis;
    fis = new FileInputStream(Excel_FilePath);
    XSSFWorkbook workbk=new XSSFWorkbook(fis);
    //readExcel.setExcelFile(Property.getProperty("TestData_SheetName"));
    XSSFSheet actionsheet=workbk.getSheet(Property.getProperty("TestData_SheetName"));
    readExcel.setExcelFile(Property.getProperty("TestData_SheetName"));
    System.out.println("The current excel sheet name is : "+Property.getProperty("TestData_SheetName"));
    System.out.println("The Test case ID inside the getColumnCount method is : "+DriverExecution.MainTestScenarioID);
    CellRangeAddress rowscount = readExcel.mergedrowscount(DriverExecution.MainTestScenarioID);

    int Frow=rowscount.getFirstRow();
    int Lrow=rowscount.getLastRow();

    System.out.println("The First Row number is : "+Frow);
    System.out.println("The Last Row number is : "+Lrow);
    //colcnt = actionsheet.getRow(Lrow).getPhysicalNumberOfCells();
    colcnt = actionsheet.getRow(Lrow).getLastCellNum();
    System.out.println("Number of columns is : "+colcnt);

最佳答案

FileInputStream excelFile = new FileInputStream(new File(Excel_FilePath));
Workbook excelWorkbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = excelWorkbook.getSheetAt(0); //This will get you first sheet     from workbook
Iterator<Row> iterator = datatypeSheet.iterator();
Row firstRow = iterator.next();
while (iterator.hasNext()) {
    Row currentRow = iterator.next();
    int currentRowNumberOfColumns = currentRow.getPhysicalNumberOfCells();
}

10-08 13:24