Alternative to deprecated getCellType
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.
alternate for getcelltype in poi 3.17 other than getCellTypeEnum()
You should be able to apply the change using the getCellTypeEnumn()
. The Cell.CELL_TYPE_*
enums are deprecated in Poi 3.17 but you can use Celltype.*
, for example:
if(cell.getCellTypeEnum() == CellType.STRING){
//your code
}else if(cell.getCellTypeEnum() == CellType.NUMERIC){
//your code
}
Values for Celltype are:
_NONE(-1),
NUMERIC(0),
STRING(1),
FORMULA(2),
BLANK(3),
BOOLEAN(4),
ERROR(5);
Hope this can help you.
Thanks.
setCellType(CellType.STRING) is deprecated
Before:
row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellType(
contentValues.put(ITEMCODE, row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue());
After:
InputStream strm =getClass().getResourceAsStream("Sample.xls"));
Workbook wb = WorkbookFactory.create(strm);
DataFormatter Stringform = new DataFormatter();
FormulaEvaluator Formeval = new HSSFFormulaEvaluator((HSSFWorkbook) wb);
Sheet sheet= wb.getSheetAt(0);
Iterator<Row> rit = sheet.rowIterator();
while(rit.hasNext()){
Row row = rit.next();
Cell cellValue = row.getCell(0);
Formeval.evaluate(row.getCell(0)); // Returns string
String cellValueStr = Stringform.formatCellValue(row.getCell(0),Formeval);
ContentValues contentValues = new ContentValues();
contentValues.put(DNNO, Stringform.formatCellValue(row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK),Formeval));
}
The method setCellType(int) from the type XSSFCell is deprecated
you can use org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC
like following type:
setCellType(Cell.CELL_TYPE_NUMERIC);
hope this help you.
Depreciated CellType method
I've been struggling with the same issue and I found the following works for me.
It is the inverse of your approach.
I check if the value is not null.
The code below is for handling string values:
private static String checkForNullString(Cell cellToCheck) {
String strCheck;
if (cellToCheck != null && cellToCheck.getCellTypeEnum() != CellType.BLANK) {
cellToCheck.setCellType(CellType.STRING);
strCheck = cellToCheck.getStringCellValue();
}
else
strCheck = "";
return strCheck;
}
To handle numeric values, simply change the following:
cellToCheck.getStringCellValue();
to
cellToCheck.getNumericCellValue());
Related Topics
How to Convert a Java Object to Xml with Open Source APIs
Are Two Java Objects with Same Hashcodes Not Necessarily Equal
Hashcode and Equals for Hashset
Is There Something Like Instanceof(Class<> C) in Java
Spring Boot Actuator Without Spring Boot
Spring Choose Bean Implementation at Runtime
Unsatisfiedlinkerror: No Opencv_Java249 in Java.Library.Path
How to Determine the Ip of My Router/Gateway in Java
Scanner Class Skips Over Whitespace
Java: CSV File Easy Read/Write
Getting Class Cast Exception Where Both Classes Are Exactly the Same
How to Inject Module Declaration into Jar
Java: Global Exception Handler
Eventlisteners Using Hibernate 4.0 with Spring 3.1.0.Release
When to Use a List Over an Array in Java