Java 已弃用的 getCellType 的替代方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39993683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:37:00  来源:igfitidea点击:

Alternative to deprecated getCellType

javaapache-poi

提问by user1766169

I'm reading an excel-file (file extension xlsx) using org.apache.poi 3.15.

我正在使用 org.apache.poi 3.15 读取 excel 文件(文件扩展名 xlsx)。

This is my code:

这是我的代码:

try (FileInputStream fileInputStream = new FileInputStream(file); XSSFWorkbook workbook = new XSSFWorkbook(file)) {
    XSSFSheet sheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = sheet.iterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();

        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "(Integer)\t");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "(String)\t");
                    break;
            }
        }
        System.out.println("");
    }
} catch (Exception e) {
    e.printStackTrace();
}

I get a warning that cell.getCellType()is deprecated. Can anyone tell me the alternative?

我收到一条cell.getCellType()已弃用的警告。谁能告诉我替代方案?

采纳答案by Tomasz Stanczak

The accepted answer shows the reason for the deprecation but misses to name the alternative:

接受的答案显示了弃用的原因,但没有说出替代方案:

CellType    getCellTypeEnum()

where the CellTypeis the enum decribing the type of the cell.

其中 theCellType是描述单元格类型的枚举。

The plan is to rename getCellTypeEnum()back to getCellType()in POI 4.0.

计划是在 POI 4.0 中重命名getCellTypeEnum()getCellType()

回答by DimaSan

From the documentation:

文档

int getCellType()Deprecated. POI 3.15. Will return a CellTypeenum in the future.

Return the cell type. Will return CellTypein version 4.0 of POI. For forwards compatibility, do not hard-code cell type literals in your code.

int getCellType()已弃用。兴趣点 3.15。将来会返回一个CellType枚举。

返回单元格类型。将CellType在 POI 4.0 版中返回。为了向前兼容,不要在代码中硬编码单元格类型文字。

回答by frva

It looks that 3.15 offers no satisfying solution: either one uses the old style with Cell.CELL_TYPE_*, or we use the method getCellTypeEnum() which is marked as deprecated. A lot of disturbances for little add value...

看起来 3.15 没有提供令人满意的解决方案:要么使用 Cell.CELL_TYPE_* 的旧样式,要么我们使用标记为已弃用的 getCellTypeEnum() 方法。很少有附加值的很多干扰......

回答by user7171758

You can use:

您可以使用:

cell.getCellTypeEnum()

Further to compare the cell type, you have to use CellType as follows:-

进一步比较单元格类型,您必须使用 CellType 如下:-

if(cell.getCellTypeEnum() == CellType.STRING){
      .
      .
      .
}

You can Refer to the documentation. Its pretty helpful:-

您可以参考文档。它非常有帮助:-

https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html

https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html

回答by Arindam

    FileInputStream fis = new FileInputStream(new File("C:/Test.xlsx"));

    //create workbook instance
    XSSFWorkbook wb = new XSSFWorkbook(fis);

    //create a sheet object to retrieve the sheet
    XSSFSheet sheet = wb.getSheetAt(0);

    //to evaluate cell type
    FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();

    for(Row row : sheet)
    {
        for(Cell cell : row)
        {
            switch(formulaEvaluator.evaluateInCell(cell).getCellTypeEnum())
            {
            case NUMERIC:
                System.out.print(cell.getNumericCellValue() + "\t");
                break;
            case STRING:
                System.out.print(cell.getStringCellValue() + "\t");
                break;
            default:
                break;

            }
        }
        System.out.println();
    }

This code will work fine. Use getCellTypeEnum()and to compare use just NUMERICor STRING.

此代码将正常工作。使用getCellTypeEnum()和 比较仅使用NUMERICSTRING

回答by Benigno Sales

Use getCellType()

使用 getCellType()

switch (cell.getCellType()) {
   case BOOLEAN :
                 //To-do
                 break;
   case NUMERIC:
                 //To-do
                 break;
   case STRING:
                 //To-do
                 break;
}

回答by Srinivas

For POI 3.17 this worked for me

对于 POI 3.17,这对我有用

switch (cellh.getCellTypeEnum()) {
    case FORMULA: 
        if (cellh.getCellFormula().indexOf("LINEST") >= 0) {
            value = Double.toString(cellh.getNumericCellValue());
        } else {
            value = XLS_getDataFromCellValue(evaluator.evaluate(cellh));
        }
        break;
    case NUMERIC:
        value = Double.toString(cellh.getNumericCellValue());
        break;
    case STRING:
        value = cellh.getStringCellValue();
        break;
    case BOOLEAN:
        if(cellh.getBooleanCellValue()){
            value = "true";
        } else {
            value = "false";
        }
        break;
    default:
        value = "";
        break;
}