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:com.actelion.research.spiritapp.ui.util.PDFUtils.java

License:Open Source License

private static Chunk getChunk(Workbook wb, Cell cell) {
    Chunk phrase = null;/*w w w  .ja  v  a  2  s .  c  o  m*/

    switch (cell.getCellType() == Cell.CELL_TYPE_FORMULA ? cell.getCachedFormulaResultType()
            : cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        phrase = new Chunk("" + cell.getStringCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        String format = cell.getCellStyle().getDataFormatString();
        if (cell.getCellStyle().getDataFormat() > 0) {
            try {
                if (format.contains("0")) {
                    //Decimal
                    DecimalFormat df = new DecimalFormat(format);
                    phrase = new Chunk(df.format(cell.getNumericCellValue()));
                } else if (format.contains("h:")) {
                    phrase = new Chunk(FormatterUtils.formatDateTimeShort(cell.getDateCellValue()));
                } else if (format.contains("yy")) {
                    phrase = new Chunk(FormatterUtils.formatDate(cell.getDateCellValue()));
                }
            } catch (Exception e) {
                System.err.println(e);
            }
        }
        if (phrase == null) {
            phrase = new Chunk("" + (int) cell.getNumericCellValue());
        }
        break;
    case Cell.CELL_TYPE_BLANK:
        phrase = new Chunk("");
        break;
    default:
        phrase = new Chunk("" + cell.getCellType());
    }
    Font font = wb.getFontAt(cell.getCellStyle().getFontIndex());
    short[] rgb = HSSFColor.getIndexHash().get((int) font.getColor()).getTriplet();

    phrase.setFont(new com.lowagie.text.Font(phrase.getFont().getBaseFont(), font.getFontHeightInPoints() - 3,
            (font.getBold() ? com.lowagie.text.Font.BOLD : com.lowagie.text.Font.NORMAL),
            new Color(rgb[0], rgb[1], rgb[2])));
    return phrase;
}

From source file:com.adobe.acs.commons.data.Variant.java

License:Apache License

private void setValue(Cell cell) {
    int cellType = cell.getCellType();
    if (cellType == Cell.CELL_TYPE_FORMULA) {
        cellType = cell.getCachedFormulaResultType();
    }/*  www. j  ava2s . c om*/
    switch (cellType) {
    case Cell.CELL_TYPE_BOOLEAN:
        setValue(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        double number = cell.getNumericCellValue();
        if (Math.floor(number) == number) {
            setValue((long) number);
        } else {
            setValue(number);
        }
        if (DateUtil.isCellDateFormatted(cell)) {
            setValue(cell.getDateCellValue());
        }
        DataFormatter dataFormatter = new DataFormatter();
        if (cellType == Cell.CELL_TYPE_FORMULA) {
            setValue(dataFormatter.formatCellValue(cell));
        } else {
            CellStyle cellStyle = cell.getCellStyle();
            setValue(dataFormatter.formatRawCellContents(cell.getNumericCellValue(), cellStyle.getDataFormat(),
                    cellStyle.getDataFormatString()));
        }
        break;
    case Cell.CELL_TYPE_STRING:
        setValue(cell.getStringCellValue().trim());
        break;
    case Cell.CELL_TYPE_BLANK:
    default:
        clear();
        break;
    }
}

From source file:com.alibaba.ims.platform.util.ExcelUtil.java

License:Open Source License

private static String getCellValue(Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue().getString();
    case Cell.CELL_TYPE_NUMERIC:
        if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
            return DateUtil.format(cell.getDateCellValue(), "yyyy-MM-dd");
        } else {/*w w w . j  av  a  2s.c o  m*/
            return new DecimalFormat("0").format(cell.getNumericCellValue());
        }
    case Cell.CELL_TYPE_BOOLEAN:
        return String.valueOf(cell.getBooleanCellValue());
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    default:
        return null;
    }
}

From source file:com.alphacell.controller.CargarDatosBean.java

public void handleFileUpload(FileUploadEvent event) {

    if (event.getFile().equals(null)) {

        FacesUtil.addInfoMessage("El archivo es null");

    }/* www .  ja  v  a  2  s.  co m*/
    InputStream file;
    HSSFWorkbook workbook = null;
    try {
        file = event.getFile().getInputstream();
        workbook = new HSSFWorkbook(file);
    } catch (IOException e) {

        FacesUtil.addErrorMessage("Error Leyendo archivo : " + e);

    }

    HSSFSheet sheet = workbook.getSheetAt(1);

    Iterator<Row> rowIterator = sheet.iterator();
    Calendar calendar = new GregorianCalendar();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();

        Iterator<Cell> cellIterator = row.cellIterator();
        //Job job = new Job();
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();

            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:

                if (HSSFDateUtil.isCellDateFormatted(cell) || HSSFDateUtil.isCellInternalDateFormatted(cell)) {
                    calendar.setTime(cell.getDateCellValue());
                } else {
                    System.out.print(cell.getNumericCellValue() + "\t\t");
                }
                break;
            case Cell.CELL_TYPE_STRING:
                System.out.print(cell.getStringCellValue() + "\t\t");
                break;
            }

        }
    }
}

From source file:com.aurel.track.exchange.excel.ExcelImportBL.java

License:Open Source License

/**
 * Gets the Double value of a cell//from   w w w .j av a 2s.  c  o  m
 * 
 * @param cell
 * @return
 */
private static Date getDateCellValue(Cell cell, Locale locale) throws ExcelImportInvalidCellValueException {
    Date dateValue = null;
    int cellType = cell.getCellType();
    if (cellType == Cell.CELL_TYPE_NUMERIC) {
        try {
            dateValue = cell.getDateCellValue();
        } catch (Exception e) {
            throw new ExcelImportInvalidCellValueException(String.valueOf(cell.getNumericCellValue()));
        }
    } else {
        if (cellType == Cell.CELL_TYPE_STRING) {
            RichTextString richTextString = cell.getRichStringCellValue();
            if (richTextString != null) {
                String stringValue = richTextString.getString();
                if (stringValue != null) {
                    stringValue = stringValue.trim();
                    if (!"".equals(stringValue)) {
                        dateValue = DateTimeUtils.getInstance().parseGUIDate(stringValue, locale);
                        if (dateValue == null) {
                            dateValue = DateTimeUtils.getInstance().parseShortDate(stringValue, locale);
                            if (dateValue == null) {
                                dateValue = DateTimeUtils.getInstance().parseISODate(stringValue);
                                if (dateValue == null) {
                                    throw new ExcelImportInvalidCellValueException(stringValue);
                                }
                            }
                        }
                    }
                }
            }
        } else {
            throw new ExcelImportInvalidCellValueException(getStringCellValue(cell));
        }
    }
    return dateValue;
}

From source file:com.aurel.track.exchange.excel.ExcelImportBL.java

License:Open Source License

/**
 * Gets the Double value of a cell//from   ww  w  . j a v  a  2 s  .  c  om
 * 
 * @param cell
 * @return
 */
private static Date getDateTimeCellValue(Cell cell, Locale locale) throws ExcelImportInvalidCellValueException {
    Date dateValue = null;
    int cellType = cell.getCellType();
    if (cellType == Cell.CELL_TYPE_NUMERIC) {
        try {
            dateValue = cell.getDateCellValue();
        } catch (Exception e) {
            throw new ExcelImportInvalidCellValueException(String.valueOf(cell.getNumericCellValue()));
        }
    } else {
        if (cellType == Cell.CELL_TYPE_STRING) {
            RichTextString richTextString = cell.getRichStringCellValue();
            if (richTextString != null) {
                String stringValue = richTextString.getString();
                if (stringValue != null) {
                    stringValue = stringValue.trim();
                    if (!"".equals(stringValue)) {
                        dateValue = DateTimeUtils.getInstance().parseGUIDateTime(stringValue, locale);
                        if (dateValue == null) {
                            dateValue = DateTimeUtils.getInstance().parseShortDateTime(stringValue, locale);
                            if (dateValue == null) {
                                dateValue = DateTimeUtils.getInstance().parseISODateTime(stringValue);
                            }
                        }
                    }
                }
            }
        } else {
            throw new ExcelImportInvalidCellValueException(getStringCellValue(cell));
        }
    }
    return dateValue;
}

From source file:com.b2international.snowowl.snomed.core.refset.automap.XlsParser.java

License:Apache License

/**
 * @param cell//from   ww  w.  j  a va  2 s .c  o  m
 * @return the textual representation of the cell or empty string if the cell is empty (null)
 */
private String getStringValue(Cell cell) {
    String value = "";
    if (cell != null) {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            value = cell.getRichStringCellValue().getString();
            break;
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                value = cell.getDateCellValue().toString();
            } else {
                value = Integer.toString((int) cell.getNumericCellValue());
            }
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            value = Boolean.toString(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            value = cell.getCellFormula();
            break;
        }
    }
    return value;
}

From source file:com.b2international.snowowl.snomed.importer.net4j.SnomedSubsetImportUtil.java

License:Apache License

private String getStringValue(final Cell cell) {
    String value = "";

    //   empty cell
    if (cell == null) {
        return "";
    }/*from   w ww. j  av  a  2s .  c  o  m*/

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        value = cell.getRichStringCellValue().getString();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            value = cell.getDateCellValue().toString();
        } else {
            value = Integer.toString((int) cell.getNumericCellValue());
        }
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        value = Boolean.toString(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        value = cell.getCellFormula();
        break;
    }

    return value;
}

From source file:com.bayareasoftware.chartengine.ds.ExcelDataStream.java

License:Apache License

private Object getCellData(Cell cell, int rowNum, int type, int index) {
    evaluator.evaluate(cell);//www  .  ja va 2 s .  co  m
    if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK || type == UNKNOWN) {
        return null;
    }
    //p("getting from row#" + rowNum + " col#" + cell.getCellNum());
    switch (type) {
    case STRING:
        return getCellString(cell);
    case DATE: {
        if (ExcelInference.isCellDateFormatted(cell)) {
            Date d = cell.getDateCellValue();
            if (d != null) {
                return d;
            }
        }
        // figure out date format
        Date ret = null;
        String dstr = getCellString(cell);
        if (dfmts[index - 1] != null) {
            ret = this.parseDate(dstr, dfmts[index - 1], -1, index);
        } else if (metadata.getColumnFormat(index) != null) {
            dfmts[index - 1] = DateUtil.createDateFormat(metadata.getColumnFormat(index));
            ret = this.parseDate(dstr, dfmts[index - 1], -1, index);
        } else if (dstr != null) {
            if (dateRecognizer == null) {
                dateRecognizer = new DateRecognizer();
            }
            dateRecognizer.reset();
            dateRecognizer.parse(dstr);
            if (!dateRecognizer.failed()) {
                dfmts[index - 1] = dateRecognizer.getSimpleDateFormat();
                ret = this.parseDate(dstr, dfmts[index - 1], -1, index);
            }
        }
        return ret;
    }
    case DOUBLE:
        return cell.getNumericCellValue();
    case INTEGER:
        double d = cell.getNumericCellValue();
        return new Double(d).intValue();
    case BOOLEAN:
        String s = cell.getRichStringCellValue().getString();
        return "true".equalsIgnoreCase(s);
    case IGNORE:
        return null;
    default:
        throw new AssertionError("unexpected type: " + DataType.toString(type));
    }
}

From source file:com.bayareasoftware.chartengine.ds.util.ExcelInference.java

License:Apache License

public static String getCellString(Cell cell, HSSFFormulaEvaluator eval, DateFormat dfmt) {
    if (cell == null) {
        return null;
    }//from  w  w w .j  a v a  2s.  c  om
    String ret = null;
    eval.evaluate(cell);
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_NUMERIC:
    case HSSFCell.CELL_TYPE_FORMULA: // ?
        if (isCellDateFormatted(cell)) {
            if (dfmt == null) {
                dfmt = new SimpleDateFormat("yyyy-MM-dd");
            }
            Date d = cell.getDateCellValue();
            if (d != null) {
                ret = dfmt.format(d);
            } else {
                ret = "";
            }
        } else {
            try {
                ret = "" + cell.getNumericCellValue();
            } catch (IllegalStateException ise) {
                int errVal = cell.getErrorCellValue();
                String formula = cell.getCellFormula();
                int cacheType = cell.getCachedFormulaResultType();
                throw new RuntimeException(ise.getMessage() + ": errVal=" + errVal + " formula='" + formula
                        + "' cacheType=" + cacheType);
            }
        }
        break;
    case HSSFCell.CELL_TYPE_BLANK:
        ret = null;
        break;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        ret = "" + cell.getBooleanCellValue();
        break;
    case HSSFCell.CELL_TYPE_STRING:
    default:
        ret = cell.getRichStringCellValue().getString();
    }
    return ret;
}