-1

I am pretty new to Selenium and learning through self study. Any help will be appreciable.

Currently I am using

String data = wb.getSheetAt(sheetnum).getRow(row).getCell(col).getStringCellValue();

It is working fine for String but not for numeric. I have a cell value of '500' and I want to fetch it. Please help.

4
  • stackoverflow.com/questions/27688250/… Commented May 28, 2017 at 8:01
  • @Debanjan There is a method defined as follows: public String getData(int sheetnum, int row, int col) { String data=wb.getSheetAt(sheetnum).getRow(row).getCell(col).getStringCellValue(); return data; } The numeric field I want to choose is driver.findElement(By.xpath("//input[@id='Weight']")).sendKeys(excel.getData(1, 1, 9)); Commented May 28, 2017 at 14:52
  • Please let me know of you need any more details. Commented May 28, 2017 at 14:55
  • @PaulamiSanyal Can you consider to put your code block & the entire error stacktrace in the question area for further analysis please? Thanks Commented May 28, 2017 at 17:47

2 Answers 2

0

Try out following code :

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();
    }

}

Hope it will help you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Debanjan and @RNS! Somehow I managed to do it with below code.
Thanks @RNS. Somehow I managed to do it using below code and it worked. public String getNumericData(int sheetnum,int row, int col) { String data=String.valueOf((int)wb.getSheetAt(sheetnum).getRow(row).getCell(col).getNumericCellValue()); return data; }
0
Double data = wb.getSheetAt(sheetnum).getRow(row).getCell(col).getNumericCellValue();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.