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:br.com.tiagods.model.Arquivo.java

public String tratarTipo(Cell celula) { //metodo usado para tratar as celulas
    switch (celula.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(celula))
            return sdf.format(celula.getDateCellValue());//campo do tipo data formatando no ato
        else {/*from  ww w .  j a v  a2 s .c o  m*/
            return String.valueOf((long) celula.getNumericCellValue());//campo do tipo numerico, convertendo double para long
        }
    case Cell.CELL_TYPE_STRING:
        return String.valueOf(celula.getStringCellValue());//conteudo do tipo texto
    case Cell.CELL_TYPE_BOOLEAN:
        return "";//conteudo do tipo booleano
    case Cell.CELL_TYPE_BLANK:
        return "";//em branco
    default:
        return "";
    }
}

From source file:businessCharts.readExcell.java

public TreeMap<Double, String> getcontentList(String colName, String colName2) throws Exception {
    TreeMap<Double, String> treemap = new TreeMap<Double, String>();
    initExcel("excel.xlsx");
    XSSFSheet spreadsheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = spreadsheet.iterator();
    XSSFRow row = (XSSFRow) rowIterator.next();
    int setColno = getColNo(colName, row);
    int setColno2 = getColNo(colName2, row);
    while (rowIterator.hasNext()) {
        row = (XSSFRow) rowIterator.next();
        Cell cell = row.getCell(setColno);
        Cell cell2 = row.getCell(setColno2);
        try {/*  w w  w . j  a v a 2s .c  o  m*/
            treemap.put(cell.getNumericCellValue(),
                    new SimpleDateFormat("yyyy-MM-dd").format(cell2.getDateCellValue()));
        } catch (Exception ex) {
            treemap.put(cell.getNumericCellValue(), "nodate");
        }
    }
    return treemap;
}

From source file:businessCharts.readExcell.java

public String[] queryByRowKey(String[] keyStringArray, String colName, String resultColName) throws Exception {
    String[] resultStringArray = new String[keyStringArray.length];
    initExcel("excel.xlsx");
    XSSFSheet spreadsheet = workbook.getSheetAt(0);

    Iterator<Row> rowIterator = spreadsheet.iterator();
    XSSFRow row = (XSSFRow) rowIterator.next();
    int getcellNo = getColNo(colName, row);
    int getReturncellKey = getColNo(resultColName, row);
    System.out.println(/*  w ww. j a va  2  s  .  c  om*/
            "Inside queryByRowKey" + " CellNocolname" + getcellNo + "ReturnCellColName" + getReturncellKey);

    for (int i = 0; i < keyStringArray.length; i++) {
        System.out.println("resultstring array elements" + keyStringArray[i]);
        rowIterator = spreadsheet.iterator();
        row = (XSSFRow) rowIterator.next();
        int flag = 0;
        while (rowIterator.hasNext()) {
            row = (XSSFRow) rowIterator.next();
            Cell cell = row.getCell(getcellNo);
            Cell returnCell = row.getCell(getReturncellKey);
            //    System.out.println("Date is " + returnCell.getDateCellValue());
            //   try{
            if (cell.getNumericCellValue() == Double.parseDouble(keyStringArray[i])) {
                flag = 1;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                //    System.out.println("Date is " + sdf.format(returnCell.getDateCellValue()));
                try {
                    if (returnCell.getDateCellValue() != null)
                        resultStringArray[i] = sdf.format(returnCell.getDateCellValue());
                } catch (Exception ex) {
                    resultStringArray[i] = "No Deadline Available";
                }
            }
            //  }
            // catch(Exception ex){
            //      continue;
            //}
        }
        if (flag == 0) {
            resultStringArray[i] = "Bug Not Available/Resolved";
            flag = 0;
        } else
            flag = 0;
    }

    return resultStringArray;
}

From source file:cn.afterturn.easypoi.excel.imports.CellValueService.java

License:Apache License

/**
 * ??/*from  ww  w . j  a va2 s.c  o m*/
 *
 * @param cell
 * @param entity
 * @return
 */
private Object getCellValue(String classFullName, Cell cell, ExcelImportEntity entity) {
    if (cell == null) {
        return "";
    }
    Object result = null;
    if ("class java.util.Date".equals(classFullName) || "class java.sql.Date".equals(classFullName)
            || ("class java.sql.Time").equals(classFullName)
            || ("class java.time.Instant").equals(classFullName)
            || ("class java.time.LocalDate").equals(classFullName)
            || ("class java.time.LocalDateTime").equals(classFullName)
            || ("class java.sql.Timestamp").equals(classFullName)) {
        //FIX: ?yyyyMMdd cell.getDateCellValue() ?
        if (CellType.NUMERIC == cell.getCellType() && DateUtil.isCellDateFormatted(cell)) {
            result = DateUtil.getJavaDate(cell.getNumericCellValue());
        } else {
            String val = "";
            try {
                val = cell.getStringCellValue();
            } catch (Exception e) {
                cell.setCellType(CellType.STRING);
                val = cell.getStringCellValue();
            }

            result = getDateData(entity, val);
            if (result == null) {
                return null;
            }
        }
        if (("class java.time.Instant").equals(classFullName)) {
            result = ((Date) result).toInstant();
        } else if (("class java.time.LocalDate").equals(classFullName)) {
            result = ((Date) result).toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        } else if (("class java.time.LocalDateTime").equals(classFullName)) {
            result = ((Date) result).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        } else if (("class java.sql.Date").equals(classFullName)) {
            result = new java.sql.Date(((Date) result).getTime());
        } else if (("class java.sql.Time").equals(classFullName)) {
            result = new Time(((Date) result).getTime());
        } else if (("class java.sql.Timestamp").equals(classFullName)) {
            result = new Timestamp(((Date) result).getTime());
        }
    } else {
        switch (cell.getCellType()) {
        case STRING:
            result = cell.getRichStringCellValue() == null ? "" : cell.getRichStringCellValue().getString();
            break;
        case NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                if ("class java.lang.String".equals(classFullName)) {
                    result = formateDate(entity, cell.getDateCellValue());
                }
            } else {
                result = readNumericCell(cell);
            }
            break;
        case BOOLEAN:
            result = Boolean.toString(cell.getBooleanCellValue());
            break;
        case BLANK:
            break;
        case ERROR:
            break;
        case FORMULA:
            try {
                result = readNumericCell(cell);
            } catch (Exception e1) {
                try {
                    result = cell.getRichStringCellValue() == null ? ""
                            : cell.getRichStringCellValue().getString();
                } catch (Exception e2) {
                    throw new RuntimeException("???", e2);
                }
            }
            break;
        default:
            break;
        }
    }
    return result;
}

From source file:cn.edu.zjnu.acm.judge.util.excel.ExcelUtil.java

License:Apache License

private static JsonElement parseAsJsonElement(Cell cell, FormulaEvaluator evaluator) {
    switch (cell.getCellType()) {
    case NUMERIC:
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
            return new JsonPrimitive(DateFormatterHolder.FORMATTER.format(cell.getDateCellValue().toInstant()));
        } else {/*from   w  ww.jav  a2  s.c  om*/
            return new JsonPrimitive(cell.getNumericCellValue());
        }
    case STRING:
        return new JsonPrimitive(cell.getStringCellValue());
    case FORMULA:
        CellValue cellValue = evaluator.evaluate(cell);
        switch (cellValue.getCellType()) {
        case NUMERIC:
            return new JsonPrimitive(cellValue.getNumberValue());
        case STRING:
            return new JsonPrimitive(cellValue.getStringValue());
        case BLANK:
            return new JsonPrimitive("");
        case BOOLEAN:
            return new JsonPrimitive(cellValue.getBooleanValue());
        case ERROR:
        default:
            return null;
        }
    case BLANK:
        return new JsonPrimitive("");
    case BOOLEAN:
        return new JsonPrimitive(cell.getBooleanCellValue());
    case ERROR:
    default:
        return null;
    }
}

From source file:cn.study.innerclass.PoiUtil.java

License:Open Source License

public static Object getCellData(Cell cell, FormulaEvaluator formula) {
    if (cell == null) {
        return null;
    }/*from w  w w  .j  a v  a2  s. c  o m*/
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        System.out.println(cell.getRichStringCellValue().getString());
        return cell.getRichStringCellValue().getString();
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            System.out.println(cell.getDateCellValue());
            return cell.getDateCellValue();
        } else {
            System.out.println(cell.getNumericCellValue());
            return cell.getNumericCellValue();
        }
    case Cell.CELL_TYPE_BOOLEAN:
        System.out.println(cell.getBooleanCellValue());
        return cell.getBooleanCellValue();
    case Cell.CELL_TYPE_FORMULA:
        System.out.println(cell.getStringCellValue());

        switch (formula.evaluate(cell).getCellType()) {
        case Cell.CELL_TYPE_STRING:
            System.out.println(formula.evaluate(cell).getStringValue());
            return formula.evaluate(cell).getStringValue();
        case Cell.CELL_TYPE_NUMERIC:
            System.out.println(formula.evaluate(cell).getNumberValue());
            return formula.evaluate(cell).getNumberValue();
        case Cell.CELL_TYPE_BOOLEAN:
            System.out.println(formula.evaluate(cell).getBooleanValue());
            return formula.evaluate(cell);

        }
    default:
        return null;
    }
}

From source file:co.foldingmap.data.ExcelDataConnector.java

License:Open Source License

/**
 * Returns a cell value as a DataCell object.
 * /* w ww .  java2  s . c o  m*/
 * @param cell
 * @return 
 */
public DataCell getCellText(Cell cell) {
    DataCell cellText;

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        cellText = new DataCell(cell.getRichStringCellValue().getString());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            cellText = new DataCell(cell.getDateCellValue().toString());
        } else {
            cellText = new DataCell(Double.toString(cell.getNumericCellValue()));
        }

        break;
    case Cell.CELL_TYPE_BOOLEAN:
        cellText = new DataCell(Boolean.toString(cell.getBooleanCellValue()));
        break;
    case Cell.CELL_TYPE_FORMULA:
        cellText = new DataCell(cell.getCellFormula());
        break;
    default:
        cellText = new DataCell("");
    }

    return cellText;
}

From source file:com.abixen.platform.service.businessintelligence.multivisualisation.application.service.file.reader.ExcelReaderService.java

License:Open Source License

private String formatIfData(final Cell cell) {
    if (cell.getCellTypeEnum() == CellType.NUMERIC && isCellDateFormatted(cell)) {
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(cell.getDateCellValue());
    }/*  ww  w  .ja va2 s  . c  om*/
    return cell.toString();
}

From source file:com.abixen.platform.service.businessintelligence.multivisualisation.service.impl.parser.ExcelParserServiceImpl.java

License:Open Source License

private DataValue parseAsDate(Cell cell) {
    DataValueDate dataValueDate = new DataValueDate();
    dataValueDate.setValue(cell.getDateCellValue());
    return dataValueDate;
}

From source file:com.accenture.bean.PlanoExcel.java

public void extraiPlanilha() {
    try {/*  ww w. j av  a  2 s.co m*/
        //Leitura
        FileInputStream arquivo = new FileInputStream(new File(fileName));

        // Carregando workbook
        XSSFWorkbook wb = new XSSFWorkbook(arquivo);
        // Selecionando a primeira aba
        XSSFSheet s = wb.getSheetAt(1);

        // Caso queira pegar valor por referencia
        CellReference cellReference = new CellReference("M8");
        Row row = s.getRow(cellReference.getRow());
        Cell cell = row.getCell(cellReference.getCol());
        System.out.println("Valor Refe:" + cell.getStringCellValue());

        // Fazendo um loop em todas as linhas
        for (Row rowFor : s) {
            // FAzendo loop em todas as colunas
            for (Cell cellFor : rowFor) {
                try {
                    // Verifica o tipo de dado
                    if (cellFor.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        // Na coluna 6 tenho um formato de data
                        if (cellFor.getColumnIndex() == 6) {
                            // Se estiver no formato de data
                            if (DateUtil.isCellDateFormatted(cellFor)) {
                                // Formatar para o padrao brasileiro
                                Date d = cellFor.getDateCellValue();
                                DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                                System.out.println(df.format(d));
                            }
                        } else {
                            // Mostrar numerico
                            System.out.println(cellFor.getNumericCellValue());
                        }
                    } else {
                        // Mostrar String
                        System.out.println(cellFor.getStringCellValue());
                    }
                } catch (Exception e) {
                    // Mostrar Erro
                    System.out.println(e.getMessage());
                }
            }
            // Mostrar pulo de linha
            System.out.println("------------------------");
        }

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