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

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

Introduction

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

Prototype

Date getDateCellValue();

Source Link

Document

Get the value of the cell as a date.

Usage

From source file:org.squashtest.tm.service.internal.batchimport.excel.OptionalDateCellCoercer.java

License:Open Source License

/**
 * @see org.squashtest.tm.service.internal.batchimport.excel.TypeBasedCellValueCoercer#coerceNumericCell(org.apache.poi.ss.usermodel.Cell)
 *///from  w w w. j a va 2s . com
@Override
protected Date coerceNumericCell(Cell cell) {
    try {
        return cell.getDateCellValue();
    } catch (IllegalArgumentException e) {
        throw new CannotCoerceException(e, Messages.ERROR_UNPARSABLE_DATE, Messages.IMPACT_FIELD_NOT_CHANGED,
                Messages.IMPACT_USE_CURRENT_DATE);
    }
}

From source file:org.squashtest.tm.service.internal.importer.ExcelRowReaderUtils.java

License:Open Source License

/**
 * If the cell is of numeric type : will read the Date value of the cell. <br>
 * If the cell is of String type : will try to parse the date with the format "dd/MM/yyyy" and return null if the parsing failed
 * @param row : the concerned row/*from ww w.j  a v  a 2 s . co m*/
 * @param columnsMapping : a map linking the header name to the column index
 * @param tag : the column header
 * @return a <code>Date</code> or <code>null</code> it the cell is not numerical or a text that can be parsed into a date
 */
public static Date readDateField(Row row, Map<String, Integer> columnsMapping, String tag) {
    Cell cell = accessToCell(row, columnsMapping, tag);
    Date toReturn = null;
    if (cell != null) {
        int type = cell.getCellType();
        if (type == Cell.CELL_TYPE_NUMERIC) {
            toReturn = cell.getDateCellValue();

        } else {
            if (type == Cell.CELL_TYPE_STRING) {
                String dateS = cell.getStringCellValue();

                try {
                    toReturn = new SimpleDateFormat("dd/MM/yyyy").parse(dateS);
                } catch (ParseException e) {
                    LOGGER.warn(e.getMessage());
                }
            }
        }
    }
    return toReturn;
}

From source file:org.sysmodb.CellInfo.java

License:BSD License

private void readCellValueAndType(CellType cellType, Cell cell) {
    switch (cellType) {
    case BLANK:/*from  ww w.  ja  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.talend.dataprep.schema.xls.XlsUtils.java

License:Open Source License

/**
 * Return the numeric value.//from  w w w  .j  a va 2 s. c o m
 *
 * @param cell the cell to extract the value from.
 * @return the numeric value from the cell.
 */
private static String getNumericValue(Cell cell, CellValue cellValue, boolean fromFormula) {
    // Date is typed as numeric
    if (HSSFDateUtil.isCellDateFormatted(cell)) { // TODO configurable??
        DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
        return sdf.format(cell.getDateCellValue());
    }
    // Numeric type (use data formatter to get number format right)
    DataFormatter formatter = new HSSFDataFormatter(Locale.ENGLISH);

    if (cellValue == null) {
        return formatter.formatCellValue(cell);
    }

    return fromFormula ? cellValue.formatAsString() : formatter.formatCellValue(cell);
}

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;
    }/*  w ww . ja va 2  s  . c  o m*/

    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.teiid.translator.excel.ExcelExecution.java

License:Open Source License

static Object convertFromExcelType(final Double value, Cell cell, final Class<?> expectedType)
        throws TranslatorException {
    if (value == null) {
        return null;
    }// www.ja v  a  2  s .  c o m

    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 new java.sql.Date(date.getTime());
    } else if (expectedType.isAssignableFrom(java.sql.Time.class)) {
        Date date = cell.getDateCellValue();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        StringBuilder sb = new StringBuilder();
        sb.append(calendar.get(Calendar.HOUR_OF_DAY)).append(":") //$NON-NLS-1$
                .append(calendar.get(Calendar.MINUTE)).append(":") //$NON-NLS-1$
                .append(calendar.get(Calendar.SECOND));
        return java.sql.Time.valueOf(sb.toString());
    }

    if (DataTypeManager.isTransformable(double.class, expectedType)) {
        try {
            return DataTypeManager.transformValue(value, expectedType);
        } catch (TransformationException e) {
            throw new TranslatorException(e);
        }
    }
    throw new TranslatorException(ExcelPlugin.Event.TEIID23002,
            ExcelPlugin.Util.gs(ExcelPlugin.Event.TEIID23002, expectedType.getName()));
}

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

License:MIT License

/**
 * Gets the cell string value with number type.
 *
 * @param poiCell//  w  ww .  ja  va  2s  . 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;/*from  w w w . ja va  2 s  .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  a v  a2s  .  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();
            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   ww  w.ja  va 2s.c o m*/
    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;
            }

        }

    }

}