当使用Apache-POI库获取单元格内容时,我得到"无法从文本单元格中获取数值",反之亦然.我如何解决它?

crs*_*ps2 22 java xlsx apache-poi 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;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

即使我更改它以获取字符串使用XSSFCell.getStringCellValue()或甚至XFFSCell.getRichTextValue,我得到上述错误消息的反向(我确保最终使它成为一个int使用Integer.parseInt(XSSFCell.getStringCellValue()).

然后错误如下:

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)
Run Code Online (Sandbox Code Playgroud)

我知道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;
}
Run Code Online (Sandbox Code Playgroud)

小智 53

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();
            }
Run Code Online (Sandbox Code Playgroud)


Thi*_*rry 24

您可以使用为此单元格定义的格式将值作为String获取:

final DataFormatter df = new DataFormatter();
final XSSFCell cell = row.getCell(cellIndex);
String valueAsString = df.formatCellValue(cell);
Run Code Online (Sandbox Code Playgroud)

感谢这个答案.


小智 21

只需使用cell.setCellType(1); 在读取单元格值之前,始终将其作为String,之后您可以使用自己的格式(类型).

拉维