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.runwaysdk.dataaccess.io.excel.ExcelUtil.java

License:Open Source License

private static Boolean getBooleanInternal(Cell cell, String positiveLabel, String negativeLabel) {
    if (cell == null) {
        return Boolean.FALSE;
    }//w w w  . j av  a  2s  . c om

    int cellType = cell.getCellType();
    // In the case of formula, find out what type the formula will produce
    if (cellType == Cell.CELL_TYPE_FORMULA) {
        cellType = cell.getCachedFormulaResultType();
    }

    if (cellType == Cell.CELL_TYPE_STRING) {
        String value = cell.getRichStringCellValue().getString().trim();

        if (value.equalsIgnoreCase(positiveLabel) || value.equalsIgnoreCase("y")
                || value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("t")
                || value.equalsIgnoreCase("true") || value.equalsIgnoreCase("x")) {
            return Boolean.TRUE;
        }
        if (value.equalsIgnoreCase(negativeLabel) || value.equalsIgnoreCase("false")
                || value.equalsIgnoreCase("f") || value.equalsIgnoreCase("no") || value.equalsIgnoreCase("n")
                || value.length() == 0) {
            return Boolean.FALSE;
        }

        throw new AttributeValueException("[" + value + "] is not a recognized boolean in excel", value);
    } else if (cellType == Cell.CELL_TYPE_NUMERIC) {
        Double value = new Double(cell.getNumericCellValue());

        return value.equals(new Double(1));
    } else {
        return Boolean.valueOf(cell.getBooleanCellValue());
    }
}

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

License:Open Source License

public static String getString(Cell cell) {
    if (cell == null) {
        return null;
    }//from w  w  w .  j a  v  a2  s.c o m

    int cellType = cell.getCellType();

    // In the case of formula, find out what type the formula will produce
    if (cellType == Cell.CELL_TYPE_FORMULA) {
        cellType = cell.getCachedFormulaResultType();
    }

    if (cellType == Cell.CELL_TYPE_BLANK) {
        return "";
    } else if (cellType == Cell.CELL_TYPE_NUMERIC) {
        return (new BigDecimal(cell.getNumericCellValue())).toString();
    } else if (cellType == Cell.CELL_TYPE_BOOLEAN) {
        return new Boolean(cell.getBooleanCellValue()).toString();
    } else {
        return cell.getRichStringCellValue().getString().trim();
    }
}

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

License:Open Source License

public static Integer getInteger(Cell cell) {
    if (cell == null) {
        return null;
    }/* w w  w  . j  a va 2 s  .c o m*/

    int cellType = cell.getCellType();

    // In the case of formula, find out what type the formula will produce
    if (cellType == Cell.CELL_TYPE_FORMULA) {
        cellType = cell.getCachedFormulaResultType();
    }

    if (cellType == Cell.CELL_TYPE_BLANK) {
        return null;
    } else if (cellType == Cell.CELL_TYPE_NUMERIC) {
        return (new BigDecimal(cell.getNumericCellValue())).intValue();
    } else {
        return Integer.parseInt(cell.getRichStringCellValue().getString().trim());
    }
}

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

License:Open Source License

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

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

License:Open Source License

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

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

License:Open Source License

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

From source file:com.runwaysdk.dataaccess.io.ExcelImporter.java

License:Open Source License

/**
 * Checks to see if the given row has specified at least one column with a value
 * //from  w w w. j  a v a2 s. c  om
 * @param row
 * @return
 */
private boolean rowHasValues(Row row) {
    Iterator<Cell> cellIterator = row.cellIterator();

    while (cellIterator.hasNext()) {
        Cell cell = cellIterator.next();
        int cellType = cell.getCellType();

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

        Object value = null;

        switch (cellType) {
        case Cell.CELL_TYPE_STRING:
            value = ExcelUtil.getString(cell);
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            value = ExcelUtil.getBoolean(cell);
            break;
        case Cell.CELL_TYPE_NUMERIC:
            value = cell.getNumericCellValue();
            break;
        }

        if (value == null) {
            continue;
        }

        if (value.toString().trim().length() > 0) {
            return true;
        }
    }
    return false;
}

From source file:com.salahatwa.randomme.ReadXLS.java

/**
 * @param filePath/*from   w ww  . j  a v  a  2s. co  m*/
 * @return  list of Readed cells from xlsx
 */
public List<ReadedBean> readXLSFromFile(String filePath) {
    List<ReadedBean> data = new ArrayList();
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(filePath);
        // Using XSSF for xlsx format, for xls use HSSF

        Workbook workbook = new XSSFWorkbook(fis);

        int numberOfSheets = workbook.getNumberOfSheets();

        //looping over each workbook sheet
        for (int i = 0; i < numberOfSheets; i++) {
            Sheet sheet = workbook.getSheetAt(i);
            Iterator rowIterator = sheet.iterator();

            //iterating over each row
            while (rowIterator.hasNext()) {

                ReadedBean readedBean = new ReadedBean();
                Row row = (Row) rowIterator.next();
                Iterator cellIterator = row.cellIterator();
                //Iterating over each cell (column wise)  in a particular row.
                while (cellIterator.hasNext()) {
                    Cell cell = (Cell) cellIterator.next();

                    if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
                        if (cell.getColumnIndex() == 0) {
                            readedBean.setCell(cell.getStringCellValue());
                        }

                    } else if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {

                        if (cell.getColumnIndex() == 0) {
                            readedBean.setCell(String.valueOf((int) cell.getNumericCellValue()));
                        }
                        //                           
                    }
                }
                System.out.println(readedBean.getCell());
                data.add(readedBean);
            }
        }
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return data;
}

From source file:com.ssy.havefun.f3d.F3DDaoImpl.java

public String getCellValue(Cell cell) {
    String ret = "";
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        ret = "";
        break;/*from  w  w w .  ja  v a  2 s.  c  om*/
    case Cell.CELL_TYPE_BOOLEAN:
        ret = String.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        ret = null;
        break;
    case Cell.CELL_TYPE_FORMULA:
        Workbook wb = cell.getSheet().getWorkbook();
        CreationHelper crateHelper = wb.getCreationHelper();
        FormulaEvaluator evaluator = crateHelper.createFormulaEvaluator();
        ret = getCellValue(evaluator.evaluateInCell(cell));
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            //                Date theDate = cell.getDateCellValue();  
            //                ret = simpleDateFormat.format(theDate);  
        } else {
            ret = NumberToTextConverter.toText(cell.getNumericCellValue());
        }
        break;
    case Cell.CELL_TYPE_STRING:
        ret = cell.getRichStringCellValue().getString();
        break;
    default:
        ret = null;
    }

    return ret; //?trim  
}

From source file:com.streamsets.pipeline.lib.parser.excel.Cells.java

License:Apache License

static Field parseCell(Cell cell, FormulaEvaluator evaluator) throws ExcelUnsupportedCellTypeException {
    CellType cellType = cell.getCellTypeEnum();
    // set the cellType of a formula cell to its cached formula result type in order to process it as its result type
    boolean isFormula = cell.getCellTypeEnum().equals(CellType.FORMULA);
    if (isFormula) {
        cellType = cell.getCachedFormulaResultTypeEnum();
    }// w w w  . java 2s .c  o m

    switch (cellType) {
    case STRING:
        return Field.create(cell.getStringCellValue());
    case NUMERIC:
        Double rawValue = cell.getNumericCellValue(); // resolves formulas automatically and gets value without cell formatting
        String displayValue = isFormula ? evaluator.evaluate(cell).formatAsString()
                : dataFormatter.formatCellValue(cell);
        boolean numericallyEquivalent = false;
        try {
            numericallyEquivalent = Double.parseDouble(displayValue) == rawValue;
        } catch (NumberFormatException e) {
        }

        if (DateUtil.isCellDateFormatted(cell)) {
            // It's a date, not a number
            java.util.Date dt = cell.getDateCellValue();
            // if raw number is < 1 then it's a time component only, otherwise date.
            return rawValue < 1 ? Field.createTime(dt) : Field.createDate(dt);
        }

        // some machinations to handle integer values going in without decimal vs. with .0 for rawValue
        return Field
                .create(numericallyEquivalent ? new BigDecimal(displayValue) : BigDecimal.valueOf(rawValue));

    case BOOLEAN:
        return Field.create(cell.getBooleanCellValue());
    case BLANK:
        return Field.create("");
    default:
        throw new ExcelUnsupportedCellTypeException(cell, cellType);
    }
}