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:utilities.XLSTaskManager.java

License:Open Source License

private Timestamp getDateColumn(Row row, String name, HashMap<String, Integer> header, int lastCellNum,
        String def) throws Exception {

    Integer cellIndex;//from   w w  w  . ja va2 s.co  m
    int idx;
    Timestamp tsValue = null;

    cellIndex = header.get(name);
    if (cellIndex != null) {
        idx = cellIndex;
        if (idx <= lastCellNum) {
            Cell c = row.getCell(idx);
            if (c != null) {
                log.info("Get date column: " + name);
                if (c.getCellType() == CellType.NUMERIC) {
                    if (HSSFDateUtil.isCellDateFormatted(c)) {
                        tsValue = new Timestamp(c.getDateCellValue().getTime());
                    }
                }
            }
        }
    } else {
        throw new Exception("Column " + name + " not found");
    }

    return tsValue;
}

From source file:workbench.db.importer.ExcelReader.java

License:Apache License

private java.util.Date getDateValue(Cell cell) {
    java.util.Date dtValue = null;
    try {/*from   w  w  w  .  j a  v  a2 s . c  o  m*/
        dtValue = cell.getDateCellValue();
    } catch (Exception ex) {
        // ignore
    }
    String fmt = cell.getCellStyle().getDataFormatString();
    double dv = cell.getNumericCellValue();
    if (dtValue == null) {
        dtValue = getJavaDate(dv);
    }

    if (dtValue != null) {
        if (isTimestampFormat(fmt)) {
            return new java.sql.Timestamp(dtValue.getTime());
        } else {
            return new java.sql.Date(dtValue.getTime());
        }
    }
    return null;
}

From source file:XlsUtils.XlsComparator.java

/**
 * Obtiene el valor de una Cell de Excel
 * @param cell//from  w w w  .  j  av a2  s.  c  o m
 * @return 
 */
public static Object getCellValue(Cell cell) {
    Object result = null;

    if (cell != null) {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                result = cell.getDateCellValue();
            } else {
                if (cell.getNumericCellValue() == (int) cell.getNumericCellValue()) {
                    result = (int) cell.getNumericCellValue();
                } else {
                    result = cell.getNumericCellValue();
                }
            }
            break;
        case Cell.CELL_TYPE_STRING:
            result = cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            result = cell.getBooleanCellValue();
            break;
        case Cell.CELL_TYPE_FORMULA:
        case Cell.CELL_TYPE_BLANK:
        case Cell.CELL_TYPE_ERROR:
            result = null;
            break;
        }
    }

    return result;
}