gpt4 book ai didi

java - 使用 Java 和 Apache POI 从 Excel 读取整数数据时避免小数点

转载 作者:行者123 更新时间:2023-12-02 03:55:07 25 4
gpt4 key购买 nike

我正在尝试用 Java 开发一个数据驱动的 Selenium 自动化框架。我已经编写了代码来从 Excel 工作表读取输入测试数据。 Excel 工作表包含两列 - 用户名和密码。我使用以下代码读取Excel数据。

String testData;
for(int j=1;j<currentRow.getPhysicalNumberOfCells();j++){
if(currentRow.getCell(j)!==null)
{
if(currentRow.getCell(j).getCellType()==Cell.CELL_TYPE_BOOLEAN){
testData=Boolean.toString(currentRow.getCell(j).getBooleanCellValue());
}
if(currentRow.getCell(j).getCellType()==Cell.CELL_TYPE_NUMERIC){
testData=Double.toString(currentRow.getCell(j).getNumericCellValue());
}
if(currentRow.getCell(j).getCellType()==Cell.CELL_TYPE_STRING){
testData=currentRow.getCell(j).getStringValue();
}
}

}

问题是,如果密码是 123,上面的代码将返回值 123.0,因此测试用例失败。我无法删除小数点,因为如果实际密码是 123.0,它将返回结果 123。如何读取单元格中给出的 Excel 数据?

最佳答案

在开始读取之前添加cell.setCellType(Cell.CELL_TYPE_STRING);

编辑:POI API Documentation说,

If what you want to do is get a String value for your numeric cell, stop!. This is not the way to do it. Instead, for fetching the string value of a numeric or boolean or date cell, use DataFormatter instead.

引用This answer for a better solution!

关于java - 使用 Java 和 Apache POI 从 Excel 读取整数数据时避免小数点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35569225/

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