65

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

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?

user1766169
  • 1,932
  • 3
  • 22
  • 44

8 Answers8

70

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

CellType    getCellTypeEnum()

where the CellType is the enum decribing the type of the cell.

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

Tomasz Stanczak
  • 12,796
  • 1
  • 30
  • 32
  • 8
    Wow. This (planned) change is the direct way into future binary incompatibilities. Great! :-) (yes, I know, you shouldn't upgrade to a new major version without checking upgrade guides etc... but it WILL be one of the first questions here as soon as POI 4.0 is out) – Florian Albrecht Oct 12 '16 at 10:15
  • 1
    Yep. Although - this is a plan only, this is what is in the comments now, still might not happen. I'm not sure how much influence a publicity can have on an open source project. – Tomasz Stanczak Oct 12 '16 at 11:14
  • 1
    @FlorianAlbrecht As long as you use things like `Cell.CELL_TYPE_NUMERIC` not `0` in your switch / if statements, POI should be able to maintain source code compatibility for your code so you'd just need to recompile – Gagravarr Oct 12 '16 at 13:04
  • @Gagravarr yes, that is exactly the definition of "binary incompatibility". – Florian Albrecht Oct 12 '16 at 13:14
  • @FlorianAlbrecht If you use int literals as the deprecation warning now tells you not to, you won't get source compatibility though! Also, sadly some fairly basic and deep design decisions in the JVM and java specs mean that you sometimes do have to break binary compatibility to make improvements to libraries – Gagravarr Oct 12 '16 at 13:59
  • 2
    @Gagravarr The deprecation warning tells me not to use the int returning method at all. If I use int literals or the constants from the library makes no difference for the deprecation. Imagine the following: I write a library using POI 3.x and getCellType(). I use the constants from the library. Now, you use my library in a project, but also POI directly, and decide to upgrade to POI 4.0. You **will** get a **binary incomatibility** at **run-time**. Yeah, the error was to upgrade to POI 4.0 without checking all your dependencies. But do you really expect no one to do that error? – Florian Albrecht Oct 12 '16 at 14:03
  • @TomaszStanczak What is the replacement for `cell.setCellType(Cell.CELL_TYPE_STRING)`? – whatthefish Aug 24 '18 at 15:40
  • actually CELL_TYPE_NUMERIC won't work - enum return type NUMERIC would work – oneday Aug 25 '20 at 06:54
23

You can use:

cell.getCellTypeEnum()

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

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

user7171758
  • 292
  • 3
  • 10
15

Use getCellType()

switch (cell.getCellType()) {
   case BOOLEAN :
                 //To-do
                 break;
   case NUMERIC:
                 //To-do
                 break;
   case STRING:
                 //To-do
                 break;
}
Hemanth Peela
  • 169
  • 4
  • 14
Benigno Sales
  • 193
  • 2
  • 6
10
    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 NUMERIC or STRING.

user1766169
  • 1,932
  • 3
  • 22
  • 44
Arindam
  • 101
  • 1
  • 3
8

From the documentation:

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

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

DimaSan
  • 12,264
  • 11
  • 65
  • 75
3

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...

frva
  • 76
  • 1
  • 5
  • 10
    You can calmly use method `getCellTypeEnum()` in 3.15. Its "Deprecated" mark is a [known bug 60228](https://bz.apache.org/bugzilla/show_bug.cgi?id=60228). It should be fixed in 3.16. – lu_ko Jan 11 '17 at 12:56
  • 1
    Strict disallowance of deprecated code = big stupid bug. – th3byrdm4n Mar 04 '17 at 01:11
0

For POI 3.17 this worked for me

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;
}
Srinivas
  • 21
  • 1
  • 7
-2

You can do this:

private String cellToString(HSSFCell cell) {
    CellType type;
    Object result;
    type = cell.getCellType();

    switch (type) {
        case NUMERIC :  //numeric value in excel
            result = cell.getNumericCellValue();
            break;
        case STRING : //String Value in Excel
            result = cell.getStringCellValue();
            break;
        default :
            throw new RuntimeException("There is no support for this type of value in Apche POI");
    }
    return result.toString();
}
ordonezalex
  • 2,645
  • 1
  • 20
  • 33
shan
  • 1
  • Hey, welcoime to StackOverflow! As the OP already said on their post, they are getting a warning saying that `cell.getCellType()` is deprecated. Your answer is using the same method, so it is not solving this particular problem. Please consider updating it! – Cris Jul 16 '20 at 07:28