Alternative to deprecated getCellType - java

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?

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.

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

Use getCellType()
switch (cell.getCellType()) {
case BOOLEAN :
//To-do
break;
case NUMERIC:
//To-do
break;
case STRING:
//To-do
break;
}

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.

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.

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

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;
}

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();
}

Related

How to handle the Unexpected arg eval type (org.apache.poi.ss.formula.eval.MissingArgEval) in apache POI

I am trying to parse an excel file which contains various kind of fomulas.The code i have writen is able to parse almost all kind of formulas but , some of the cells contains reference of another cell like =C20 like this. In this type of cells my code is having
Unexpected arg eval type (org.apache.poi.ss.formula.eval.MissingArgEval)
My code is able to handle below types
=IF(D14=0;" - ";E14/D14), =IF('TreeData-Report'!AB92="H";"";SUM('TreeData-Report'!C92:'TreeData-Report'!E92)) .. etc.
But when it is coming as a single argument like C20,D14 ..etc, it is failing .
And i will post the code where exactly it is failing
while (cellIterator.hasNext() && cellIteratorTotal.hasNext()) {
cellCount++;
Cell currentCell = cellIterator.next();
Cell currentCellTotal = cellIteratorTotal.next();
String cellValue = excelManager.evalCell(currentCell);// from here i am sending the value for validation
String cellValueTotal = excelManager.evalCell(currentCellTotal);
This is the validator class where i am chekcing thetype of the cell value
public String evalCell(Cell cell) {
String cellValue = "";
if (cell.getCellTypeEnum() == CellType.STRING) {
cellValue = cell.getStringCellValue();
} else if (cell.getCellTypeEnum() == CellType.NUMERIC) {
cellValue = formatDoubleNumberToString(cell.getNumericCellValue());
} else if (cell.getCellTypeEnum() == CellType.FORMULA) {
cellValue = evalFormulaCell(cell);
}
return cellValue.trim();
}
public String evalFormulaCell(Cell cell) {
String cellValue = "";
switch (formulaEvaluator.evaluateFormulaCellEnum(cell)) {//Here the code fails if cell contains C20,D14.. etc this type of values
case BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case NUMERIC:
cellValue = formatDoubleNumberToString(cell.getNumericCellValue());
break;
case STRING:
cellValue = cell.getStringCellValue();
break;
case BLANK:
cellValue = "";
break;
case ERROR:
cellValue = "Error Occurred with Code :"+String.valueOf(cell.getErrorCellValue());
break;
case _NONE:
cellValue = "";
break;
// CELL_TYPE_FORMULA will never occur
case FORMULA:
cellValue = "";
break;
}
return cellValue;
}
Please help me to handle this kind of errors , and i am very sorry i am am not able to make my post crystal clear.Thanks in advance.I am using poi 3.16 jar.

Apache POI: non-deprecated way to get cell content? [duplicate]

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?
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.
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
Use getCellType()
switch (cell.getCellType()) {
case BOOLEAN :
//To-do
break;
case NUMERIC:
//To-do
break;
case STRING:
//To-do
break;
}
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.
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.
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...
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;
}
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();
}

Reading cell content as rich text using Apache POI. Handling numeric cells when using cell.getRichStringCellValue () method

I want to read a cell value from an Excel Spreadsheet as a rich text, not String, but an exception is thrown when the cell type is numeric and I am using cell.getRichStringCellValue () method . What would be a good approach to handle this problem?
You need to follow the approach carefully and lovingly laid out in the Apache POI documentation (who'd have thought?!). You'll want to do something like:
import org.apache.poi.ss.usermodel.*;
Workbook wb = WorkbookFactory.create(new File("input.xls"));
Sheet sheet1 = wb.getSheetAt(0);
for (Row row : sheet1) {
for (Cell cell : row) {
CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
System.out.print(cellRef.formatAsString());
System.out.print(" - ");
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
RichTextString contents = cell.getRichStringCellValue();
// TODO Handle contents
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
// TODO Handle Date value
} else {
double number = cell.getNumericCellValue();
// TODO Handle number
}
break;
case Cell.CELL_TYPE_BOOLEAN:
boolean value = cell.getBooleanCellValue();
// TODO Handle
break;
case Cell.CELL_TYPE_FORMULA:
// Either get formula, or check last value, or evaluate
break;
default:
// Shouldn't happen
}
}
}
Then add your own logic for handling the contents now that you have fetched them

How to get the formula cell value(data) using apache poi 3.1

I am using Apache poi-3.1-FINAL-20080629 in my application. here, I have one problem using formula...
My Cell has formula(sheet2!C10) and the data inside this cell is String type (e.g $3,456)...How to access that cell also want to display the formula.
My code Looks like:
HSSFWorkbook wb = new HSSFWorkbook(file);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(sheet, wb);
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
System.out.println("\n");
Iterator cells = row.cellIterator();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
int cellType = cell.getCellType();
if (HSSFCell.CELL_TYPE_NUMERIC == cellType)
System.out.print(cell.getNumericCellValue() + " ");
else if (HSSFCell.CELL_TYPE_STRING == cellType)
System.out.print(cell.getStringCellValue() + " ");
else if (HSSFCell.CELL_TYPE_BOOLEAN == cellType)
System.out.print(cell.getBooleanCellValue() + " ");
else if (HSSFCell.CELL_TYPE_BLANK == cellType)
System.out.print("BLANK ");
else if (HSSFCell.CELL_TYPE_FORMULA == cellType) {
System.out.print(evaluator.evaluateInCell(cell).toString() ); } else
System.out.print("Unknown cell type " + cellType);
}
}
evaluator.evaluateInCell(cell).toString() is throwing null pointer exception. Please Help!!!
OK, so first up, Apache POI 3.1 is rather old. The clue is in the jar name - poi-3.1-FINAL-20080629 dates from 2008, so 6 years ago! It's well worth upgrading, the number of bug fixes since then is pretty vast....
As for your problem, I'm fairly sure that you don't want to be calling evaluator.evaluateInCell. That's almost never the right method to call
You have three options available to you, depending on what you want to do with your formula cell:
I want the formula string
Call Cell.getCellFormula and you'll get back the formula string
I want the last formula value Excel calculated
When Excel writes out a file, it normally stores the last evaluated value for the formula, in a cache, to make opening nice and quick. You can read this, if present, from Apache POI. It's only a cache, so it might not always be correct, but it normally is.
You need to call Cell.getCachedFormulaResultType, then switch on that, and read the values out using the normal getters, eg
int cellType = cell.getCellType();
handleCell(cell, cellType);
private void handleCell(Cell cell, int cellType) {
if (HSSFCell.CELL_TYPE_NUMERIC == cellType)
System.out.print(cell.getNumericCellValue() + " ");
else if (HSSFCell.CELL_TYPE_STRING == cellType)
System.out.print(cell.getStringCellValue() + " ");
else if (HSSFCell.CELL_TYPE_BOOLEAN == cellType)
System.out.print(cell.getBooleanCellValue() + " ");
else if (HSSFCell.CELL_TYPE_BLANK == cellType)
System.out.print("BLANK ");
else if (HSSFCell.CELL_TYPE_FORMULA == cellType)
handleCell(cell, cell.getCachedFormulaResultType());
else
System.out.print("Unknown cell type " + cellType);
}
I want Apache POI to calculate the formula result for me
Your best bet here is to get a FormulaEvaluator object and call evaluate:
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
// suppose your formula is in B3
CellReference cellReference = new CellReference("B3");
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol());
CellValue cellValue = evaluator.evaluate(cell);
switch (cellValue.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cellValue.getBooleanValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println(cellValue.getNumberValue());
break;
case Cell.CELL_TYPE_STRING:
System.out.println(cellValue.getStringValue());
break;
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_ERROR:
break;
// CELL_TYPE_FORMULA will never happen
case Cell.CELL_TYPE_FORMULA:
break;
}
The Apache POI Formula Evaluation page has more on all of these

how to store a parsed .xslx file in a dto

Code:parses through the excel file using poi and prints the output in the console and also creates a new excel file to show the output.
XSSFWorkbook workbook = new XSSFWorkbook(fileName);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row ;
XSSFCell cell;
Iterator<Row> rows = sheet.rowIterator();
while(rows.hasNext())
{
row = (XSSFRow)rows.next();
Iterator<Cell> cells = row.cellIterator();
while(cells.hasNext())
{
cell = (XSSFCell)cells.next();
switch(cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()+"\t\t");
break;
case Cell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()+ "\t\t");
break;
case Cell.CELL_TYPE_STRING:System.out.println(cell.getStringCellValue()+ "\t\t");
break;
}
}System.out.println("");
}fileName.close();
FileOutputStream out = new FileOutputStream(new File("C://data.xlsx"));
workbook.write(out);
out.close();
Output:
Id
Name
Location
Role
Salary
111.0
Kumar
Chennai
Developer
1000.0
112.0
Larsen
Bangalore
Developer
2000.0
Queries:
1. How to get the output in the same format as in excel?
2. How to store the output in a DTO object?
try this way to add Values DTO
Create one Student DTO with properties like and setter and getters with Id,Name,Location,Role,Salary
while(rows.hasNext())
{
row = (XSSFRow)rows.next();
Iterator<Cell> cells = row.cellIterator();
StudentDTO std = new StudentDTO();
while(cells.hasNext())
{
cell = (XSSFCell)cells.next();
switch(cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()+"\t\t");
std.setId(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()+ "\t\t");
std.setName(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:System.out.println(cell.getStringCellValue()+ "\t\t");
std.setLocation(cell.getStringCellValue());
break;
}
}System.out.println("");
}

Categories