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

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

Introduction

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

Prototype

public static boolean isCellDateFormatted(Cell cell) 

Source Link

Document

Check if a cell contains a date Since dates are stored internally in Excel as double values we infer it is a date if it is formatted as such.

Usage

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

License:Apache License

private static OriginalCellType getCellType(final Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return OriginalCellType.DATE;
        }// ww w.java  2s  . c o  m
        return OriginalCellType.NUMERIC;
    case Cell.CELL_TYPE_STRING:
        return OriginalCellType.STRING;
    case Cell.CELL_TYPE_BLANK:
        return OriginalCellType.BLANK;
    case Cell.CELL_TYPE_BOOLEAN:
        return OriginalCellType.BOOLEAN;
    case Cell.CELL_TYPE_ERROR:
        return OriginalCellType.ERROR;
    case Cell.CELL_TYPE_FORMULA:
        return OriginalCellType.FORMULA;
    default:
        throw new UnsupportedOperationException("CellType " + cell.getCellType() + " is invalid");
    }
}

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

License:Apache License

@Override
public Boolean fromSpreadsheet(final Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        final String value = cell.getRichStringCellValue().getString().trim();

        Boolean ret = null;//from  w ww .  ja v  a2s.c  om
        for (final String str : TRUE_VALUES) {
            if (str.equalsIgnoreCase(value)) {
                ret = Boolean.TRUE;
                break;
            }
        }

        if (ret == null) {
            for (final String str : FALSE_VALUES) {
                if (str.equalsIgnoreCase(value)) {
                    ret = Boolean.FALSE;
                    break;
                }
            }
        }

        if (ret == null) {
            throw new InvalidCellValueException();
        }

        return ret;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            throw new InvalidCellValueException();
        }

        return cell.getNumericCellValue() > 0 ? Boolean.TRUE : Boolean.FALSE;
    case Cell.CELL_TYPE_BOOLEAN:
        return Boolean.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.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 w  w w . ja v  a2  s.  c om*/
            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.SpreadsheetDoubleAdapter.java

License:Apache License

@Override
public Double fromSpreadsheet(final Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        try {/* w  w w  .  ja  va2  s  . c  om*/
            // remove trailing spaces and replace comma with period
            final String value = cell.getRichStringCellValue().getString().trim().replace(',', '.');
            return Double.valueOf(value);
        } catch (final NumberFormatException e) {
            throw new InvalidCellValueException();
        }
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            throw new InvalidCellValueException();
        } else {
            return Double.valueOf(cell.getNumericCellValue());
        }
    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.SpreadsheetIntegerAdapter.java

License:Apache License

@Override
public Integer fromSpreadsheet(final Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        try {//  ww w . jav  a2 s . c o m
            return Integer.valueOf(cell.getRichStringCellValue().getString().trim());
        } catch (final NumberFormatException e) {
            throw new InvalidCellValueException();
        }
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            throw new InvalidCellValueException();
        } else {
            final double value = cell.getNumericCellValue();
            final double floor = Math.floor(value);
            if (value == floor) {
                return Integer.valueOf((int) value);
            } else {
                throw new InvalidCellValueException();
            }
        }
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_BOOLEAN:
    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.SpreadsheetLongAdapter.java

License:Apache License

@Override
public Long fromSpreadsheet(final Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        try {//w w w. java  2 s  . c o m
            return Long.valueOf(cell.getRichStringCellValue().getString().trim());
        } catch (final NumberFormatException e) {
            throw new InvalidCellValueException();
        }
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            throw new InvalidCellValueException();
        } else {
            final double value = cell.getNumericCellValue();
            final double floor = Math.floor(value);
            if (value == floor) {
                return Long.valueOf((long) value);
            } else {
                throw new InvalidCellValueException();
            }
        }
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_BOOLEAN:
    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 {/*  w  w w  .  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  www. j ava2 s  . c  om*/
 *
 * @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

/**
 * @param cell/*from  w ww .  j  a  v a  2s .  c  o m*/
 *            the cell
 * @return true if it's a (fake) date cell
 */
protected boolean isDate(final Cell cell) {
    return cell.getCellType() == Cell.CELL_TYPE_NUMERIC && DateUtil.isCellDateFormatted(cell);
}

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

License:Open Source License

/**
 * @param cell/*from ww  w  . j a va2s . co m*/
 *            the cell
 * @return true if it's not a (fake) date cell
 */
protected boolean isDouble(final Cell cell) {
    return cell.getCellType() == Cell.CELL_TYPE_NUMERIC && !DateUtil.isCellDateFormatted(cell);
}