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:net.morphbank.mbsvc3.mapsheet.XlsFieldMapper.java

License:Open Source License

public static Date createDate(Cell cell, int colIndex, int rowIndex) {
    if (cell != null) {
        try {/*w ww.ja va  2s  .  c  o  m*/
            return cell.getDateCellValue();
        } catch (Exception e) { // show the cell coordinates

            String error = "Date format ambiguous at row " + (rowIndex + 1) + " col " + colIndex
                    + ". Cell content: " + cell.getStringCellValue();
            MorphbankConfig.SYSTEM_LOGGER.info(error);
            return parseDate(cell.getStringCellValue());
        }
    }
    return null;
}

From source file:net.pcal.sqlsheet.XlsResultSet.java

License:Apache License

public Object getObject(int jdbcColumn) throws SQLException {
    Cell cell = getCell(jdbcColumn);
    int columnType = metadata.getColumnType(jdbcColumn);
    try {/*from   w w  w.  j a  va  2 s  .c  om*/
        if (cell == null) {
            return null;
        }
        switch (cell.getCellType()) {

        case Cell.CELL_TYPE_BOOLEAN:
            if (columnType == Types.VARCHAR) {
                return cell.getBooleanCellValue();
            } else {
                throw new RuntimeException("The cell (" + getCurrentRow() + "," + jdbcColumn
                        + ") is a boolean and cannot be cast to ("
                        + XlsResultSetMetaData.columnTypeNameMap.get(columnType) + ".");
            }
        case Cell.CELL_TYPE_STRING:
            if (columnType == Types.VARCHAR) {
                return cell.getStringCellValue();
            } else {
                throw new RuntimeException("The cell (" + getCurrentRow() + "," + jdbcColumn
                        + ") is a string cell and cannot be cast to ("
                        + XlsResultSetMetaData.columnTypeNameMap.get(columnType) + ".");
            }
        case Cell.CELL_TYPE_NUMERIC:
            if (columnType == Types.VARCHAR) {
                return String.valueOf(cell.getNumericCellValue());
            } else if (columnType == Types.DOUBLE) {
                return cell.getNumericCellValue();
            } else if (columnType == Types.DATE) {
                if (DateUtil.isCellDateFormatted(cell)) {
                    java.util.Date value = cell.getDateCellValue();
                    return new java.sql.Date(value.getTime());
                }
            } else {
                throw new RuntimeException("The cell (" + getCurrentRow() + "," + jdbcColumn
                        + ") is a numeric cell and cannot be cast to ("
                        + XlsResultSetMetaData.columnTypeNameMap.get(columnType) + ".");
            }

        default:
            return null;
        }
    } catch (Exception e) {
        throw wrapped(e);
    }
}

From source file:net.pcal.sqlsheet.XlsResultSet.java

License:Apache License

public Date getDate(int jdbcColumn) throws SQLException {
    Cell cell = getCell(jdbcColumn);
    return cell == null ? null : new Date(cell.getDateCellValue().getTime());
}

From source file:net.pcal.sqlsheet.XlsResultSet.java

License:Apache License

public Date getDate(String jdbcColumn) throws SQLException {
    Cell cell = getCell(jdbcColumn);
    return cell == null ? null : new Date(cell.getDateCellValue().getTime());
}

From source file:net.pcal.sqlsheet.XlsResultSet.java

License:Apache License

public Date getDate(int jdbcColumn, Calendar cal) throws SQLException {
    Cell cell = getCell(jdbcColumn);
    if (cell == null) {
        return null;
    } else {/*from   ww  w  . j a  v a2s. c o  m*/
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(cell.getDateCellValue());
        calendar.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, cal.get(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
        return new Date(calendar.getTime().getTime());
    }
}

From source file:net.pcal.sqlsheet.XlsResultSet.java

License:Apache License

public Date getDate(String jdbcColumn, Calendar cal) throws SQLException {
    Cell cell = getCell(jdbcColumn);
    if (cell == null) {
        return null;
    } else {/*w w  w  .  ja v  a 2s .co  m*/
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(cell.getDateCellValue());
        calendar.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, cal.get(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));
        return new Date(calendar.getTime().getTime());
    }
}

From source file:net.sf.ahtutils.report.util.DataUtil.java

public static Object getCellValue(Cell cell) {
    Object value = new Object();

    // Prevent a NullPointerException
    if (cell != null) {
        if (cell.getHyperlink() != null) {
            Workbook workbook = new XSSFWorkbook();
            FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
            Hyperlink link = cell.getHyperlink();
            String address = link.getAddress();
            if (logger.isTraceEnabled()) {
                logger.trace("Found a Hyperlink to " + cell.getHyperlink().getAddress() + " in cell "
                        + cell.getRowIndex() + "," + cell.getColumnIndex());
            }/*from  w w  w .  ja va2s  . c  o m*/
            cell = evaluator.evaluateInCell(cell);
        }
        // Depending on the cell type, the value is read using Apache POI methods

        switch (cell.getCellType()) {

        // String are easy to handle
        case Cell.CELL_TYPE_STRING:
            logger.trace("Found string " + cell.getStringCellValue());
            value = cell.getStringCellValue();
            break;

        // Since date formatted cells are also of the numeric type, this needs to be processed
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                DateFormat df = SimpleDateFormat.getDateInstance();
                logger.trace("Found date " + df.format(date));
                value = date;
            } else {
                logger.trace("Found general number " + cell.getNumericCellValue());
                value = cell.getNumericCellValue();
            }
            break;
        }
    } else {
        logger.trace("Found cell with NULL value");
    }
    return value;
}

From source file:net.sf.excelutils.WorkbookUtils.java

License:Apache License

/**
 * get value of the cell//from   w  w  w . j  av  a 2s  .  c  o m
 *
 * @param sheet Sheet
 * @param rowNum int
 * @param colNum int
 * @return Date
 */
public static Date getDateCellValue(Sheet sheet, int rowNum, int colNum) {
    Row row = getRow(rowNum, sheet);
    Cell cell = getCell(row, colNum);
    return cell.getDateCellValue();
}

From source file:net.sf.jasperreports.engine.data.AbstractPoiXlsDataSource.java

License:Open Source License

/**
 *
 *//* w ww  .j a  va  2s  . com*/
public Object getFieldValue(JRField jrField) throws JRException {
    String fieldName = jrField.getName();

    Integer columnIndex = columnNames.get(fieldName);
    if (columnIndex == null && fieldName.startsWith("COLUMN_")) {
        columnIndex = Integer.valueOf(fieldName.substring(7));
    }
    if (columnIndex == null) {
        throw new JRException("Unknown column name : " + fieldName);
    }
    Sheet sheet = workbook.getSheetAt(sheetIndex);
    Cell cell = sheet.getRow(recordIndex).getCell(columnIndex);
    Class<?> valueClass = jrField.getValueClass();

    if (valueClass.equals(String.class)) {
        return cell.getStringCellValue();
    }
    try {
        if (valueClass.equals(Boolean.class)) {
            if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                return cell.getBooleanCellValue();
            } else {
                return convertStringValue(cell.getStringCellValue(), valueClass);
            }
        } else if (Number.class.isAssignableFrom(valueClass)) {
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                return convertNumber(cell.getNumericCellValue(), valueClass);
            } else {
                if (numberFormat != null) {
                    return FormatUtils.getFormattedNumber(numberFormat, cell.getStringCellValue(), valueClass);
                } else {
                    return convertStringValue(cell.getStringCellValue(), valueClass);
                }
            }
        } else if (Date.class.isAssignableFrom(valueClass)) {
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                return cell.getDateCellValue();
            } else {
                if (dateFormat != null) {
                    return FormatUtils.getFormattedDate(dateFormat, cell.getStringCellValue(), valueClass);
                } else {
                    return convertStringValue(cell.getStringCellValue(), valueClass);
                }
            }
        } else {
            throw new JRException("Field '" + jrField.getName() + "' is of class '" + valueClass.getName()
                    + "' and can not be converted");
        }
    } catch (Exception e) {
        throw new JRException("Unable to get value for field '" + jrField.getName() + "' of class '"
                + valueClass.getName() + "'", e);
    }
}

From source file:net.sf.taverna.t2.activities.spreadsheet.ExcelSpreadsheetReader.java

License:Open Source License

private String getCellValue(Cell cell, DataFormatter dataFormatter) {
    String value = null;/*from   w  w  w .j  av  a 2  s . co  m*/
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN:
        value = Boolean.toString(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            //            value = cell.getDateCellValue().toString();
            value = dataFormatter.formatCellValue(cell);
        } else {
            value = Double.toString(cell.getNumericCellValue());
        }
        break;
    case Cell.CELL_TYPE_STRING:
        value = cell.getStringCellValue();
        break;
    case Cell.CELL_TYPE_FORMULA:
        switch (cell.getCachedFormulaResultType()) {
        case Cell.CELL_TYPE_BOOLEAN:
            value = Boolean.toString(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                value = cell.getDateCellValue().toString();
            } else {
                value = Double.toString(cell.getNumericCellValue());
            }
            break;
        case Cell.CELL_TYPE_STRING:
            value = cell.getStringCellValue();
            break;
        default:
            break;
        }
    default:
        break;
    }
    // value = dataFormatter.formatCellValue(cell);
    // if ("".equals(value)) value = null;
    return value;
}