[英]Cannot access numeric value of cell from a String cell
我正在嘗試讀取一個 excel 文件並相應地執行我的測試用例,現在我得到的錯誤是java.lang.IllegalStateException:無法從數字單元格中獲取字符串值這是我的代碼實現:您可能會注意到我有用戶 String.valueOf(); 但無濟於事。
public static List<Map<String, String>> getTestDetails()
{
List<Map<String, String>> list = null;
FileInputStream fileInputStream = null;
try
{
fileInputStream = new FileInputStream(FrameworkConstants.getExcelsheetspath()+"TestCasesToBeExecuted.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
XSSFSheet sheet = workbook.getSheet("RunManager");
int lastRowNum = sheet.getLastRowNum();
int lastColNum = sheet.getRow(0).getLastCellNum();
Map<String, String> map = null;
list = new ArrayList<Map<String,String>>();
for(int i=1; i<lastRowNum; i++)
{
map = new HashMap<String, String>();
for(int j=0; j<lastColNum; j++)
{
String key = sheet.getRow(0).getCell(j).getStringCellValue();
System.out.println("Value of key:: "+key);
String value = String.valueOf(sheet.getRow(i).getCell(j).getStringCellValue());
System.out.println("Value of value:: "+value);
map.put(key, value);
}
list.add(map);
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
if(Objects.nonNull(fileInputStream))
{
try
{
fileInputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return list;
}
這是我的方法調用代碼:
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context)
{
List<Map<String, String>> list = ExcelUtils.getTestDetails();
List<IMethodInstance> result = new ArrayList<IMethodInstance>();
for(int i=0; i<methods.size(); i++)
{
for(int j=0; j<list.size(); j++)
{
if(methods.get(i).getMethod().equals(list.get(j).get("Test Name")))
{
if(list.get(j).get("Execute").equalsIgnoreCase("yes"))
{
methods.get(i).getMethod().setDescription(list.get(j).get("Test Description"));
methods.get(i).getMethod().setInvocationCount(Integer.parseInt(list.get(j).get("Count"))); methods.get(i).getMethod().setPriority(Integer.parseInt(list.get(j).get("Priority")));
result.add(methods.get(i));
}
}
}
}
return result;
}
我的導入如下
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
您可以在獲取單元格值之前檢查Cell_Type
。
String value = null;
if (Sheet.getRow(i).getCell(0).getCellType() == Cell.CELL_TYPE_NUMERIC) {
value = NumberToTextConverter.toText(Sheet.getRow(i).getCell(0).getNumericCellValue());
} else
value = Sheet.getRow(i).getCell(0).getStringCellValue();
}
進口:
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.ss.usermodel.Cell;
Maven 依賴項:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
刪除以下導入
import org.apache.poi.ss.usermodel.CellType;
根據文檔https://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFCell.html#getCellType--
你應該能夠做到:
String value = null;
if (sheet.getRow(i).getCell(j).getCellType() == CellType.NUMERIC) {
value=String.valueOf(sheet.getRow(i).getCell(j).getNumericCellValue());
} else{
value = sheet.getRow(i).getCell(j).getStringCellValue();
}
如果單元格可能有 boolean,字符串,我們不知道的數字。 我通常設置類型然后獲取值。
tempCell.setCellType(CellType.STRING);
tempCell.getStringCellValue();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.