我的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