Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to deprecated getCellType

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?

like image 409
user1766169 Avatar asked Oct 12 '16 08:10

user1766169


People also ask

What does getCellType return?

getCellType. Return the cell type.

How do you find the cell type?

At the Cell level, we can use its getCellType() method to get the cell type. Apache POI supports the following cell types: BLANK.

What is workbook in Java?

public interface Workbook extends java.io.Closeable, java.lang.Iterable<Sheet> High level representation of a Excel workbook. This is the first object most users will construct whether they are reading or writing a workbook. It is also the top level object for creating new sheets/etc.


1 Answers

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.

like image 151
Tomasz Stanczak Avatar answered Sep 28 '22 21:09

Tomasz Stanczak