Example usage for org.apache.poi.ss.usermodel Cell getNumericCellValue

List of usage examples for org.apache.poi.ss.usermodel Cell getNumericCellValue

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Cell getNumericCellValue.

Prototype

double getNumericCellValue();

Source Link

Document

Get the value of the cell as a number.

Usage

From source file:mw.sqlitetool.ExcelHelper.java

public Object getCellValue(Cell cell) {
    if (cell == null || cell.getCellType() == cell.CELL_TYPE_BLANK) {
        return "";
    } else if (cell.getCellType() == cell.CELL_TYPE_BOOLEAN) {
        return cell.getBooleanCellValue();
    } else if (cell.getCellType() == cell.CELL_TYPE_ERROR) {
        return cell.getErrorCellValue();
    } else if (cell.getCellType() == cell.CELL_TYPE_FORMULA) {
        FormulaEvaluator evaluator = _workbook.getCreationHelper().createFormulaEvaluator();
        CellValue val = evaluator.evaluate(cell);
        if (val.getCellType() == cell.CELL_TYPE_BOOLEAN) {
            return val.getBooleanValue();
        } else if (val.getCellType() == cell.CELL_TYPE_NUMERIC) {
            return val.getNumberValue();
        } else if (val.getCellType() == cell.CELL_TYPE_STRING) {
            return val.getStringValue();
        } else if (val.getCellType() == cell.CELL_TYPE_ERROR) {
            return val.getErrorValue();
        }/*  w w  w  .j av a 2 s  .c  o  m*/
    } else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        } else {
            return cell.getNumericCellValue();
        }
    } else if (cell.getCellType() == cell.CELL_TYPE_STRING) {
        return cell.getStringCellValue();
    }
    return "";
}

From source file:nc.noumea.mairie.appock.services.impl.ImportExcelServiceImpl.java

License:Open Source License

private String getCellValue(Cell cell) {
    if (cell == null) {
        return null;
    }//from   w w w  . ja va  2s .c  o m

    // Les deprecated son un bug connu de la librairie, ils se sont plants et l'ont laiss deperecated
    if (cell.getCellTypeEnum() == CellType.STRING) {
        return cell.getStringCellValue();
    } else if (cell.getCellTypeEnum() == CellType.NUMERIC) {
        return String.valueOf((int) cell.getNumericCellValue());
    }

    return null;
}

From source file:negocio.parser.ExcelReader.java

@Override
public IExcelContent leerArchivo(String ruta) throws Exception {
    java.util.Date date = new java.util.Date();
    Date entrada = new Date(date.getTime());
    IExcelContent ec = ExcelContent.getInstantiateExcelContent();
    try {//from w  w  w.j a v  a2 s .  c  o m
        LogDAO dao = new LogDAO();
        LogDTO dto = new LogDTO("Leer archivo", "Comienzo de lectura de archivo", entrada.toString(),
                entrada.toString());
        dao.registrarLog(dto);
        File archivo = new File(ruta);
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(archivo)); //crear un libro excel
        XSSFSheet sheet = workbook.getSheetAt(0); //acceder a la primera hoja
        Iterator<Row> rowIterator = sheet.iterator();
        Row row;
        boolean sw = true;
        ArrayList<List<String>> datos = new ArrayList<>();
        while (rowIterator.hasNext()) {

            row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            Cell celda;
            List<String> fila = new ArrayList<>();
            while (cellIterator.hasNext()) {
                celda = cellIterator.next();
                String dato = "";
                switch (celda.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    if (HSSFDateUtil.isCellDateFormatted(celda)) {
                        dato = celda.getDateCellValue().toString();
                    } else {
                        dato = celda.getNumericCellValue() + "";
                    }
                    break;
                case Cell.CELL_TYPE_STRING:
                    dato = celda.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    dato = celda.getBooleanCellValue() + "";
                    break;
                }
                fila.add(dato);
            }
            if (sw) {
                sw = false;
                ec.setTitulos(fila);
            } else {
                datos.add(fila);
            }
        }
        ec.setDatos(datos);
        workbook.close();
        return ec;
    } catch (IOException ex) {
        Logger.getLogger(ExcelReader.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;

}

From source file:net.bafeimao.umbrella.support.data.entity.ExcelEntityParser.java

License:Apache License

private String getCellValue(Cell cell) {
    Preconditions.checkNotNull("cell");

    Object retVal = null;//from   w ww.jav  a2  s  . c  om
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_FORMULA:
        // Get the type of Formula
        switch (cell.getCachedFormulaResultType()) {
        case Cell.CELL_TYPE_STRING:
            retVal = cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_NUMERIC:
            retVal = cell.getNumericCellValue();
            break;
        default:
        }
        // retVal = formulaEval.evaluate(cell).formatAsString();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell))
            retVal = cell.getDateCellValue();
        else
            retVal = cell.getNumericCellValue();
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        retVal = cell.getBooleanCellValue();
        break;
    case Cell.CELL_TYPE_STRING:
        retVal = cell.getStringCellValue();
        break;
    default:
        retVal = null;
    }
    return retVal == null ? null : retVal.toString();
}

From source file:net.ceos.project.poi.annotated.core.CellHandler.java

License:Apache License

/**
 * Recover the value from the Excel./*from w  w  w .j a  v  a2 s  . c om*/
 * 
 * @param cell
 *            the {@link Cell}
 * @return the content of the cell
 */
private static String readCell(final Cell cell) {
    if (cell == null) {
        return null;
    }

    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        return cell.getStringCellValue();
    }

    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        Double value = cell.getNumericCellValue();
        return value.toString();
    }

    if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        return switchCachedFormulaManager(cell);
    }
    return null;
}

From source file:net.ceos.project.poi.annotated.core.CellHandler.java

License:Apache License

/**
 * Manage the cached formula case./*from w w  w  . j  a  v  a2s. co m*/
 * 
 * @param cellthe
 *            {@link Cell}
 * @return the content of the cell
 */
private static String switchCachedFormulaManager(final Cell cell) {
    switch (cell.getCachedFormulaResultType()) {
    case Cell.CELL_TYPE_NUMERIC:
        Double value = cell.getNumericCellValue();
        return value.toString();

    case Cell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue().toString();

    default:
        return null;
    }
}

From source file:net.ceos.project.poi.annotated.core.MaskTest.java

License:Apache License

/**
 * //from  w  w w.  java 2  s. co m
 * Test the marshal/unmarshal of one object applying mask at the Double
 * attribute with {@link XlsElement}
 */
@Test(dataProvider = "dataProvider")
public void checkDoubleFormatMask(ObjectMask charger, Workbook wb) throws Exception {
    // format double attributes
    Cell cellDouble1 = extractCell(charger, wb.getSheetAt(0), 9);
    assertEquals(Double.valueOf(cellDouble1.getNumericCellValue()), charger.getDoubleAttribute2());

    Cell cellDouble2 = extractCell(charger, wb.getSheetAt(0), 10);
    assertEquals(Double.valueOf(cellDouble2.getNumericCellValue()), charger.getDoubleAttribute3());
}

From source file:net.ceos.project.poi.annotated.core.MaskTest.java

License:Apache License

/**
 * //w w w  .  j av  a  2  s  .  c  o  m
 * Test the marshal/unmarshal of one object applying mask at the Float
 * attribute with {@link XlsElement}
 */
// @Test(dataProvider = "dataProvider")
public void checkFloatFormatMask(ObjectMask charger, Workbook wb) throws Exception {
    // FIX this problem : incorrect application of the mask
    // format float attributes
    Cell cellFloat1 = extractCell(charger, wb.getSheetAt(0), 20);
    assertEquals(cellFloat1.getNumericCellValue(), charger.getFloatAttribute2());

    Cell cellFloat2 = extractCell(charger, wb.getSheetAt(0), 21);
    assertEquals(cellFloat2.getNumericCellValue(), charger.getFloatAttribute3());
}

From source file:net.ceos.project.poi.annotated.core.MaskTest.java

License:Apache License

/**
 * /*  w  w w .jav a 2s  .  c o m*/
 * Test the marshal/unmarshal of one object applying mask at the Boolean
 * attribute with {@link XlsElement}
 */
@Test(dataProvider = "dataProvider")
public void checkIntegerFormatMask(ObjectMask charger, Workbook wb) throws Exception {
    // transform integer attributes
    Cell cellInteger = extractCell(charger, wb.getSheetAt(0), 28);
    assertEquals(charger.getIntegerAttribute1(), Integer.valueOf((int) cellInteger.getNumericCellValue()));
    assertEquals(parserNumeric(charger.getIntegerAttribute1(), "0.0"), cellInteger.getNumericCellValue());
}