gpt4 book ai didi

java - 无法从数字单元格获取字符串值?

转载 作者:行者123 更新时间:2023-12-02 01:52:52 26 4
gpt4 key购买 nike

我使用关键字驱动数据框架通过 Excel 工作表执行测试脚本。

执行时出现错误:

java.lang.RuntimeException: java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell

我的 Selenium 代码是

    package DemoData;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import DataConfig.ExcelDataConfig;

public class ReadArrayData
{

WebDriver driver;


@Test(dataProvider="Tipreports")
public void Dataprovider(String username, String password) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "F:\\New folder\\chromedriver.exe");

driver = new ChromeDriver();

driver.get("http://xxxxxxxxxxxxxxxx/");

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

driver.manage().window().maximize();

driver.findElement(By.id("txt_username")).sendKeys(username);

driver.findElement(By.id("txt_pin")).sendKeys(password);

driver.findElement(By.xpath("//*[@id='btn_click']")).click();

Thread.sleep(5000);

//driver.getCurrentUrl();

Assert.assertTrue(driver.getCurrentUrl().contains("http://xxxxxxxxxxxxx/Algorithm_Run.aspx"),"User Not able to Login - Invalid Credentials");

}

@AfterMethod
public void Close()
{
driver.quit();
}

@DataProvider(name="Tipreports")
public Object[][] passdata()
{

ExcelDataConfig config = new ExcelDataConfig("E:\\Workspace\\KeywordFramework\\TestData\\Dataprovider.xlsx");
int rows = config.getRowCount(0);

Object[][] data = new Object[rows][2];

for(int i=0;i<rows;i++)
{
data[i][0]=config.getData(0, i, 0);
data[i][1]=config.getData(0, i, 1);

}
return data;

}

}

这是我的 Excel 数据配置代码:

package DataConfig;

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelDataConfig
{
XSSFWorkbook wb;
XSSFSheet Sheet1;

public ExcelDataConfig(String excelpath)
{
try
{
File src = new File(excelpath);
FileInputStream fis = new FileInputStream(src);
wb= new XSSFWorkbook(fis);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}

public String getData(int sheetnumber, int row, int column)
{
Sheet1 = wb.getSheetAt(sheetnumber);

String data =Sheet1.getRow(row).getCell(column).getStringCellValue();

return data;

}

public int getRowCount(int sheetindex)
{
int row = wb.getSheetAt(sheetindex).getLastRowNum();

row = row+1;

return row;
}

}

您能帮我解决这个问题吗?为什么会出现此错误?我已经使用 for 循环声明了2 Columns and 4 Rows。但我收到无法获取错误,因为无法从 Numeic Cell 获取字符串值。

最佳答案

试试这个:

String data;

if(cell.getCellType()==CellType.STRING)
data = cell.getStringCellValue();
else if(cell.getCellType()==CellType.NUMERIC)
data = String.valueOf(cell.getNumericCellValue());
else ...

这是以文本形式检索单元格内容的更好方法。

感谢@AxelRichter,该手册显示了一个更完整的示例来检索单元格内容:https://poi.apache.org/components/spreadsheet/quick-guide.html#CellContents

关于java - 无法从数字单元格获取字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52701398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com