xssf How to get anything as String

14,630

Solution 1

Excel stores some cells as strings, but most as numbers with special formatting rules applied to them. If you want to get the raw values, use a switch statement based on cell.getCellType() as some of the other answers have shown.

However, if what you want is a string of the cell, showing the same as what Excel would show, based on applying all the formatting rules on the cell + cell types, then Apache POI has a class to do just that - DataFormatter

All you need to do is something like:

 Workbook wb = WorkbookFactory.create(new File("myfile.xls"));
 DataFormatter df = new DataFormatter();

 Sheet s = wb.getSheetAt(0);
 Row r1 = s.getRow(0);
 Cell cA1 = r1.getCell(0);

 String asItLooksInExcel = df.formatCellValue(cA1);

Doesn't matter what the cell type is, DataFormatter will format it as best it can for you, using the rules applied in Excel, and giving you back a nicely formatted string at the end.

Solution 2

The accepeted answer does not work with formula cells (in the result String you get the formula, not the result of the formula). Here is what worked for me in every case:

final XSSFWorkbook workbook = new XSSFWorkbook(file);
final DataFormatter dataFormatter = new DataFormatter();
final FormulaEvaluator objFormulaEvaluator = new XSSFFormulaEvaluator(workbook);
final Cell cell = ...;
objFormulaEvaluator.evaluate(cell);
final String cellValue = dataFormatter.formatCellValue(cell, objFormulaEvaluator);

Solution 3

You can add check on CELL type as below :

switch(cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t\t");
                    break;
            }
Share:
14,630
mxcd
Author by

mxcd

Updated on June 15, 2022

Comments

  • mxcd
    mxcd about 2 years

    I try to parse an excel file into XML using apache poi xssf. Now having a cell and not knowing what is in it I just want to get a String out of it. But when I use

    cell.getStringCellValue()
    

    it throws an exception, what is not very suprising since it is documented this way. So I build my way around that by checking weather it is a numeric or a text cell. But what to do with formula cells. They may contain numbers like

    = A2 + B2
    

    What gives me the sum (e.g. 4) or a reference to another text

    = C2
    

    what might refer to a text like "Hans".

    How can I know what is really in my cell and how do I get a String out of it?

  • mxcd
    mxcd over 10 years
    I already did that. But what to do when the CELL type is CELL_TYPE_FORMULA? How can I know which getter I have to use??
  • sameer joshi
    sameer joshi over 5 years
    is it works for reference cell? like first cell has reference of another sheet first cell..