我的Java無法從Excel工作表中硒的webdriver數值與Java
public class Readexcel {
public void readfile(String filepath, String filename, String sheetname) throws IOException
{
File file = new File(filepath+"\\"+filename);
FileInputStream fis = new FileInputStream(file);
// for creating .xlsx workbook
Workbook wb = new XSSFWorkbook(fis);
// for reading the sheet by its name
Sheet sh = wb.getSheet(sheetname);
//find the total rows in sheet
int rowcount = sh.getLastRowNum()-sh.getFirstRowNum();
// create a loop to create
for(int i=0;i<rowcount+1;i++)
{
Row row= sh.getRow(i);
// create a loop to print cell values
for(int j=0;j<row.getLastCellNum();j++)
{
Cell cell= row.getCell(j);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(row.getCell(j).getStringCellValue() + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(row.getCell(j).getNumericCellValue() + " ");
break;
}
System.out.print(row.getCell(j).getStringCellValue()+"||");
}
System.out.println();
}
}
public static void main(String...strings) throws IOException
{
Readexcel re = new Readexcel();
String filepath = "F://Excelsheet";
re.readfile(filepath,"Book1.xlsx","Sheet1");
}
}
通過使用上面的代碼中,我得到一個錯誤「無法從數字單元格文本值」。任何幫助?此外我的輸出未正確對齊。所有的字符串都顯示一個一個。輸出應該像
Username Password
john 123
rambo 456
,但我完成後,該// create a loop to print cell values
評論越來越像輸出
Username
password
john
用戶名用戶名||密碼密碼||約翰約翰|| 12345.0得到這樣的輸出(rambo無法打印) – Ab123
我在我的答案中粘貼了我的readfile函數。我的輸出如下所示: – Gelbi
已編輯的代碼請檢查 – Ab123