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:com.radaee.excel.ToHtml.java

License:Apache License

private String getText(Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        return "";
    case Cell.CELL_TYPE_BOOLEAN:
        break;//from  w  w w .jav a2 s .c om
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    case Cell.CELL_TYPE_NUMERIC:
        String phone = Double.toString(cell.getNumericCellValue());

        String str[] = phone.split("[.]");
        phone = str[0];
        if (str[1].contains("E")) {
            String str1[] = str[1].split("[E]");
            phone += str1[0];
        }

        return phone;
    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();
    default:
        return "";
    }
    return null;
}

From source file:com.rapidminer.operator.nio.Excel2007SheetTableModel.java

License:Open Source License

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Cell cell;
    if (config != null) {
        Row row = sheet.getRow(rowIndex + config.getRowOffset());
        if (row == null) {
            return null;
        }//  ww  w.j  a va  2 s . co  m
        cell = row.getCell(columnIndex + config.getColumnOffset());
    } else {
        Row row = sheet.getRow(rowIndex);
        if (row == null) {
            return null;
        }
        cell = row.getCell(columnIndex);
    }
    if (cell == null) {
        return null;
    }
    if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        return cell.getBooleanCellValue();
    } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        return cell.getStringCellValue();
    } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        } else {
            return cell.getNumericCellValue();
        }
    } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
        return cell.getErrorCellValue();
    } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        return cell.getNumericCellValue();
    } else {
        // last resort, should not come to this
        // maybe return null?
        return "";
    }
}

From source file:com.rapidminer.operator.nio.model.Excel2007ResultSet.java

License:Open Source License

@Override
public Number getNumber(int columnIndex) throws ParseException {
    final Cell cell = getCurrentCell(columnIndex);
    if (cell == null) {
        return Double.NaN;
    }/*ww  w . j a  v  a 2 s.  c o m*/
    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC || cell.getCellType() == Cell.CELL_TYPE_FORMULA
            && cell.getCachedFormulaResultType() == Cell.CELL_TYPE_NUMERIC) {
        final double value = cell.getNumericCellValue();
        return Double.valueOf(value);
    } else {
        String valueString = "";
        try {
            valueString = cell.getStringCellValue();
            return Double.valueOf(valueString);
        } catch (NumberFormatException e) {
            throw new ParseException(new ParsingError(currentRow, columnIndex,
                    ParsingError.ErrorCode.UNPARSEABLE_REAL, valueString));
        } catch (IllegalStateException e) {
            throw new ParseException(new ParsingError(currentRow, columnIndex,
                    ParsingError.ErrorCode.UNPARSEABLE_REAL, "CELL_NOT_NUMERIC"));
        }
    }
}

From source file:com.rapidminer.operator.nio.model.Excel2007ResultSet.java

License:Open Source License

@Override
public String getString(int columnIndex) {
    final Cell cell = getCurrentCell(columnIndex);
    if (cell == null) {
        return "";
    }// www.j  a  v a2  s.  c  o m
    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        return String.valueOf(cell.getNumericCellValue());
    } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        String value;
        if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_NUMERIC) {
            value = String.valueOf(cell.getNumericCellValue());
        } else {
            value = cell.getStringCellValue();
        }
        return value;
    } else {
        try {
            return cell.getStringCellValue();
        } catch (IllegalStateException e) {
            return "";
        }
    }
}

From source file:com.read.main.LeerPDF.java

/**
 * @param args the command line arguments
 *///  ww w  . j av a 2 s  .c  om
public static void main(String[] args) throws IOException {
    try {

        FileInputStream file = new FileInputStream(new File("/home/aaron/Escritorio/Example.xlsx"));
        XSSFWorkbook workbook2 = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook2.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();

            System.out.println("Numero de Columnas: " + row.getLastCellNum());

            System.out.println(row.getRowNum());

            if (row.getRowNum() == 0) {
                System.out.println("Fila Cero");
            } else {

                int numColumna = 0;

                while (numColumna < row.getLastCellNum()) {

                    Cell cell = row.getCell(numColumna);

                    try {
                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            System.out.print(numColumna + ".- BOOLEAN: ");
                            System.out.print(cell.getBooleanCellValue() + "\t\t");
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(numColumna + ".- NUMERIC: ");
                            System.out.print(cell.getNumericCellValue() + "\t\t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(numColumna + ".- STRING: ");
                            System.out.print(cell.getStringCellValue() + "\t\t");
                            break;
                        }
                    } catch (Exception e) {
                        System.err.println(e);
                    }
                    ;

                    numColumna++;
                }
            }

            System.out.println("");
        }
        file.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.rodrigodev.xgen4j_table_generator.test.common.assertion.excel.conditions.ExcelFile.java

License:Open Source License

@Override
public boolean matches(InputStream actualInputStream) {

    boolean result = true;

    try {/*  ww  w .  j  av a  2  s .c o  m*/
        try (Workbook expectedWb = WorkbookFactory.create(expectedInputStream)) {
            Sheet expectedSheet = expectedWb.getSheetAt(0);

            try (Workbook actualWb = WorkbookFactory.create(actualInputStream)) {
                Sheet actualSheet = actualWb.getSheetAt(0);

                int expectedRowCount = expectedSheet.getLastRowNum();
                for (int r = 0; r <= expectedRowCount; r++) {
                    Row expectedRow = expectedSheet.getRow(r);
                    Row actualRow = actualSheet.getRow(r);
                    if (actualRow == null) {
                        actualRow = actualSheet.createRow(r);
                    }

                    int expectedCellCount = expectedRow.getLastCellNum();
                    for (int c = 0; c < expectedCellCount; c++) {
                        Cell expectedCell = expectedRow.getCell(c, Row.CREATE_NULL_AS_BLANK);
                        Cell actualCell = actualRow.getCell(c, Row.CREATE_NULL_AS_BLANK);

                        if (expectedCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                            assertThat(actualCell.getNumericCellValue())
                                    .isEqualTo(expectedCell.getNumericCellValue(), offset(0.00001));
                        } else {
                            expectedCell.setCellType(Cell.CELL_TYPE_STRING);
                            actualCell.setCellType(Cell.CELL_TYPE_STRING);
                            assertThat(actualCell.getStringCellValue())
                                    .isEqualTo(expectedCell.getStringCellValue());
                        }
                    }
                }
            }
        }
    } catch (AssertionError error) {
        describedAs(error.getMessage());
        result = false;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return result;
}

From source file:com.runwaysdk.dataaccess.io.excel.AttributeColumn.java

License:Open Source License

/**
 * Excel contains several different types of cells, with different getters.
 * This method checks the expected type, calls the appropriate getter on the
 * cell, then wraps the result in the correct java type for use in the
 * typesafe setter methods./*from   w ww. j a  va2s .com*/
 * 
 * @param cell
 * @param column
 * @return
 */
public Object getValue(Cell cell) throws Exception {
    String type = this.javaType();

    if (this.isEnum()) {
        String cellValue = ExcelUtil.getString(cell);
        Class<?> enumClass = LoaderDecorator.load(type);

        BusinessEnumeration[] values = (BusinessEnumeration[]) enumClass.getMethod("values").invoke(null);
        for (BusinessEnumeration value : values) {
            if (cellValue.equalsIgnoreCase(value.getDisplayLabel())) {
                return value;
            }
        }

        // We did not find a matching enum value. That is a problem.
        MdAttributeEnumerationDAO mdAttribute = (MdAttributeEnumerationDAO) this.getMdAttribute()
                .getMdAttributeConcrete();
        throw new InvalidEnumerationName("devMessage", cellValue, mdAttribute.getMdEnumerationDAO());
    }

    /*
     * Check for null values
     */
    if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
        return null;
    } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        switch (cell.getCachedFormulaResultType()) {
        case Cell.CELL_TYPE_STRING:
            String value = cell.getRichStringCellValue().getString();

            if (value == null || value.length() == 0) {
                return null;
            }

            break;
        case Cell.CELL_TYPE_BLANK:
            return null;
        }
    } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        String value = cell.getRichStringCellValue().getString();

        if (value == null || value.length() == 0) {
            return null;
        }
    }

    if (type.equals(String.class.getName())) {
        return ExcelUtil.getString(cell);
    } else if (type.equals(Long.class.getName())) {
        return new Long(new Double(cell.getNumericCellValue()).longValue());
    } else if (type.equals(Float.class.getName())) {
        return new Float(new Double(cell.getNumericCellValue()).floatValue());
    } else if (type.equals(Double.class.getName())) {
        return new Double(cell.getNumericCellValue());
    } else if (type.equals(BigDecimal.class.getName())) {
        return new BigDecimal(cell.getNumericCellValue());
    } else if (type.equals(Integer.class.getName())) {
        return new Integer(new Double(cell.getNumericCellValue()).intValue());
    } else if (type.equals(Boolean.class.getName())) {
        return ExcelUtil.getBoolean(cell,
                (MdAttributeBooleanDAOIF) this.getMdAttribute().getMdAttributeConcrete());
    } else if (type.equals(java.util.Date.class.getName())) {
        return cell.getDateCellValue();
    }
    String error = "The type [" + type + "] is not supported as a parameter.";
    throw new ProgrammingErrorException(error);
}

From source file:com.runwaysdk.dataaccess.io.excel.DecimalFieldColumn.java

License:Open Source License

public Object getCellValue(Cell cell) throws Exception {
    return new BigDecimal(cell.getNumericCellValue());
}

From source file:com.runwaysdk.dataaccess.io.excel.DoubleFieldColumn.java

License:Open Source License

public Object getCellValue(Cell cell) throws Exception {
    return new Double(cell.getNumericCellValue());
}

From source file:com.runwaysdk.dataaccess.io.excel.ErrorSheet.java

License:Open Source License

public void addRow(Row _row) {
    Row row = this.errorSheet.createRow(count++);
    row.setZeroHeight(_row.getZeroHeight());
    row.setHeight(_row.getHeight());/*w  w  w .j a  va 2s  . com*/

    CellStyle style = _row.getRowStyle();

    if (style != null) {
        Workbook workbook = row.getSheet().getWorkbook();

        CellStyle clone = workbook.createCellStyle();
        clone.cloneStyleFrom(style);

        row.setRowStyle(clone);
    }

    Iterator<Cell> cellIterator = _row.cellIterator();
    while (cellIterator.hasNext()) {
        Cell oldCell = cellIterator.next();
        Cell newCell = row.createCell(oldCell.getColumnIndex());

        int cellType = oldCell.getCellType();

        if (cellType == Cell.CELL_TYPE_FORMULA) {
            cellType = oldCell.getCachedFormulaResultType();
        }

        switch (cellType) {
        case Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        }
    }
}