Can not get a text value from a numeric cell "Poi"

3

I'm trying to consume data from an excel spreadsheet, but I always try to format the spreadsheet for text and number, and even then the error persists.

How do you arrange this?

I saw that a person resolved using this cell.setCellType(Cell.CELL_TYPE_STRING); but I do not know where I should fit this snippet into 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();
    
asked by anonymous 08.05.2015 / 15:42

1 answer

3

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

That is:

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

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

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

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

Notice that your source code is strange ... For example, you are reading both the user and the password of the first column (% w / o%) of each row in the worksheet ... Something that does not make much sense users and passwords are the same, if this is the case you would not need to read it twice.)

    
08.05.2015 / 16:18