简体   繁体   English

线程“主”java.lang.IllegalStateException 中的异常:无法从数字单元格中获取文本值

[英]Exception in thread "main" java.lang.IllegalStateException: Cannot get a text value from a numeric cell

I have written code as below to fetch value from Excel.我编写了如下代码来从 Excel 中获取值。

String CID = s1.getRow(i).getCell(0).getStringCellValue();

but in excel, 1st cell is a numeric value, but in above code I am trying to fetch String cell value.但在 excel 中,第一个单元格是一个数值,但在上面的代码中,我试图获取字符串单元格值。 thats why I am getting error as :这就是为什么我收到错误:

Exception in thread "main" java.lang.IllegalStateException: Cannot get a text value from a numeric cell

Can anyone please provide a solution for this.任何人都可以为此提供解决方案。 how to fetch the numeric value from excel?如何从excel中获取数值?

getCellType() for any cell gives you the type of the cell.The types are:任何单元格的 getCellType() 都会为您提供单元格的类型。类型是:

Cell.CELL_TYPE_BLANK
Cell.CELL_TYPE_NUMERIC
Cell.CELL_TYPE_STRING
Cell.CELL_TYPE_FORMULA
Cell.CELL_TYPE_BOOLEAN
Cell.CELL_TYPE_ERROR

It is better to use a switch statement and collect the correct type of cell value.最好使用 switch 语句并收集正确类型的单元格值。 There exists getNumericCellValue() and getStringCellValue() functions but it is safer with types.存在 getNumericCellValue() 和 getStringCellValue() 函数,但使用类型更安全。

我不确定,但我认为存在getNumericalCellValue()getIntegerCellValue()返回你想要的。

Add apostrophe as prefix to that number in cell, automatically it will be converted as text.在单元格中为该数字添加撇号作为前缀,它将自动转换为文本。 it had worked for me它对我有用

Check the cell type first and then get its value.首先检查单元格类型,然后获取其值。

           row.getCell(index).getCellType();
Number ==> row.getCell(index).getNumericCellValue();
String ==> row.getCell(index).getStringCellValue();

你应该使用它=>

String CID = new BigDecimal(s1.getRow(i).getCell(0).getNumericCellValue()).toString();

String unitPrice = new Float(wb.getSheetAt(0).getRow(0).getCell(7).getNumericCellValue()).toString(); String unitPrice = new Float(wb.getSheetAt(0).getRow(0).getCell(7).getNumericCellValue()).toString();

Note: its the best way to get the data in string if the value is stored in the numerical type in excel file.注意:如果值以数字类型存储在excel文件中,它是获取字符串数据的最佳方法。 where we are getting the data from 7th cell of 1st row.我们从第一行的第 7 个单元格获取数据的地方。

final DataFormatter df = new DataFormatter();
final XSSFCell cell = row.getCell(cellIndex);
String valueAsString = df.formatCellValue(cell);

This might help这可能有帮助

Below solution worked for me以下解决方案对我有用

package dummy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.Assert;

public class read_excel {

    public static void main(String[] args) throws Exception {
        String filename = "abP.xlsx";

        try (FileInputStream fis = new FileInputStream(filename)) {
            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            //XSSFWorkbook workbook = new XSSFWorkbook(fis);
            XSSFSheet sheet = workbook.getSheetAt(0);

            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                XSSFRow row = (XSSFRow) rows.next();
                Iterator cells = row.cellIterator();
                while (cells.hasNext()) {
                    XSSFCell cell = (XSSFCell) cells.next();

                    CellType type = cell.getCellTypeEnum();
                    if (type == CellType.STRING) {
                        System.out.println("[" + cell.getRowIndex() + ", "
                            + cell.getColumnIndex() + "] = STRING; Value = "
                            + cell.getRichStringCellValue().toString());
                    } else if (type == CellType.NUMERIC) {
                        System.out.println("[" + cell.getRowIndex() + ", "
                                     + cell.getColumnIndex() + "] = NUMERIC; Value = "
                                      + cell.getRawValue());
                        
                      //  System.out.println("[" + cell.getRowIndex() + ", "
                      //      + cell.getColumnIndex() + "] = NUMERIC; Value = "
                       //     + cell.getNumericCellValue());
                    } else if (type == CellType.BOOLEAN) {
                        System.out.println("[" + cell.getRowIndex() + ", "
                            + cell.getColumnIndex() + "] = BOOLEAN; Value = "
                            + cell.getBooleanCellValue());
                    } else if (type == CellType.BLANK) {
                        System.out.println("[" + cell.getRowIndex() + ", "
                            + cell.getColumnIndex() + "] = BLANK CELL");
                    }
                    else if (type == CellType.FORMULA) {
                        System.out.println("[" + cell.getRowIndex() + ", "
                            + cell.getColumnIndex() + "] = BLANK CELL");
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Please add an apostrophe before number in Excel cell.请在 Excel 单元格中的数字前添加撇号。 That solved my similar problem when using Revit API.这解决了我在使用 Revit API 时遇到的类似问题。 Eg:例如:

'123 '123

暂无
暂无

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

相关问题 java.lang.IllegalStateException:将Excel工作表上传到服务器时,无法从文本单元格异常中获取数值 - java.lang.IllegalStateException: Cannot get a numeric value from a text cell Exception while uploading excel sheet to server java.lang.IllegalStateException:无法从数字公式单元格获取文本值 - java.lang.IllegalStateException: Cannot get a text value from a numeric formula cell java.lang.IllegalStateException:无法从文本单元格获取数值 - java.lang.IllegalStateException: Cannot get a numeric value from a text cell 收到此错误:消息:java.lang.IllegalStateException:无法从数字单元格获取文本值 - Getting this error: Message: java.lang.IllegalStateException: Cannot get a text value from a numeric cell 使用Selenium的线程“main”java.lang.IllegalStateException中的异常 - Exception in thread “main” java.lang.IllegalStateException for using Selenium 线程“main”中的异常 java.lang.IllegalStateException:驱动程序可执行文件是一个目录 - Exception in thread “main” java.lang.IllegalStateException: The driver executable is a directory 线程“main”中的异常 java.lang.IllegalStateException:已连接 - Exception in thread “main” java.lang.IllegalStateException: Already connected spark-submit 在线程“main”中抛出异常 java.lang.IllegalStateException:找不到任何构建目录 - spark-submit throws Exception in thread "main" java.lang.IllegalStateException: Cannot find any build directories Android java.lang.IllegalStateException,不在主线程上 - Android java.lang.IllegalStateException, not on the main thread java.lang.IllegalStateException:不在主线程上 - java.lang.IllegalStateException: Not on the main thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM