-1

I have data in Excel but when I run the code output in decimal but my excel cell format in Text

1.23456789E8 
28.5

Also, how do I select radio button base on test data from excel sheet means select female button if cell data is female.

Also how to select data from the drop-down menu. Attached image with Link for Excel file

//ReadExcelShe

package excelpoi;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelShe {
    public Sheet readExcel(String filePath,String fileName,String sheetName) throws IOException {
        //Create a object of File class to open xlsx file
        File file = new File(filePath+"\\"+fileName);

        //Create an object of FileInputStream class to read excel file
        FileInputStream inputStream = new FileInputStream(file);
        Workbook wb = new XSSFWorkbook(inputStream);

        Sheet  she = wb.getSheet(sheetName);
        return she; 
    }
}

// googletestppo

package testpoi;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.apache.poi.sl.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import excelpoi.ReadExcelShe;

public class googletestppo {
    // TODO Auto-generated method stub

    WebDriver driver;
    @Test(dataProvider = "testdata")

    public void login(String firstn, String lastn, String usern, String passw, String regusern, String password, String month, String day, String year) throws InterruptedException {
        driver = new ChromeDriver();
        driver.get("http://facebook.com/");
        driver.findElement(By.name("firstname")).sendKeys(firstn); 
        ///amidfdsds
        driver.findElement(By.name("firstname")).sendKeys(lastn); 
        ///sfasfasbad
        driver.findElement(By.name("reg_email__")).sendKeys(usern);
        //dsasfzxv@gmail.com
        driver.findElement(By.name("reg_email_confirmation__")).sendKeys(regusern);
        //dsasfzxv@gmail.com
        driver.findElement(By.name("reg_passwd__")).sendKeys(password);
        //1234567890  // this output come in decimal  
        WebElement maleRadioBtn = driver.findElement(By.id("u_0_a"));
        maleRadioBtn.click();

        WebElement femaleRadioBtn = driver.findElement(By.id("u_0_9"));
        femaleRadioBtn.click();

        Select dropdown = new Select(driver.findElement(By.id("month")));
        dropdown.selectByVisibleText(month);
        //Oct
        Select dropdown1 = new Select(driver.findElement(By.id("day")));
        dropdown1.selectByVisibleText(day);
        //29 this come in decimal 
        Select dropdown2 = new Select(driver.findElement(By.id("year")));
        dropdown2.selectByVisibleText(year);
        // 1985 this come in decimal 
        driver.findElement(By.name("websubmit")).click();
    }

    @DataProvider(name = "testdata")

    public String[][] readExcel() throws IOException {
        ReadExcelShe file = new ReadExcelShe();
        org.apache.poi.ss.usermodel.Sheet she = file.readExcel(System.getProperty("user.dir") + "\\", "DDF.xlsx", "Sheet2");

        int rowCount = she.getLastRowNum();
        int columnCount = she.getRow(0).getPhysicalNumberOfCells();

        String[][] inputdata = new String[rowCount][columnCount];
        for (int i = 0; i < rowCount; i++) {
            Row row = she.getRow(i + 1);
            for (int j = 0; j < columnCount; j++) {
                inputdata[i][j] = row.getCell(j).toString();
                System.out.println(inputdata[i][j]);
            }
        }
        return inputdata;
    }
}

enter image description here

Nic3500
  • 8,144
  • 10
  • 29
  • 40
  • `row.getCell(j).toString()`: Simply calling the `XSSFCell`'s `toString` method is not the proper way to get the cell values. Apache POI's own documentation [Busy Developers' Guide to HSSF and XSSF Features](https://poi.apache.org/components/spreadsheet/quick-guide.html) shows how to do in [Getting the cell contents](https://poi.apache.org/components/spreadsheet/quick-guide.html#CellContents). So `... DataFormatter formatter = new DataFormatter(); ... Cell cell = row.getCell(j); inputdata[i][j] = formatter.formatCellValue(cell); ...` – Axel Richter Oct 16 '18 at 03:23
  • you can refere more at – Ashish Kamble Oct 16 '18 at 06:22
  • It looks like you are asking 3 different questions here. You need to pick one for this particular question and remove the others. We can't answer more than one question at a time. – JeffC Oct 16 '18 at 21:33

1 Answers1

0

I have data in Excel but when I run the code output in decimal but my excel cell format in >Text

1.23456789E8

28.5

See following post Always show two decimal points in excel cells using Apache poi and official document: Busy Developers' Guide to HSSF and XSSF Features

You can indicate decimal format with DataFormat class. If you want to specify decimal format, then input set format as "#,##0.0000". Else please input format as "text".

Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("format sheet");
CellStyle style;
DataFormat format = wb.createDataFormat();
Row row;
Cell cell;
int rowNum = 0;
int colNum = 0;

row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("0.0"));
cell.setCellStyle(style);

row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#,##0.0000"));
cell.setCellStyle(style);

try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
    wb.write(fileOut);
}

wb.close();

For Excel drop down list, see the post Excel Drop down list using Apache POI

hiropon
  • 1,675
  • 2
  • 18
  • 41