想要使用selenium从excel读取用户凭据

时间:2016-06-22 13:13:28

标签: selenium-webdriver

我正在尝试从.xlsx excel版本中检索用户凭据。我已经编写了从excel检索数据的代码,但无法将日期插入到appication中。请帮帮我。

FileInputStream stream=new FileInputStream("D:/Ravi-Training/ExcelRead.xlsx");
    XSSFWorkbook wb=new XSSFWorkbook(stream);
    XSSFSheet sheet=wb.getSheetAt(0);
    /*Iterator<Row> rt=sheet.iterator();
    while(rt.hasNext())
    {
        Row row=rt.next();
        Iterator<Cell> ct=row.cellIterator();
        while(ct.hasNext())
        {
            Cell cell=ct.next();
        }*/
    System.out.println(sheet.getSheetName());
    System.out.println(sheet.getLastRowNum());
    XSSFRow row=sheet.getRow(1);
    XSSFCell cell1=row.getCell(0);
    XSSFCell cell2=row.getCell(1);
    XSSFCell cell3=row.getCell(2);
    System.out.println(cell1);
    System.out.println(cell2);
    System.out.println(cell3);

2 个答案:

答案 0 :(得分:0)

请尝试以下代码。

        FileInputStream inputStream = new FileInputStream(new File(filepath));
        XSSFWorkbook wb = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = null;

        if (sheetobj instanceof Integer) {
            sheet = wb.getSheetAt((int) sheetobj);
        }
        if (sheetobj instanceof String) {
            sheet = wb.getSheet((String) sheetobj);
        }

        Iterator<Row> rowIt = sheet.rowIterator();

        Row row = rowIt.next();

        int noOfrow = sheet.getLastRowNum();
        int noOfcolumn = row.getLastCellNum();

        excelValue = new String[noOfrow][noOfcolumn];

        System.out.println("No: of column " + noOfcolumn + " no: of row " + noOfrow);

        wb.close();

希望这会有所帮助。

答案 1 :(得分:0)

    FileInputStream fs=new FileInputStream("Path of excel sheet");
    Workbook wb1=WorkbookFactory.create(fs);
    Sheet sh=wb1.getSheetAt(0);
    Row row=sh.createRow(8);
    //Write data
    Cell cell=row.createCell(8);
    cell.setCellType(Cell.CELL_TYPE_STRING);
    cell.setCellValue("ravi");
    FileOutputStream fos=new FileOutputStream("Path of excel sheet");
    wb1.write(fos);
    fos.close();