【问题标题】:While reading the data from output excel,it shows error in selenium webdriver从输出 excel 读取数据时,在 selenium webdriver 中显示错误
【发布时间】:2025-12-09 02:25:01
【问题描述】:
FileInputStream f2 = new FileInputStream("D:\\Screenshot\\test_grid.xls");  
Workbook CPT_check =Workbook.getWorkbook(f2);
Sheet c1=CPT_check.getSheet(0);
String CPT_check_final =c1.getCell(2,7).getContents();

if(Final_result==CPT_check_final)
    System.out.println("CPT " +Final_result+" is correct");
else
    System.out.println("CPT " +Final_result+" is not correct");

这是我的代码,当我运行它时,它显示错误为

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
    at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:318)
    at login.Login_first.main(Login_first.java:135)

场景是我使用我的代码在 excel 中写入数据,并且我需要从同一个 excel 中读取数据。所以我尝试了上面的代码。它显示错误。请帮我解决这个问题。

【问题讨论】:

  • 看来,2,7 单元格不存在。尝试手动打开您的 xls 文件并检查内容。

标签: java excel selenium


【解决方案1】:

你可以试试下面的代码获取单元格的内容

   FileInputStream f2 = new FileInputStream("D:\\Screenshot\\test_grid.xls");  
   Workbook CPT_check =Workbook.getWorkbook(f2);
   Sheet c1=CPT_check.getSheet(0);

   for(int rows=0;rows<c1.getRows();rows++)
   { 
   for(int cols=0;cols<c1.getColumns();cols++)
   { 
   System.out.println(c1.getCell(cols, rows).getContents().trim()); 
   }
   }

【讨论】: