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

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

Introduction

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

Prototype

public static double getExcelDate(Date date) 

Source Link

Document

Given a Date, converts it into a double representing its internal Excel representation, which is the number of days since 1/1/1900.

Usage

From source file:br.com.tecsinapse.dataio.util.WorkbookUtil.java

License:LGPL

private double toExcelDate(Date date) {
    return DateUtil.getExcelDate(date);
}

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

License:Apache License

private void updateObject(Cell cell, Object x) throws SQLException {
    if (x instanceof String) {
        cell.setCellValue((String) x);
    } else if (x instanceof char[]) {
        cell.setCellValue(new String((char[]) x));
    } else if (x instanceof Double) {
        if (x.equals(Double.NEGATIVE_INFINITY) || x.equals(Double.POSITIVE_INFINITY) || x.equals(Double.NaN)) {
            cell.setCellValue(BAD_DOUBLE);
        } else {//from ww  w . ja v  a  2  s  .c o  m
            cell.setCellValue((Double) x);
        }
    } else if (x instanceof Number) {
        cell.setCellValue(((Number) x).doubleValue());
        // } else if (x instanceof java.sql.Date) {
        // cell.setCellValue(new java.util.Date(((java.sql.Date)x).getTime()));
        // if (dateStyle != null) cell.setCellStyle(dateStyle);
    } else if (x instanceof java.util.Date) {
        cell.setCellValue(DateUtil.getExcelDate((java.util.Date) x));
        if (dateStyle != null)
            cell.setCellStyle(dateStyle);
    } else if (x instanceof Boolean) {
        cell.setCellValue((Boolean) x);
    } else if (x == null) {
        cell.setCellValue((String) null);
    } else {
        throw new SQLException("Unknown value type for ExcelResultSet.updateObject: " + x + " ("
                + x.getClass().getName() + ")");
    }
}

From source file:org.eclipse.emfforms.internal.spreadsheet.core.converter.EMFFormsSpreadsheetSingleAttributeConverter.java

License:Open Source License

/**
 * {@inheritDoc}//from   ww  w .j  a  va  2s .  c  om
 *
 * @see org.eclipse.emfforms.spi.spreadsheet.core.converter.EMFFormsSpreadsheetValueConverter#setCellValue(org.apache.poi.ss.usermodel.Cell,
 *      java.lang.Object, org.eclipse.emf.ecore.EStructuralFeature,
 *      org.eclipse.emf.ecp.view.spi.context.ViewModelContext)
 */
@Override
public void setCellValue(Cell cell, Object value, EStructuralFeature eStructuralFeature,
        ViewModelContext viewModelContext) throws EMFFormsConverterException {
    if (value == null) {
        return;
    }
    final EAttribute eAttribute = EAttribute.class.cast(eStructuralFeature);
    if (eAttribute == null) {
        throw new EMFFormsConverterException("The provided eAttribute is null."); //$NON-NLS-1$
    }
    final EDataType attributeType = eAttribute.getEAttributeType();
    if (attributeType == null) {
        throw new EMFFormsConverterException("The attributeType of the provided eAttribute is null."); //$NON-NLS-1$
    }

    if (isBoolean(attributeType.getInstanceClass())) {
        cell.setCellValue(Boolean.class.cast(value));
    } else if (isByte(attributeType.getInstanceClass()) || isShort(attributeType.getInstanceClass())
            || isInteger(attributeType.getInstanceClass()) || isLong(attributeType.getInstanceClass())) {
        cell.setCellValue(Number.class.cast(value).doubleValue());
    } else if (isFloat(attributeType.getInstanceClass()) || isDouble(attributeType.getInstanceClass())) {
        writeFloatDouble(cell, value, viewModelContext, eAttribute);
    } else if (isBigInteger(attributeType.getInstanceClass())) {
        writeBigInteger(cell, value, viewModelContext);
    } else if (isBigDecimal(attributeType.getInstanceClass())) {
        writeBigDecimal(cell, value, viewModelContext, eAttribute);
    } else if (isDate(attributeType.getInstanceClass())) {
        cell.setCellValue(DateUtil.getExcelDate(Date.class.cast(value)));
        cell.setCellStyle((CellStyle) viewModelContext.getContextValue(EMFFormsCellStyleConstants.DATE));
    } else if (isXMLDate(attributeType.getInstanceClass())) {
        final XMLGregorianCalendar xmlDate = XMLGregorianCalendar.class.cast(value);
        cell.setCellValue(DateUtil
                .getExcelDate(xmlDate.toGregorianCalendar(TimeZone.getTimeZone("UTC"), null, xmlDate), false)); //$NON-NLS-1$
        cell.setCellStyle((CellStyle) viewModelContext.getContextValue(EMFFormsCellStyleConstants.DATE));
    } else {
        cell.setCellValue(EcoreUtil.convertToString(attributeType, value));
        cell.setCellStyle((CellStyle) viewModelContext.getContextValue(EMFFormsCellStyleConstants.TEXT));
    }
}

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

License:Apache License

@SuppressWarnings("unused")
private static void writeCell(Cell cell, Object val, boolean userTemplate,
        ExcelWriteFieldMappingAttribute attribute, Object bean) {
    if (attribute != null && attribute.getLinkField() != null) {
        String addressFieldName = attribute.getLinkField();
        String address = null;/*from  w  w w.j  a  va  2s  .  c  o m*/
        if (bean != null) {
            address = (String) getFieldValue(bean, addressFieldName, true);
        }
        Workbook wb = cell.getRow().getSheet().getWorkbook();

        Hyperlink link = wb.getCreationHelper().createHyperlink(attribute.getLinkType());
        link.setAddress(address);
        cell.setHyperlink(link);
        // Its style can't inherit from cell.
        CellStyle style = wb.createCellStyle();
        Font hlinkFont = wb.createFont();
        hlinkFont.setUnderline(Font.U_SINGLE);
        hlinkFont.setColor(IndexedColors.BLUE.getIndex());
        style.setFont(hlinkFont);
        if (cell.getCellStyle() != null) {
            style.setFillBackgroundColor(cell.getCellStyle().getFillBackgroundColor());
        }
        cell.setCellStyle(style);
    }
    if (val == null) {
        cell.setCellValue((String) null);
        return;
    }
    Class<?> clazz = val.getClass();
    if (val instanceof Byte) {// Double
        Byte temp = (Byte) val;
        cell.setCellValue((double) temp.byteValue());
    } else if (val instanceof Short) {
        Short temp = (Short) val;
        cell.setCellValue((double) temp.shortValue());
    } else if (val instanceof Integer) {
        Integer temp = (Integer) val;
        cell.setCellValue((double) temp.intValue());
    } else if (val instanceof Long) {
        Long temp = (Long) val;
        cell.setCellValue((double) temp.longValue());
    } else if (val instanceof Float) {
        Float temp = (Float) val;
        cell.setCellValue((double) temp.floatValue());
    } else if (val instanceof Double) {
        Double temp = (Double) val;
        cell.setCellValue((double) temp.doubleValue());
    } else if (val instanceof Date) {// Date
        Date dateVal = (Date) val;
        long time = dateVal.getTime();
        // read is based on 1899/12/31 but DateUtil.getExcelDate is base on
        // 1900/01/01
        if (time >= TIME_1899_12_31_00_00_00_000 && time < TIME_1900_01_01_00_00_00_000) {
            Date incOneDay = new Date(time + 24 * 60 * 60 * 1000);
            double d = DateUtil.getExcelDate(incOneDay);
            cell.setCellValue(d - 1);
        } else {
            cell.setCellValue(dateVal);
        }

        if (!userTemplate) {
            Workbook wb = cell.getRow().getSheet().getWorkbook();
            CellStyle cellStyle = cell.getCellStyle();
            if (cellStyle == null) {
                cellStyle = wb.createCellStyle();
            }
            DataFormat dataFormat = wb.getCreationHelper().createDataFormat();
            // @see #BuiltinFormats
            // 0xe, "m/d/yy"
            // 0x14 "h:mm"
            // 0x16 "m/d/yy h:mm"
            // {@linke https://en.wikipedia.org/wiki/Year_10,000_problem}
            /** [1899/12/31 00:00:00:000~1900/01/01 00:00:000) */
            if (time >= TIME_1899_12_31_00_00_00_000 && time < TIME_1900_01_02_00_00_00_000) {
                cellStyle.setDataFormat(dataFormat.getFormat("h:mm"));
                // cellStyle.setDataFormat(dataFormat.getFormat("m/d/yy h:mm"));
            } else {
                // if ( time % (24 * 60 * 60 * 1000) == 0) {//for time
                // zone,we can't use this way.
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(dateVal);
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                int minute = calendar.get(Calendar.MINUTE);
                int second = calendar.get(Calendar.SECOND);
                int millisecond = calendar.get(Calendar.MILLISECOND);
                if (millisecond == 0 && second == 0 && minute == 0 && hour == 0) {
                    cellStyle.setDataFormat(dataFormat.getFormat("m/d/yy"));
                } else {
                    cellStyle.setDataFormat(dataFormat.getFormat("m/d/yy h:mm"));
                }
            }
            cell.setCellStyle(cellStyle);
        }
    } else if (val instanceof Boolean) {// Boolean
        cell.setCellValue(((Boolean) val).booleanValue());
    } else {// String
        cell.setCellValue((String) val.toString());
    }
}

From source file:org.soulwing.jawb.poi.ApachePoiDateTimeConverter.java

License:Apache License

/**
 * {@inheritDoc}/*w ww  .j  a  v a 2 s.  c om*/
 */
@Override
public double convertDate(String date) {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    try {
        return DateUtil.getExcelDate(df.parse(date));
    } catch (ParseException ex) {
        throw new IllegalArgumentException("invalid date format");
    }
}