Cannot get a text value from a Numeric Cell "Poi"

Asked

Viewed 695 times

3

I am trying to consume data from a spreadsheet in excel, but always gives this error, already tried to format the spreadsheet for text and for number and even then the error persists.

How do you fix it?

I saw that one person solved using this cell.setCellType(Cell.CELL_TYPE_STRING); but I don’t know where I should fit this bit in my code.

WebElement searchbox = driver.findElement(By.name("j_username"));
    WebElement searchbox2 = driver.findElement(By.name("j_password"));         




    try {

          FileInputStream file = new FileInputStream(new File("C:\\paulo.xls")); 
          HSSFWorkbook workbook = new HSSFWorkbook(file);

          HSSFSheet sheet = workbook.getSheetAt(0);

        for (int i=1; i <= sheet.getLastRowNum(); i++){

                String j_username = sheet.getRow(i).getCell(0).getStringCellValue();
                String j_password = sheet.getRow(i).getCell(0).getStringCellValue();


                searchbox.sendKeys(j_username);
                searchbox2.sendKeys(j_password);


                searchbox.submit();       

                driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);

        }

          workbook.close();
          file.close();

         } catch (FileNotFoundException fnfe) {
          fnfe.printStackTrace();
         } catch (IOException ioe) {
          ioe.printStackTrace();

1 answer

3


You are getting an instance of the type Cell in sheet.getRow(i).getCell(0).

That is to say:

Cell cell = sheet.getRow(linhaDaCelula).getCell(colunaDaCelula);

Cells may be of the type CELL_TYPE_BLANK, CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR, CELL_TYPE_FORMULA, CELL_TYPE_NUMERIC or CELL_TYPE_STRING.

You can get cell type with the method getCellStyle() and modify it with the method setCellType().

Depending on the type of your cell you should use the correct methods to obtain its value. They are: getDateCellValue(), getErrorCellValue(), getNumericCellValue(), getRichStringCellValue() and getStringCellValue().


Note that your source code is strange... For example, you are reading both the user and password in the first column (getCell(0)) of each line in the spreadsheet... Something that doesn’t make much sense (it would mean that users and passwords are the same, if that’s the case you wouldn’t need to read twice).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.