Example usage for org.apache.poi.ss.usermodel DateUtil isCellDateFormatted

List of usage examples for org.apache.poi.ss.usermodel DateUtil isCellDateFormatted

Introduction

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

Prototype

public static boolean isCellDateFormatted(Cell cell) 

Source Link

Document

Check if a cell contains a date Since dates are stored internally in Excel as double values we infer it is a date if it is formatted as such.

Usage

From source file:org.drools.scorecards.parser.xls.XLSScorecardParser.java

License:Apache License

private void processSheet(HSSFSheet worksheet) throws ScorecardParseException {
    for (Row row : worksheet) {
        int currentRowCtr = row.getRowNum();
        excelDataCollector.newRow(currentRowCtr);
        for (Cell cell : row) {
            int currentColCtr = cell.getColumnIndex();
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                excelDataCollector.newCell(currentRowCtr, currentColCtr, cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    excelDataCollector.newCell(currentRowCtr, currentColCtr, cell.getDateCellValue());
                } else {
                    excelDataCollector.newCell(currentRowCtr, currentColCtr,
                            Double.valueOf(cell.getNumericCellValue()));
                }// w w  w  . ja v a2 s . c  om
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                excelDataCollector.newCell(currentRowCtr, currentColCtr,
                        Boolean.valueOf(cell.getBooleanCellValue()).toString());
                break;
            case Cell.CELL_TYPE_FORMULA:
                break;
            case Cell.CELL_TYPE_BLANK:
                excelDataCollector.newCell(currentRowCtr, currentColCtr, "");
                break;
            }
        }
    }
}

From source file:org.eclipse.lyo.samples.excel.adapter.dao.internal.ExcelDaoImpl.java

License:Open Source License

private String getCellValue(Cell cell) {
    if (cell != null) {
        String value = null;/*from ww w. j  ava 2  s .  com*/
        int type = cell.getCellType();
        if (type == Cell.CELL_TYPE_STRING) {
            value = cell.getStringCellValue();
        } else if (type == Cell.CELL_TYPE_NUMERIC) {
            if (DateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                value = FastDateFormat.getInstance(DEFAULT_OUTPUT_DATE_FORMAT).format(date);
            } else {
                double d = cell.getNumericCellValue();
                if (d == Math.floor(d)) { // need to consider when d is negative
                    value = "" + (int) d;
                } else {
                    value = "" + cell.getNumericCellValue();
                }
            }
        } else if (type == Cell.CELL_TYPE_FORMULA) {
            // get calculated value if the cell type is formula 
            Workbook wb = cell.getSheet().getWorkbook();
            CreationHelper crateHelper = wb.getCreationHelper();
            FormulaEvaluator evaluator = crateHelper.createFormulaEvaluator();
            // get recursively if the value is still formula 
            value = getCellValue(evaluator.evaluateInCell(cell));
        }
        return value;
    }
    return null;
}

From source file:org.formulacompiler.spreadsheet.internal.excel.xls.loader.ExcelXLSLoader.java

License:Open Source License

private Object getNumberValue(Cell _xlsCell) {
    final double value = _xlsCell.getNumericCellValue();
    final boolean isDate = DateUtil.isCellDateFormatted(_xlsCell);
    final boolean isTime = isTime(_xlsCell);
    if (isDate || isTime) {
        if (null != this.globalTimeFormat
                && this.globalTimeFormat.equals(_xlsCell.getCellStyle().getDataFormatString())) {
            return RuntimeDouble_v2.dateFromNum(_xlsCell.getNumericCellValue(), this.globalTimeZone,
                    ComputationMode.EXCEL);
        }//from   w  w  w .  ja va  2  s . co m
        if ((isDate && value < 1) || (isTime && value < 365)) {
            return new Duration(value);
        } else {
            return new LocalDate(value);
        }
    } else {
        return value;
    }
}

From source file:org.generationcp.middleware.util.PoiUtil.java

License:Open Source License

private static Object getNumericValue(final Cell cell) {
    if (DateUtil.isCellDateFormatted(cell)) {
        final Date date = cell.getDateCellValue();
        return PoiUtil.EXCEL_DATE_FORMATTER.format(date);
    }//from   w ww.  ja  v  a2s.c  o m

    final double doubleVal = cell.getNumericCellValue();
    if (doubleVal % 1 == 0) {
        return (int) doubleVal;
    } else {
        return doubleVal;
    }
}

From source file:org.hellojavaer.poi.excel.utils.ExcelUtils.java

License:Apache License

private static Object _readCell(Cell cell) {
    if (cell == null) {
        return null;
    }//from  w  w w . ja va2  s.co  m
    int cellType = cell.getCellType();
    Object value = null;
    switch (cellType) {
    case Cell.CELL_TYPE_BLANK:
        value = null;
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        boolean bool = cell.getBooleanCellValue();
        value = bool;
        break;
    case Cell.CELL_TYPE_ERROR:
        // cell.getErrorCellValue();
        ExcelReadException e = new ExcelReadException("Cell type error");
        e.setRowIndex(cell.getRowIndex());
        e.setColIndex(cell.getColumnIndex());
        e.setCode(ExcelReadException.CODE_OF_CELL_ERROR);
        throw e;
    case Cell.CELL_TYPE_FORMULA:
        value = cell.getCellFormula();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        Object inputValue = null;//
        double doubleVal = cell.getNumericCellValue();
        if (DateUtil.isCellDateFormatted(cell)) {
            inputValue = DateUtil.getJavaDate(doubleVal);
        } else {
            long longVal = Math.round(cell.getNumericCellValue());
            if (Double.parseDouble(longVal + ".0") == doubleVal) {
                inputValue = longVal;
            } else {
                inputValue = doubleVal;
            }
        }
        value = inputValue;
        break;
    case Cell.CELL_TYPE_STRING:
        value = cell.getStringCellValue();
        break;
    default:
        throw new RuntimeException("unsupport cell type " + cellType);
    }
    return value;
}

From source file:org.isource.util.CSVUtils.java

private static List<List> readWorkbook(HSSFWorkbook workbook) {

    List<List> lines = new ArrayList<List>();

    workbook = evaluateFormulas(workbook);

    HSSFSheet sheet = workbook.getSheetAt(0);

    Iterator<Row> rowIterator = sheet.iterator();

    while (rowIterator.hasNext()) {

        Row row = rowIterator.next();/* ww w  .  jav a  2s. c o m*/

        List<String> line = new ArrayList<String>();

        //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();

        while (cellIterator.hasNext()) {

            Cell cell = cellIterator.next();

            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                line.add(new Boolean(cell.getBooleanCellValue()).toString());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
                    line.add(dateFormat.format(cell.getDateCellValue()));
                } else {
                    line.add(new Double(cell.getNumericCellValue()).toString());
                }
                break;
            case Cell.CELL_TYPE_STRING:
                line.add(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                switch (cell.getCachedFormulaResultType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    line.add(new Double(cell.getNumericCellValue()).toString());
                    break;
                case Cell.CELL_TYPE_STRING:
                    line.add(cell.getRichStringCellValue().toString());
                    break;
                }
                break;
            }
        }

        lines.add(line);
    }

    return lines;
}

From source file:org.jberet.support.io.ExcelUserModelItemReader.java

License:Open Source License

protected Object getCellValue(final Cell c, final int cellType) {
    final Object cellValue;
    switch (cellType) {
    case Cell.CELL_TYPE_STRING:
        cellValue = c.getStringCellValue();
        break;/* w ww  .j a  v a2s.com*/
    case Cell.CELL_TYPE_BOOLEAN:
        cellValue = c.getBooleanCellValue();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        cellValue = DateUtil.isCellDateFormatted(c) ? c.getDateCellValue() : c.getNumericCellValue();
        break;
    case Cell.CELL_TYPE_BLANK:
        cellValue = null;
        break;
    case Cell.CELL_TYPE_FORMULA:
        if (formulaEvaluator == null) {
            formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
        }
        formulaEvaluator.evaluateFormulaCell(c);
        cellValue = getCellValue(c, c.getCachedFormulaResultType());
        break;
    default:
        cellValue = c.getStringCellValue();
        break;
    }
    return cellValue;
}

From source file:org.joeffice.spreadsheet.cell.CellUtils.java

License:Apache License

public static String getFormattedText(Cell cell) {
    if (cell == null) {
        return "";
    }/*from   w ww . java  2s. c o m*/
    int type = cell.getCellType();
    if (type == Cell.CELL_TYPE_STRING) {
        return cell.getStringCellValue();
    } else if (type == Cell.CELL_TYPE_NUMERIC) {
        if (DateUtil.isCellDateFormatted(cell)) {
            return DATE_FORMATTER.format(cell.getDateCellValue());
        } else {
            return NUMBER_FORMATTER.format(cell.getNumericCellValue());
        }
    } else if (type == Cell.CELL_TYPE_BOOLEAN) {
        return String.valueOf(cell.getBooleanCellValue());
    } else {
        return "";
    }
}

From source file:org.kopsox.spreadsheet.util.POIUtil.java

License:Open Source License

@SuppressWarnings("boxing")
private static final Value getValueFromNumeric(Cell cell, String formula) {

    Value ret = null;//  w w  w.ja  v a2 s .  c  o m

    //Numeric can be a Date or a Double
    if (!DateUtil.isCellDateFormatted(cell)) {
        DoubleValue tmp = new DoubleValue(cell.getNumericCellValue());
        tmp.setFormula(formula);
        ret = tmp;

    } else {
        DateValue tmp = new DateValue(cell.getDateCellValue());
        tmp.setFormula(formula);
        ret = tmp;

    }

    return ret;
}

From source file:org.mousephenotype.cda.threei.util.AnaExcelReader.java

License:Apache License

public String[] getRow() {
    if (!this.rowIterator.hasNext()) {
        this.lastRowRead = null;
        return null;
    }/*  ww  w  .  j  av  a 2s .c o m*/

    Row currentRow = this.rowIterator.next();
    Iterator<Cell> cellIterator = currentRow.iterator();
    int nColumns = this.getNumberOfColumns();

    // Get row details
    String[] resultRow = new String[nColumns];
    for (int col = 0; col < nColumns; col++) {
        if (cellIterator.hasNext()) {
            Cell currentCell = cellIterator.next();
            //getCellTypeEnum shown as deprecated for version 3.15
            //getCellTypeEnum ill be renamed to getCellType starting from version 4.0
            if (currentCell.getCellTypeEnum() == CellType.STRING) {
                resultRow[col] = "" + currentCell.getStringCellValue();
            } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
                if (DateUtil.isCellDateFormatted(currentCell)) {
                    resultRow[col] = "" + currentCell.getDateCellValue();
                } else {
                    resultRow[col] = currentCell.getNumericCellValue() + "";
                }
            } else {
                resultRow[col] = "NonStringNonNumericValue";
            }
        }
    }
    numberOfRowsRead++;
    this.lastRowRead = resultRow;
    return resultRow;
}