简体   繁体   English

无法从字符串单元格访问单元格的数值

[英]Cannot access numeric value of cell from a String cell

I am trying to read an excel file and execute my test case accordingly, now the error I am getting is java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell Here is my implementation of the code: You might notice that I have user String.valueOf();我正在尝试读取一个 excel 文件并相应地执行我的测试用例,现在我得到的错误是java.lang.IllegalStateException:无法从数字单元格中获取字符串值这是我的代码实现:您可能会注意到我有用户 String.valueOf(); but to no avail.但无济于事。


    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; 
    }

And here is my code for method call:这是我的方法调用代码:


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;
}

My imports are as follows我的导入如下

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;

You can have check of Cell_Type before getting the cell value.您可以在获取单元格值之前检查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();
  }

Imports :进口

import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.ss.usermodel.Cell;

Maven dependencies : 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>

Remove the below import删除以下导入

import org.apache.poi.ss.usermodel.CellType;

According to the doc https://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFCell.html#getCellType--根据文档https://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFCell.html#getCellType--

You should be able to do:你应该能够做到:

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();
    }

In case of cell might have boolean, string, number which we don't know.如果单元格可能有 boolean,字符串,我们不知道的数字。 i usually set the type and then get the value.我通常设置类型然后获取值。

tempCell.setCellType(CellType.STRING);
tempCell.getStringCellValue();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM