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?
getCellType. Return 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With