无法从selenium webdriver中使用java获取excel表中的数值

时间:2016-08-17 12:27:46

标签: java excel selenium

我的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");
      }

}

通过使用上面的代码,我收到错误&#34;无法从数字单元格获取文本值&#34;。任何帮助?此外,我的输出未正确分配。所有字符串都显示为一个。输出应该像

Username Password
john     123
rambo    456    

但是我得到了像

这样的输出
Username
password
john

1 个答案:

答案 0 :(得分:2)

// 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