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.joeffice.spreadsheet.cell.CellUtils.java

License:Apache License

public static String getFormattedText(Cell cell) {
    if (cell == null) {
        return "";
    }/*  w  w  w  .j a v a2  s  .c o  m*/
    int type = cell.getCellType();
    if (type == Cell.CELL_TYPE_STRING) {
        return cell.getStringCellValue();
    } else if (type == Cell.CELL_TYPE_NUMERIC) {
        if (DateUtil.isCellDateFormatted(cell)) {
            return DATE_FORMATTER.format(cell.getDateCellValue());
        } else {
            return NUMBER_FORMATTER.format(cell.getNumericCellValue());
        }
    } else if (type == Cell.CELL_TYPE_BOOLEAN) {
        return String.valueOf(cell.getBooleanCellValue());
    } else {
        return "";
    }
}

From source file:org.kopsox.spreadsheet.util.POIUtil.java

License:Open Source License

@SuppressWarnings("boxing")
private static final Value getValueFromNumeric(Cell cell, String formula) {

    Value ret = null;/*  w  w  w .ja va2 s.c  o m*/

    //Numeric can be a Date or a Double
    if (!DateUtil.isCellDateFormatted(cell)) {
        DoubleValue tmp = new DoubleValue(cell.getNumericCellValue());
        tmp.setFormula(formula);
        ret = tmp;

    } else {
        DateValue tmp = new DateValue(cell.getDateCellValue());
        tmp.setFormula(formula);
        ret = tmp;

    }

    return ret;
}

From source file:org.laukvik.excel.ExcelTableModel.java

License:Apache License

public String getString(Cell cell) {
    if (cell == null) {
        return null;
    }/*from  www  .j av a2s  . c o  m*/
    try {
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
            Date date = cell.getDateCellValue();
            return dateFormat.format(date);
        }
    } catch (Exception e) {
    }
    try {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            return null;
        case Cell.CELL_TYPE_BOOLEAN:
            return cell.getBooleanCellValue() + "";
        case Cell.CELL_TYPE_ERROR:
            return null;
        case Cell.CELL_TYPE_FORMULA:
            return cell.getCellFormula();
        case Cell.CELL_TYPE_NUMERIC:
            Double d = cell.getNumericCellValue();
            return d.intValue() + "";
        case Cell.CELL_TYPE_STRING:
            return cell.getStringCellValue();

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.meveo.service.catalog.impl.PricePlanMatrixService.java

License:Open Source License

private Date getCellAsDate(Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_ERROR:
    case Cell.CELL_TYPE_BLANK:
    case Cell.CELL_TYPE_FORMULA:
        return null;
    case Cell.CELL_TYPE_NUMERIC:
        return DateUtil.getJavaDate(cell.getNumericCellValue());
    default://from  w  w w.jav a2s . co  m
        try {
            return cell.getDateCellValue();
        } catch (Exception e) {
            try {
                return sdf.parse(cell.getStringCellValue());
            } catch (ParseException e1) {
                return null;
            }
        }
    }
}

From source file:org.mousephenotype.cda.threei.util.AnaExcelReader.java

License:Apache License

public String[] getRow() {
    if (!this.rowIterator.hasNext()) {
        this.lastRowRead = null;
        return null;
    }/* ww  w.j  a va  2  s. c  om*/

    Row currentRow = this.rowIterator.next();
    Iterator<Cell> cellIterator = currentRow.iterator();
    int nColumns = this.getNumberOfColumns();

    // Get row details
    String[] resultRow = new String[nColumns];
    for (int col = 0; col < nColumns; col++) {
        if (cellIterator.hasNext()) {
            Cell currentCell = cellIterator.next();
            //getCellTypeEnum shown as deprecated for version 3.15
            //getCellTypeEnum ill be renamed to getCellType starting from version 4.0
            if (currentCell.getCellTypeEnum() == CellType.STRING) {
                resultRow[col] = "" + currentCell.getStringCellValue();
            } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
                if (DateUtil.isCellDateFormatted(currentCell)) {
                    resultRow[col] = "" + currentCell.getDateCellValue();
                } else {
                    resultRow[col] = currentCell.getNumericCellValue() + "";
                }
            } else {
                resultRow[col] = "NonStringNonNumericValue";
            }
        }
    }
    numberOfRowsRead++;
    this.lastRowRead = resultRow;
    return resultRow;
}

From source file:org.myorg.insertar.insertarSabanaTopComponent.java

public void procesaDatosEntrada(List sheetData) {
    ///* ww w. j a  v a2s .co  m*/
    // Iterates the data and print it out to the console.
    //
    int cnt = 0;
    int fProblema = 0;
    int nTipos = 0;
    SimpleDateFormat formatDateJava = new SimpleDateFormat("dd-MM-yyyy");

    HSSFRichTextString richTextString;
    // ...........................................................................................
    for (int i = 0; i < sheetData.size(); i++) {

        List list = (List) sheetData.get(i);
        System.out.println(i + " -> Tenemos Exel con " + list.size() + " columnas");

        // ...........................................................................................
        if (cnt == 0) { // CARGAMOS LOS NOMBRES DE LOS CAMPOS

            for (int j = 0; j < list.size(); j++) {

                Cell cell = (Cell) list.get(j);
                // ............................................................
                System.out.println("Tipo  Nombre =" + cell.getCellType());
                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {

                    richTextString = (HSSFRichTextString) cell.getRichStringCellValue();

                    this.nombres[j] = richTextString.getString();
                    System.out.println("Cargando Nombre =" + this.nombres[j]);
                }
            }
            this.nColumnas = list.size();
            System.out.println("Numero de campos=" + this.nColumnas);

        }
        // ...........................................................................................
        if (cnt == 1) { // CARGAMOS LOS TIPOS DE LOS CAMPOS
            nTipos = list.size();
            for (int j = 0; j < list.size(); j++) {

                Cell cell = (Cell) list.get(j);

                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {

                    richTextString = (HSSFRichTextString) cell.getRichStringCellValue();

                    this.tipos[j] = richTextString.getString();
                }
                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {

                    this.tipos[j] = Double.toString(cell.getNumericCellValue());
                    System.out.println("Cargo this.tipos[" + j + "] = " + this.tipos[j]);

                }

            }

        }
        // ...........................................................................................
        if (cnt > 1) { // CARGAMOS LOS DATOS
            System.out.println("------------CARGAMOS LOS DATOS -----------");
            if (nTipos != list.size()) {
                this.sLogTxt += "AVISO: FILA DATOS(" + (cnt - 2)
                        + ") -> HAY UN PROBLEMA :EL NMERO DE CAMPOS DEFINIDOS Y EL QUE CONTIENE LA L?NEA DE DATOS NO COINCIDE ("
                        + nTipos + "!=" + list.size() + ") \n";
                //   fProblema = 2 ;
            }
            if (fProblema != 2) {

                for (int j = 0; j < list.size(); j++) {

                    Cell cell = (Cell) list.get(j);

                    this.tablaDatos[cnt - 2][j] = "";
                    // ............................................................                                           
                    if (this.tipos[j].equals("4.0")) { // es un tipo fecha
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                        try {
                            this.tablaDatos[cnt - 2][j] = String.valueOf(sdf.format(cell.getDateCellValue()));
                        } catch (IllegalStateException e) {
                            this.sLogTxt += "AVISO: FILA DATOS(" + (cnt - 2)
                                    + ") -> HAY UN PROBLEMA EL TIPO FECHA EN EL CAMPO: " + this.nombres[j]
                                    + "\n";
                            fProblema = 1;
                        }

                    } else {
                        // ............................................................
                        if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                            try {
                                this.tablaDatos[cnt - 2][j] = Double.toString(cell.getNumericCellValue());
                            } catch (IllegalStateException e) {
                                this.sLogTxt += "AVISO: FILA DATOS(" + (cnt - 2)
                                        + ") -> HAY UN PROBLEMA EL TIPO DE DATO NUMERICO EN EL CAMPO: "
                                        + this.nombres[j] + "\n";
                                fProblema = 1;
                            }

                        } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                            // ............................................................
                            try {
                                richTextString = (HSSFRichTextString) cell.getRichStringCellValue();
                                this.tablaDatos[cnt - 2][j] = richTextString.getString();
                            } catch (IllegalStateException e) {
                                this.sLogTxt += "AVISO: FILA DATOS(" + (cnt - 2)
                                        + ") ->HAY UN PROBLEMA EL TIPO DE DATO TEXTO EN EL CAMPO: "
                                        + this.nombres[j] + "\n";
                                fProblema = 1;

                            }

                        } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                            // ............................................................
                            try {
                                this.tablaDatos[cnt - 2][j] = Boolean.toString(cell.getBooleanCellValue());
                            } catch (IllegalStateException e) {
                                this.sLogTxt += "AVISO: FILA DATOS(" + (cnt - 2)
                                        + ") ->HAY UN PROBLEMA EL TIPO DE DATO BOOLEANO EN EL CAMPO: "
                                        + this.nombres[j] + "\n";
                                fProblema = 1;
                            }

                        } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                            // ............................................................
                            this.tablaDatos[cnt - 2][j] = "";

                        }
                    }

                }

                // ...........................................................................................

            }
            cnt++;
        } else { // Nos saltamos el procesamiento de esta lnea y seguimos contando.
            cnt++;
            fProblema = 0;
        }
    }

    // ...........................................................................................
    this.nDatos = cnt;
    System.out.println("----------- HE CARGADO " + this.nDatos + " REGISTROS ------------");
    this.sLogTxt += "----------- HE CARGADO " + this.nDatos + " REGISTROS  ------------" + "\n";
    logAcciones.setText(this.sLogTxt);
    if (fProblema == 0) {
        JOptionPane.showMessageDialog(null, "\nHE CARGADO:" + this.nDatos + " REGISTROS", "INFORMACIN",
                JOptionPane.WARNING_MESSAGE);
    } else {
        JOptionPane.showMessageDialog(null, "\nSE HAN DETECTADO PROBLEMAS, SE CANCELA LA CARGA DE ARCHIVO EXEL",
                "AVISO", JOptionPane.WARNING_MESSAGE);
    }
}

From source file:org.natica.expense.ExpenseUtility.java

public List<Expense> parseExcel(File file) throws IOException, ExpenseExcelFormatException {
    List<Expense> expenses = new ArrayList<Expense>();
    FileInputStream fis;/*w ww .  j a v a2 s . c  o m*/

    fis = new FileInputStream(file);
    XSSFWorkbook wb;

    wb = new XSSFWorkbook(fis);
    XSSFSheet sh = wb.getSheetAt(0);

    for (Row row : sh) {
        if (row.getRowNum() == 0) {
            if (!checkHeaderRow(sh.getRow(0)))
                throw new ExpenseExcelFormatException("Excel Balk simleri Hataldr.");
            else
                continue;
        }
        Expense e = new Expense();
        for (Cell cell : row) {
            if (cell.getColumnIndex() == 0) {
                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    if (!HSSFDateUtil.isCellDateFormatted(cell)) {
                        throw new ExpenseExcelFormatException("Hatal veri! Satr:" + row.getRowNum()
                                + " Stun:" + cell.getColumnIndex() + " Hata Nedeni: Veri tipi tarih deil");
                    }
                } else {
                    throw new ExpenseExcelFormatException("Hatal veri! Satr:" + row.getRowNum() + " Stun:"
                            + cell.getColumnIndex() + " Hata Nedeni: Veri tipi tarih deil");
                }
                e.setExpenseEntryDate(cell.getDateCellValue());
            } else if (cell.getColumnIndex() == 1) {
                if (cell.getCellType() != Cell.CELL_TYPE_STRING) {
                    throw new ExpenseExcelFormatException("Hatal veri! Satr:" + row.getRowNum() + " Stun:"
                            + cell.getColumnIndex() + " Hata Nedeni: Veri tipi yaz deil");
                }
                e.setProjectName(cell.getStringCellValue());
            } else if (cell.getColumnIndex() == 2) {
                if (cell.getCellType() != Cell.CELL_TYPE_STRING) {
                    throw new ExpenseExcelFormatException("Hatal veri! Satr:" + row.getRowNum() + " Stun:"
                            + cell.getColumnIndex() + " Hata Nedeni: Veri tipi yaz deil");
                }
                e.setExpenseName(cell.getStringCellValue());
            } else if (cell.getColumnIndex() == 3) {
                if (cell.getCellType() != Cell.CELL_TYPE_STRING) {
                    throw new ExpenseExcelFormatException("Hatal veri! Satr:" + row.getRowNum() + " Stun:"
                            + cell.getColumnIndex() + " Hata Nedeni: Veri tipi yaz deil");
                }
                e.setPaymentMethod(cell.getStringCellValue());
            } else if (cell.getColumnIndex() == 4) {
                if (cell.getCellType() != Cell.CELL_TYPE_STRING) {
                    throw new ExpenseExcelFormatException("Hatal veri! Satr:" + row.getRowNum() + " Stun:"
                            + cell.getColumnIndex() + " Hata Nedeni: Veri tipi yaz deil");
                }
                e.setCurrency(cell.getStringCellValue());
            } else if (cell.getColumnIndex() == 5) {
                if (cell.getCellType() != Cell.CELL_TYPE_NUMERIC) {
                    throw new ExpenseExcelFormatException("Hatal veri! Satr:" + row.getRowNum() + " Stun:"
                            + cell.getColumnIndex() + " Hata Nedeni: Veri tipi nmerik deil");
                }
                e.setNetAmount(BigDecimal.valueOf(cell.getNumericCellValue()));
            } else if (cell.getColumnIndex() == 6) {
                if (cell.getCellType() != Cell.CELL_TYPE_STRING) {
                    throw new ExpenseExcelFormatException("Hatal veri! Satr:" + row.getRowNum() + " Stun:"
                            + cell.getColumnIndex() + " Hata Nedeni: Veri tipi yaz deil");
                }
                e.setRestaurant(cell.getStringCellValue());
            } else if (cell.getColumnIndex() == 7) {
                if (cell.getCellType() != Cell.CELL_TYPE_NUMERIC) {
                    throw new ExpenseExcelFormatException("Hatal veri! Satr:" + row.getRowNum() + " Stun:"
                            + cell.getColumnIndex() + " Hata Nedeni: Veri tipi nmerik deil");
                }
                e.setDocumentNumber(Integer.valueOf((int) cell.getNumericCellValue()));
            }
        }
        expenses.add(e);
    }

    if (wb != null)
        wb.close();

    if (fis != null)
        fis.close();

    return expenses;
}

From source file:org.netxilia.impexp.impl.ExcelImportService.java

License:Open Source License

private ICellCommand copyCell(Cell poiCell, CellReference cellReference, HSSFPalette palette,
        NetxiliaStyleResolver styleResolver) throws FormulaParsingException {

    CellStyle poiStyle = poiCell.getCellStyle();
    Styles styles = PoiUtils.poiStyle2Netxilia(poiStyle,
            poiCell.getSheet().getWorkbook().getFontAt(poiStyle.getFontIndex()), palette, styleResolver);

    IGenericValue value = null;//www  .  j  a v  a  2s.c  om
    Formula formula = null;

    // log.info("CELL TYPE:" + cellReference + " type:" + poiCell.getCellType() + " "
    // + (poiCell.getCellType() == Cell.CELL_TYPE_FORMULA ? poiCell.getCellFormula() : "no"));
    switch (poiCell.getCellType()) {
    // TODO translate errors

    case Cell.CELL_TYPE_STRING:
        value = new StringValue(poiCell.getStringCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(poiCell)) {
            DateTime dt = new DateTime(poiCell.getDateCellValue());
            // TODO decide whether is date or time
            if (dt.isBefore(EXCEL_START)) {
                value = new DateValue(dt.toLocalTime());
            } else if (dt.getMillisOfDay() == 0) {
                value = new DateValue(dt.toLocalDate());
            } else {
                value = new DateValue(dt.toLocalDateTime());
            }
        } else {
            value = new NumberValue(poiCell.getNumericCellValue());
        }
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        value = new BooleanValue(poiCell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        if (poiCell.getCellFormula() != null) {
            formula = formulaParser.parseFormula(new Formula("=" + poiCell.getCellFormula()));
        }
        break;
    }

    if ((styles == null || styles.getItems().isEmpty()) && formula == null
            && (value == null || value.equals(GenericValueUtils.EMTPY_STRING))) {
        return null;
    }
    return CellCommands.cell(new AreaReference(cellReference), value, formula, styles);
}

From source file:org.niord.importer.aton.batch.AbstractDkAtonImportProcessor.java

License:Open Source License

/** Returns the date value of the cell with the given header column key */
Date dateValue(String colKey) {
    Cell cell = row.getCell(colIndex.get(colKey));
    return cell == null ? null : cell.getDateCellValue();
}

From source file:org.niord.importer.aton.batch.AbstractDkAtonImportProcessor.java

License:Open Source License

/** Returns the date value of the cell with the given header column key */
Date dateValueOrNull(String colKey) {
    try {/*from w  w  w  .  jav a  2  s.c  om*/
        Cell cell = row.getCell(colIndex.get(colKey));
        return cell == null ? null : cell.getDateCellValue();
    } catch (Exception e) {
        return null;
    }
}