【问题标题】:unable to get numeric values from excel sheet in selenium webdriver with java无法使用 java 从 selenium webdriver 中的 excel 表中获取数值
【发布时间】:2016-12-24 02:43:35
【问题描述】:

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

但我得到的输出像

Username
password
john

【问题讨论】:

    标签: java excel selenium


    【解决方案1】:

    // create a loop to print cell values 对此发表评论后更改你的 for 循环:

    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((int)row.getCell(j).getNumericCellValue() + " ");
                break;
    
                }
    
    }
    

    开关是识别单元格的类型。对于数字单元格,您必须使用 getNumericCellValue() 而不是 getStringCellValue()

    对于第二个问题,使用System.out.print() 而不是System.out.println(),它用于打印双引号之间的内容并将打印光标移动到下一行。

    编辑:

    这就是我的 readFile() 函数的样子:

    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((int)row.getCell(j).getNumericCellValue() + " ");
                    break;
    
                }
    
            }
    
            System.out.println();
        }
    
    }
    

    编辑 2

    System.out.print(row.getCell(j).getNumericCellValue() + " "); 更改为 System.out.print((int)row.getCell(j).getNumericCellValue() + " "); 以防 Cell.CELL_TYPE_NUMERIC

    【讨论】:

    • 用户名用户名||密码密码||约翰约翰|| 12345.0 得到这样的输出(rambo 无法打印)
    • 我在我的答案中粘贴了我的 readfile 函数。我的输出如下所示:
    • 删除该行:System.out.print(row.getCell(j).getStringCellValue()+"||");
    • 我已经粘贴了我的 readfile 函数在我的答案中的样子
    • 非常接近,数值是123但显示的是123.0
    猜你喜欢
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多