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.framework.common.ExcelSpreadsheet.java

public List<String> getSpreadSheetAsArray() {

    List<String> cellValue = new ArrayList<String>();

    // Iterate through each rows from first sheet
    Iterator<Row> rowIterator = requiredWorksheet.iterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();/*from   w ww.ja  v  a  2 s.  c  o m*/

        // For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext()) {

            Cell cell = cellIterator.next();

            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                //                        System.out.print(cell.getBooleanCellValue() + "\t\t");
                if (cell.getBooleanCellValue()) {
                    cellValue.add("TRUE");
                } else {
                    cellValue.add("FALSE");
                }

                break;
            case Cell.CELL_TYPE_NUMERIC:
                //                        System.out.print(cell.getNumericCellValue() + "\t\t");

                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    Format formatter = new SimpleDateFormat("yyyy-MM-dd");
                    String s = formatter.format(cell.getDateCellValue());
                    cellValue.add(s);
                } else {
                    double value = cell.getNumericCellValue();
                    cellValue.add(Double.toString(value));
                }

                break;
            case Cell.CELL_TYPE_STRING:
                //                        System.out.print(cell.getStringCellValue() + "\t\t");
                cellValue.add(cell.getStringCellValue());
                break;
            }
        }
        //            System.out.println("");
    }
    return cellValue;
}

From source file:com.github.camaral.sheeco.exceptions.SpreadsheetViolation.java

License:Apache License

private static Object getCellValue(final Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        }/*from  ww  w .  j  a  v a2 s  . c  o m*/
        return cell.getNumericCellValue();
    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue();
    case Cell.CELL_TYPE_ERROR:
        return cell.getErrorCellValue();
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    default:
        throw new UnsupportedOperationException("CellType " + cell.getCellType() + " is invalid");
    }
}

From source file:com.github.camaral.sheeco.type.adapter.SpreadsheetDateAdapter.java

License:Apache License

@Override
public Date fromSpreadsheet(final Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        } else {//from  ww  w.  ja  va 2 s .c o m
            throw new InvalidCellValueException();
        }
    case Cell.CELL_TYPE_STRING:

        for (final SimpleDateFormat format : DATE_PATTERNS) {
            try {
                return format.parse(cell.getRichStringCellValue().getString().trim());
            } catch (final ParseException e) {
                continue;
            }
        }
        throw new InvalidCellValueException();
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_BOOLEAN:
        throw new InvalidCellValueException();
    case Cell.CELL_TYPE_ERROR:
    case Cell.CELL_TYPE_FORMULA:
    default:
        throw new InvalidCellFormatException(
                "The cell type: " + cell.getCellType() + " is either not supported or not possible");
    }
}

From source file:com.github.camaral.sheeco.type.adapter.SpreadsheetStringAdapter.java

License:Apache License

@Override
public String fromSpreadsheet(final Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        final RichTextString text = cell.getRichStringCellValue();
        final String value = text.getString().trim();
        return !value.isEmpty() ? value : null;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return String.valueOf(SpreadsheetDateAdapter.DATE_PATTERNS[0].format(cell.getDateCellValue()));
        } else {//from   w  ww .  ja  v  a  2 s .  c  o  m
            return decimalFormat.format(cell.getNumericCellValue());
        }
    case Cell.CELL_TYPE_BOOLEAN:
        return String.valueOf(cell.getBooleanCellValue());
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_ERROR:
    case Cell.CELL_TYPE_FORMULA:
    default:
        throw new InvalidCellFormatException(
                "The cell type: " + cell.getCellType() + " is either not supported or not possible");
    }
}

From source file:com.github.crab2died.utils.Utils.java

License:Open Source License

/**
 * ??/*from w w w .  j  a v a 2  s  . c o m*/
 *
 * @param c ?
 * @return ?
 */
public static String getCellValue(Cell c) {
    String o;
    switch (c.getCellTypeEnum()) {
    case BLANK:
        o = "";
        break;
    case BOOLEAN:
        o = String.valueOf(c.getBooleanCellValue());
        break;
    case FORMULA:
        o = calculationFormula(c);
        break;
    case NUMERIC:
        if (DateUtil.isCellDateFormatted(c)) {
            o = DateUtils.date2Str(c.getDateCellValue());
        } else {
            o = String.valueOf(c.getNumericCellValue());
            o = matchDoneBigDecimal(o);
            o = RegularUtils.converNumByReg(o);
        }
        break;
    case STRING:
        o = c.getStringCellValue();
        break;
    default:
        o = null;
        break;
    }
    return o;
}

From source file:com.github.jferard.spreadsheetwrapper.xls.poi.XlsPoiWriter.java

License:Open Source License

/** {@inheritDoc} */
@Override//from  w  ww.ja  va 2s.c o  m
public/*@Nullable*/Object getCellContent(final int rowIndex, final int colIndex) {
    final Cell cell = this.getPOICell(rowIndex, colIndex);
    if (cell == null)
        return null;

    Object result;
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        result = null;
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        result = Boolean.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        result = "#Err";
        break;
    case Cell.CELL_TYPE_FORMULA:
        result = cell.getCellFormula();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (this.isDate(cell)) {
            result = cell.getDateCellValue();
        } else {
            final double value = cell.getNumericCellValue();
            if (value == Math.rint(value))
                result = Integer.valueOf((int) value);
            else
                result = value;
        }
        break;
    case Cell.CELL_TYPE_STRING:
        result = cell.getStringCellValue();
        break;
    default:
        throw new IllegalArgumentException(String.format("Unknown type of cell %d", cell.getCellType()));
    }
    return result;
}

From source file:com.github.jferard.spreadsheetwrapper.xls.poi.XlsPoiWriter.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from  w ww.j ava2  s  .co  m*/
public/*@Nullable*/Date getDate(final int r, final int c) {
    final Cell cell = this.getPOICell(r, c);
    if (cell == null || !this.isDate(cell))
        return null;

    return cell.getDateCellValue();
}

From source file:com.haulmont.mp2xls.helper.XlsHelper.java

License:Apache License

public static Object getCellValue(Cell cell) {
    if (cell == null) {
        return null;
    }/*from ww  w.  j  a  v  a2s  .c  o m*/

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_STRING:
        String formattedCellValue = cell.getStringCellValue().replace(String.valueOf(NON_BREAKING_SPACE), " ")
                .trim();
        return formattedCellValue.isEmpty() ? null : formattedCellValue;
    case Cell.CELL_TYPE_NUMERIC:
        if (isDateCell(cell)) {
            return cell.getDateCellValue();
        }

        Double numericCellValue = cell.getNumericCellValue();
        return isAlmostInt(numericCellValue) ? numericCellValue.intValue() : numericCellValue;
    case Cell.CELL_TYPE_FORMULA:
        /*
        formattedCellValue = cell.getStringCellValue();
        if (formattedCellValue != null) {
            formattedCellValue = formattedCellValue.replace(String.valueOf(NON_BREAKING_SPACE), " ").trim();
        }
        */
        return getFormulaCellValue(cell/*, formattedCellValue*/);
    default:
        throw new CellTypeIsNotSupportedException(cell);
    }
}

From source file:com.heimaide.server.common.utils.excel.ImportExcel.java

License:Open Source License

/**
 * ??//from   w w w . j  a v  a2  s .  c  o m
 * @param row ?
 * @param column ???
 * @return ?
 */
public Object getCellValue(Row row, int column) {
    Object val = "";
    try {
        Cell cell = row.getCell(column);
        if (cell != null) {
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                val = cell.getNumericCellValue();
            } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                val = cell.getStringCellValue();
            } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
                try {
                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                        val = cell.getDateCellValue();
                    } else {
                        val = String.valueOf(cell.getNumericCellValue());
                    }
                } catch (IllegalStateException e) {
                    val = String.valueOf(cell.getRichStringCellValue());
                }
            } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                val = cell.getBooleanCellValue();
            } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
                val = cell.getErrorCellValue();
            }
        }
    } catch (Exception e) {
        return val;
    }
    return val;
}

From source file:com.helger.poi.excel.ExcelReadUtils.java

License:Apache License

@Nullable
public static Date getCellValueJavaDate(@Nullable final Cell aCell) {
    if (aCell != null)
        try {/*  w ww.  j a va  2 s  . c  o m*/
            return aCell.getDateCellValue();
        } catch (final RuntimeException ex) {
            // fall through
            s_aLogger.warn("Failed to get cell value as date: " + ex.getMessage());
        }
    return null;
}