java - 使用 Apache-POI 库获取单元格内容时,我得到了 "Cannot get a numeric value from a text cell"和相反的结果。我如何解决它?

标签 java apache-poi xlsx illegalstateexception

我意识到这个问题有点令人困惑,但我不知道该怎么说。无论如何,这是原始代码:

private void readFile(String excelFileName) throws FileNotFoundException, IOException {
    XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(excelFileName));
    if (workbook.getNumberOfSheets() > 1){
        System.out.println("Please make sure there is only one sheet in the excel workbook.");
    }
    XSSFSheet sheet = workbook.getSheetAt(0);
    int numOfPhysRows = sheet.getPhysicalNumberOfRows();
    XSSFRow row;
    XSSFCell num;
    for(int y = 1;y < numOfPhysRows;y++){    //start at the 2nd row since 1st should be category names
        row = sheet.getRow(y);
        poNum = row.getCell(1);
        item = new Item(Integer.parseInt(poNum.getStringCellValue());
        itemList.add(item);
        y++;
    }
}

private int poiConvertFromStringtoInt(XSSFCell cell){
    int x = Integer.parseInt(Double.toString(cell.getNumericCellValue()));
    return x;
}

我收到以下错误:

Exception in thread "main" java.lang.IllegalStateException: Cannot get a numeric value from a text cell
    at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:781)
    at org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:199)

即使我将其更改为使用 XSSFCell.getStringCellValue() 或什至 XFFSCell.getRichTextValue 获取字符串,我也会得到与上述错误消息相反的信息(并且我确保最终使用 Integer.parseInt(XSSFCell.getStringCellValue()) 将其设为 int。

然后错误显示为:

Exception in thread "main" java.lang.IllegalStateException: Cannot get a text value from a numeric cell
    at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:781)
    at org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:199)

我知道 excel 电子表格列实际上是一个字符串。我无法更改 Excel 工作表,因为它在其他地方始终使用相同的格式并首先格式化每列会占用大量处理时间。

有什么建议吗?

[解决方案] 这是我从@Wivani 的帮助中得出的解决方案代码:

private long poiGetCellValue(XSSFCell cell){
    long x;
    if(cell.getCellType() == 0)
        x = (long)cell.getNumericCellValue();
    else if(cell.getCellType() == 1)
        x = Long.parseLong(cell.getStringCellValue());
    else
        x = -1;
    return x;
}

最佳答案

Use This as reference

switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.println(cell.getRichStringCellValue().getString());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        System.out.println(cell.getDateCellValue());
                    } else {
                        System.out.println(cell.getNumericCellValue());
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.println(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    System.out.println(cell.getCellFormula());
                    break;
                default:
                    System.out.println();
            }

关于java - 使用 Apache-POI 库获取单元格内容时,我得到了 "Cannot get a numeric value from a text cell"和相反的结果。我如何解决它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6508203/

相关文章:

java - 将表示为包含时区名称 ('z' 的字符串的时间转换为 UTC 时间

java - 获取 WiFi 状态并使用广播接收器对其进行操作

java - 使用 java 从 MS Word 文件读取形状(矩形、正方形、圆形、箭头等)、剪贴画

Jboss 5.0 EAP 上的 java.lang.NoClassDefFoundError : Could not initialize class org. apache.poi.POIXMLDocument

java - docx4j/xlsx4j : create simple spreadsheet

java - 使用 Apache POI 库将工作表调整到 xlsx 文件中的单个页面时出现问题

r - 在 xlsx 包的 write.xlsx 中格式化日期

java - Netty 二进制数据直到 0x0a 才会刷新

Java 正则表达式性能 : Reluctant Quantifier or Character Class?

java - 如何使用 Apache Poi 在折线图中设置轴标签