Alternative to deprecated getCellType

user1766169 picture user1766169 · Oct 12, 2016 · Viewed 101k times · Source

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?

Answer

Tomasz Stanczak picture Tomasz Stanczak · Oct 12, 2016

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.