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.shareok.data.msofficedata.ExcelHandler.java

private boolean isCellDateFormatted(Cell cell) throws Exception {
    try {//from ww  w .j a va  2s  .c  o m
        return DateUtil.isCellDateFormatted(cell);
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new Exception("The cell type data formatted cannot be decided!");
    }
}

From source file:org.springframework.batch.item.excel.poi.ArrayPoiSheet.java

License:Apache License

/**
 * {@inheritDoc}//from  ww  w.ja  v a 2s.  co m
 */
@Override
public Object[] getRow(final int rowNumber) {
    final Row row = this.delegate.getRow(rowNumber);
    if (row == null) {
        return null;
    }
    final List<Object> cells = new LinkedList<Object>();

    for (int i = 0; i < getNumberOfColumns(); i++) {
        Cell cell = row.getCell(i);
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                cells.add(cell.getDateCellValue());
            } else {
                cells.add(cell.getNumericCellValue());
            }
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            cells.add(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
        case Cell.CELL_TYPE_BLANK:
            cells.add(cell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            cells.add(getFormulaEvaluator().evaluate(cell).formatAsString());
            break;
        default:
            throw new IllegalArgumentException("Cannot handle cells of type " + cell.getCellType());
        }
    }
    return cells.toArray(new Object[cells.size()]);
}

From source file:org.sysmodb.CellInfo.java

License:BSD License

private void readCellValueAndType(CellType cellType, Cell cell) {
    switch (cellType) {
    case BLANK://ww  w  .j  a  v  a 2  s.c  o  m
        value = "";
        type = "blank";
        break;
    case BOOLEAN:
        value = String.valueOf(cell.getBooleanCellValue());
        type = "boolean";
        break;
    case NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            type = "datetime";
            Date dateCellValue = cell.getDateCellValue();
            value = dateFormatter.format(dateCellValue);
        } else {
            double numericValue = cell.getNumericCellValue();
            int intValue = (int) numericValue;
            if (intValue == numericValue) {
                value = String.valueOf(intValue);
            } else {
                value = String.valueOf(numericValue);
            }
            type = "numeric";
        }
        break;
    case STRING:
        value = cell.getStringCellValue();
        type = "string";
        break;
    case FORMULA:
        try {
            formula = cell.getCellFormula();
        } catch (FormulaParseException e) {

        }
        CellType resultCellType = cell.getCachedFormulaResultTypeEnum();
        readCellValueAndType(resultCellType, cell);
        break;
    default:
        value = "";
        type = "none";
        break;
    }
}

From source file:org.teiid.translator.excel.BaseExcelExecution.java

License:Apache License

Object convertFromExcelType(final Double value, Cell cell, final Class<?> expectedType)
        throws TranslatorException {
    if (value == null) {
        return null;
    }//from  ww  w.  ja v  a 2s.com

    if (expectedType.isAssignableFrom(Double.class)) {
        return value;
    } else if (expectedType.isAssignableFrom(Timestamp.class)) {
        Date date = cell.getDateCellValue();
        return new Timestamp(date.getTime());
    } else if (expectedType.isAssignableFrom(java.sql.Date.class)) {
        Date date = cell.getDateCellValue();
        return TimestampWithTimezone.createDate(date);
    } else if (expectedType.isAssignableFrom(java.sql.Time.class)) {
        Date date = cell.getDateCellValue();
        return TimestampWithTimezone.createTime(date);
    }

    if (expectedType == String.class && dataFormatter != null) {
        return dataFormatter.formatCellValue(cell);
    }

    Object val = value;

    if (DateUtil.isCellDateFormatted(cell)) {
        Date date = cell.getDateCellValue();
        val = new java.sql.Timestamp(date.getTime());
    }

    try {
        return DataTypeManager.transformValue(val, expectedType);
    } catch (TransformationException e) {
        throw new TranslatorException(e);
    }
}

From source file:org.tiefaces.components.websheet.utility.CellUtility.java

License:MIT License

/**
 * Gets the cell string value with number type.
 *
 * @param poiCell/*from   ww  w .jav  a 2  s .c  o m*/
 *            the poi cell
 * @return the cell string value with number type
 */
private static String getCellStringValueWithNumberType(final Cell poiCell) {
    String result;
    if (DateUtil.isCellDateFormatted(poiCell)) {
        result = poiCell.getDateCellValue().toString();
    } else {
        result = BigDecimal.valueOf(poiCell.getNumericCellValue()).toPlainString();
        // remove .0 from end for int
        if (result.endsWith(".0")) {
            result = result.substring(0, result.length() - 2);
        }
    }
    return result;
}

From source file:org.unhcr.eg.odk.utilities.xlsform.controller.SheetProcessor.java

public static Object getCelValue(Cell cell) {
    Object cellValue = null;//w ww  . j  a v a 2s.c  o  m
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN:
        cellValue = cell.getBooleanCellValue();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            cellValue = cell.getDateCellValue();
        } else {
            cellValue = cell.getNumericCellValue();
        }
        break;
    case Cell.CELL_TYPE_STRING:
        cellValue = cell.getStringCellValue();
        break;
    case Cell.CELL_TYPE_BLANK:
        cellValue = new String("");
        break;
    case Cell.CELL_TYPE_ERROR:
        cellValue = cell.getErrorCellValue();
        break;
    case Cell.CELL_TYPE_FORMULA:
        cellValue = cell.getCellFormula();
        break;
    }
    return cellValue;
}

From source file:org.unhcr.eg.odk.utilities.xlsform.controller.SheetProcessor.java

public static Object getWithIntCelValue(Cell cell) {
    Object cellValue = null;//from w ww .  j  av  a 2  s.  c  om
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN:
        cellValue = cell.getBooleanCellValue();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            cellValue = cell.getDateCellValue();
        } else {
            cellValue = cell.getNumericCellValue();
            Double i = (Double) cellValue;
            cellValue = i.intValue();
        }
        break;
    case Cell.CELL_TYPE_STRING:
        cellValue = cell.getStringCellValue();
        break;
    case Cell.CELL_TYPE_BLANK:
        cellValue = new String("");
        break;
    case Cell.CELL_TYPE_ERROR:
        cellValue = cell.getErrorCellValue();
        break;
    case Cell.CELL_TYPE_FORMULA:
        cellValue = cell.getCellFormula();
        break;
    }
    return cellValue;
}

From source file:org.unhcr.eg.odk.utilities.xlsform.excel.ExcelFileUtility.java

protected static void copyContent(Sheet sheetSource, Sheet sheetDestination) {
    //Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheetSource.iterator();
    int i = 0;/*from   w  w  w .j a  va2s  . com*/
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Row rowDestination = sheetDestination.createRow(i);
        i++;
        //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        int j = 0;
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            Cell cellDestination = rowDestination.createCell(j);
            j++;
            cellDestination.setCellComment(cell.getCellComment());
            //                cellDestination.setCellStyle(cell.getCellStyle());
            cellDestination.setCellType(cell.getCellType());
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                cellDestination.setCellValue(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    cellDestination.setCellValue(cell.getDateCellValue());
                } else {
                    cellDestination.setCellValue(cell.getNumericCellValue());
                }
                break;
            case Cell.CELL_TYPE_STRING:
                cellDestination.setCellValue(cell.getRichStringCellValue());
                break;
            case Cell.CELL_TYPE_BLANK:
                cellDestination.setCellValue(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_ERROR:
                cellDestination.setCellValue(cell.getErrorCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                cellDestination.setCellFormula(cell.getCellFormula());
                break;
            }

        }

    }

}

From source file:org.wandora.application.tools.extractors.excel.AbstractExcelExtractor.java

License:Open Source License

protected String getCellValueAsString(Cell cell, int type) {
    if (cell != null) {
        switch (type) {
        case Cell.CELL_TYPE_ERROR: {
            return "ERROR" + cell.getErrorCellValue();
        }//w  w w  .j av a  2  s .  c  om
        case Cell.CELL_TYPE_BOOLEAN: {
            return "" + cell.getBooleanCellValue();
        }
        case Cell.CELL_TYPE_NUMERIC: {
            if (DateUtil.isCellDateFormatted(cell)) {
                return dateFormat.format(cell.getDateCellValue());
            } else {
                double value = cell.getNumericCellValue();
                String formatString = cell.getCellStyle().getDataFormatString();
                int formatIndex = cell.getCellStyle().getDataFormat();
                return formatter.formatRawCellContents(value, formatIndex, formatString);
            }
        }
        case Cell.CELL_TYPE_STRING: {
            return cell.getRichStringCellValue().getString();
        }
        }
    }
    return null;
}

From source file:plugins.excel.client.util.ExcelReader.java

License:Microsoft Reference Source License

private HashMap<String, Integer> getColumnTypes(Sheet sheet, ArrayList<String> columnNames,
        boolean columnNamesInFirstLine) {
    Cell c = null;/*from w w  w . j  a  v  a2  s .  c o  m*/
    int type = Types.NULL;
    boolean stopFor = false;
    HashMap<String, Integer> types = new HashMap<String, Integer>();

    for (int i = 0; i < columnNames.size(); i++) {
        type = Types.NULL;
        stopFor = false;
        for (int j = sheet.getFirstRowNum() + (columnNamesInFirstLine ? 1 : 0); j <= sheet
                .getLastRowNum(); j++) {
            c = sheet.getRow(j).getCell(i);
            if (c != null) {
                switch (c.getCellType()) {
                case (Cell.CELL_TYPE_STRING):
                case (Cell.CELL_TYPE_FORMULA):
                    type = Types.VARCHAR;
                    stopFor = true;
                    break;
                case (Cell.CELL_TYPE_NUMERIC):
                    if (DateUtil.isCellDateFormatted(c)) {
                        if (type != Types.VARCHAR && type != Types.DOUBLE) {
                            type = Types.DATE;
                        }
                    } else {
                        if (type != Types.VARCHAR) {
                            type = Types.DOUBLE;
                        }
                    }
                    break;
                case (Cell.CELL_TYPE_ERROR):
                    if (type == Types.NULL || type == Types.BOOLEAN) {
                        type = Types.INTEGER;
                    }
                    break;
                case (Cell.CELL_TYPE_BOOLEAN):
                    if (type == Types.NULL) {
                        type = Types.BOOLEAN;
                    }
                    break;
                }
            }

            if (stopFor) {
                break;
            }
        }

        types.put(columnNames.get(i), type);
    }
    return types;
}