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:net.ceos.project.poi.annotated.core.CellHandler.java

License:Apache License

/**
 * Read a date value from the Cell./* w w  w.  j  a v  a2 s.  c  o  m*/
 * 
 * @param object
 *            the object
 * @param field
 *            the {@link Field} to set
 * @param cell
 *            the {@link Cell} to read
 * @param xlsAnnotation
 *            the {@link XlsElement} element
 * @throws ConverterException
 *             the conversion exception type
 */
protected static void dateReader(final Object object, final Field field, final Cell cell,
        final XlsElement xlsAnnotation) throws ConverterException {
    if (StringUtils.isNotBlank(readCell(cell))) {
        try {
            if (StringUtils.isBlank(xlsAnnotation.transformMask())) {
                field.set(object, cell.getDateCellValue());
            } else {
                String date = cell.getStringCellValue();

                String tM = xlsAnnotation.transformMask();
                String fM = xlsAnnotation.formatMask();
                String decorator = StringUtils.isEmpty(tM)
                        ? (StringUtils.isEmpty(fM) ? CellStyleHandler.MASK_DECORATOR_DATE : fM)
                        : tM;

                SimpleDateFormat dt = new SimpleDateFormat(decorator);

                Date dateConverted = dt.parse(date);
                field.set(object, dateConverted);
            }
        } catch (ParseException | IllegalArgumentException | IllegalAccessException e) {
            /*
             * if date decorator do not match with a valid mask launch
             * exception
             */
            throw new ConverterException(ExceptionMessage.CONVERTER_DATE.getMessage(), e);
        }
    }
}

From source file:net.ceos.project.poi.annotated.core.CellHandler.java

License:Apache License

/**
 * Read a date time value from the Cell.
 * /*from   ww  w .ja va  2 s  . c o  m*/
 * @param object
 *            the object
 * @param field
 *            the {@link Field} to set
 * @param cell
 *            the {@link Cell} to read
 * @param xlsAnnotation
 *            the {@link XlsElement} element
 * @throws ConverterException
 *             the conversion exception type
 */
protected static void localDateReader(final Object object, final Field field, final Cell cell,
        final XlsElement xlsAnnotation) throws ConverterException {

    if (StringUtils.isNotBlank(readCell(cell))) {
        try {
            if (StringUtils.isBlank(xlsAnnotation.transformMask())) {
                field.set(object,
                        cell.getDateCellValue().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
            } else {
                String date = cell.getStringCellValue();

                String tM = xlsAnnotation.transformMask();
                String fM = xlsAnnotation.formatMask();
                String decorator = StringUtils.isEmpty(tM)
                        ? (StringUtils.isEmpty(fM) ? CellStyleHandler.MASK_DECORATOR_DATE : fM)
                        : tM;

                SimpleDateFormat dt = new SimpleDateFormat(decorator);

                Date dateConverted = dt.parse(date);
                field.set(object, dateConverted.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
            }
        } catch (ParseException | IllegalArgumentException | IllegalAccessException e) {
            /*
             * if date decorator do not match with a valid mask launch
             * exception
             */
            throw new ConverterException(ExceptionMessage.CONVERTER_LOCALDATE.getMessage(), e);
        }
    }
}

From source file:net.ceos.project.poi.annotated.core.CellHandler.java

License:Apache License

/**
 * Read a date time value from the Cell.
 * //from w w w .  ja v  a 2s . c o m
 * @param object
 *            the object
 * @param field
 *            the {@link Field} to set
 * @param cell
 *            the {@link Cell} to read
 * @param xlsAnnotation
 *            the {@link XlsElement} element
 * @throws ConverterException
 *             the conversion exception type
 */
protected static void localDateTimeReader(final Object object, final Field field, final Cell cell,
        final XlsElement xlsAnnotation) throws ConverterException {
    if (StringUtils.isNotBlank(readCell(cell))) {
        try {
            if (StringUtils.isBlank(xlsAnnotation.transformMask())) {
                field.set(object,
                        cell.getDateCellValue().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
            } else {
                String date = cell.getStringCellValue();

                String tM = xlsAnnotation.transformMask();
                String fM = xlsAnnotation.formatMask();
                String decorator = StringUtils.isEmpty(tM)
                        ? (StringUtils.isEmpty(fM) ? CellStyleHandler.MASK_DECORATOR_DATE : fM)
                        : tM;

                SimpleDateFormat dt = new SimpleDateFormat(decorator);
                Date dateConverted = dt.parse(date);
                field.set(object, dateConverted.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
            }
        } catch (ParseException | IllegalArgumentException | IllegalAccessException e) {
            /*
             * if date decorator do not match with a valid mask launch
             * exception
             */
            throw new ConverterException(ExceptionMessage.CONVERTER_LOCALDATETIME.getMessage(), e);
        }
    }
}

From source file:net.ceos.project.poi.annotated.core.MaskTest.java

License:Apache License

/**
 * /*from   ww  w.  j a va2  s  . c o  m*/
 * Test the marshal/unmarshal of one object applying mask at the Date
 * attribute with {@link XlsElement}
 */
@Test(dataProvider = "dataProvider")
public void checkDateFormatMask(ObjectMask charger, Workbook wb) throws Exception {
    // format date attributes
    Cell cellDate1 = extractCell(charger, wb.getSheetAt(0), 2);
    assertEquals(cellDate1.getDateCellValue(), charger.getDateAttribute2());

    Cell cellDate2 = extractCell(charger, wb.getSheetAt(0), 3);
    assertEquals(cellDate2.getDateCellValue(), charger.getDateAttribute3());

    Cell cellDate3 = extractCell(charger, wb.getSheetAt(0), 4);
    assertEquals(cellDate3.getDateCellValue(), charger.getDateAttribute4());
}

From source file:net.duckling.ddl.util.ExcelReader.java

License:Apache License

/**
 * /*  ww  w .  j av a  2s  .com*/
 * ?Cell?
 * 
 * @param cell
 * @return
 */
private Object getCellFormatValue(Cell cell) {
    Object cellvalue = "";
    if (cell != null) {
        // ?CellType
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:// ?CellTypeNUMERIC
        case Cell.CELL_TYPE_FORMULA: {
            // ?cell?Date
            if (DateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                cellvalue = date;
            } else {// 
                // ??Cell
                cellvalue = String.valueOf(cell.getNumericCellValue());
            }
            break;
        }
        case Cell.CELL_TYPE_STRING:// ?CellTypeSTRING
            // ??Cell
            cellvalue = cell.getRichStringCellValue().getString();
            break;
        default:// Cell
            cellvalue = "";
        }
    } else {
        cellvalue = "";
    }
    return cellvalue;
}

From source file:net.lizhaoweb.maker.code.java.model.excel.read.ExcelFileReader.java

License:Open Source License

private Set<FieldInformation> analysisSheet(Configuration configuration, Sheet sheet) {
    if (configuration == null) {
        throw new IllegalArgumentException("The configuration is null");
    }//from  ww  w  .ja v a2 s  . com
    if (sheet == null) {
        throw new IllegalArgumentException("The sheet is null");
    }

    // Sheet 
    int rowSize = sheet.getLastRowNum();
    if (rowSize < 1) {
        throw new IllegalArgumentException("The sheet rows number less than 1");
    }

    // Sheet 
    Row titleRow = sheet.getRow(0);
    if (titleRow == null) {
        throw new IllegalArgumentException("The sheet title row is not found");
    }

    // Sheet 
    int columnSize = titleRow.getLastCellNum();
    if (columnSize < 1) {
        throw new IllegalArgumentException("The sheet columns number less than 1");
    }

    // ?
    Map<Integer, FieldInformation> fieldInformationMap = new HashMap<Integer, FieldInformation>();

    // ??
    ExecutorService fieldNameExecutorService = Executors.newCachedThreadPool();
    List<Future<Map<Integer, String>>> fieldNameFutureList = new ArrayList<Future<Map<Integer, String>>>();

    // ? Sheet 
    for (int columnIndex = 0; columnIndex < columnSize; columnIndex++) {
        FieldInformation fieldInformation = new FieldInformation();
        Cell cell = titleRow.getCell(columnIndex);
        String titleContent = cell.getStringCellValue();
        fieldInformation.setTitle(titleContent);
        fieldInformation.setDescribe(titleContent);
        fieldInformation.setType("String");
        fieldInformationMap.put(columnIndex, fieldInformation);

        // ???
        Future<Map<Integer, String>> classNameFuture = fieldNameExecutorService
                .submit(new TranslateCallable(this.getTranslator(), configuration, columnIndex, titleContent));
        fieldNameFutureList.add(classNameFuture);
    }

    // ? Sheet ?
    for (int rowIndex = 1; rowIndex <= rowSize; rowIndex++) {
        Row dataRow = sheet.getRow(rowIndex);
        if (dataRow == null) {// 
            continue;
        }
        if (dataRow.getLastCellNum() < columnSize) {// ?
            continue;
        }
        for (int columnIndex = 0; columnIndex < columnSize; columnIndex++) {
            FieldInformation fieldInformation = fieldInformationMap.get(columnIndex);
            Cell cell = dataRow.getCell(columnIndex);
            CellType cellType = cell.getCellTypeEnum();
            if (CellType.BOOLEAN == cellType) {
                fieldInformation.setType("java.lang.Boolean");
            } else if (CellType.NUMERIC == cellType) {
                try {
                    Date dateValue = cell.getDateCellValue();
                    if (dateValue == null) {
                        fieldInformation.setType("java.lang.Double");
                    } else {
                        fieldInformation.setType("java.util.Date");
                    }
                } catch (Exception e) {
                    fieldInformation.setType("java.lang.Double");
                }
            }
        }
    }

    // ??
    for (Future<Map<Integer, String>> future : fieldNameFutureList) {
        try {
            Map<Integer, String> fieldNameMap = future.get();
            for (Map.Entry<Integer, String> fieldNameMapEntry : fieldNameMap.entrySet()) {
                Integer key = fieldNameMapEntry.getKey();
                String value = fieldNameMapEntry.getValue();
                fieldInformationMap.get(key).setName(StringUtil.uncapitalize(value));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    fieldNameExecutorService.shutdown();

    Set<FieldInformation> result = new HashSet<FieldInformation>(fieldInformationMap.values());
    return result;
}

From source file:net.mcnewfamily.rmcnew.model.Util.java

License:Open Source License

public static String getCellValueAsStringOrEmptyString(Cell cell) {
    if (cell == null) {
        return "";
    }//ww w . j  a  v  a2 s  . c om
    String value;
    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;
    default:
        value = "";
    }
    return value;
}

From source file:net.morphbank.loadexcel.SheetReader.java

License:Open Source License

public String getEntry(String sheetName, int col, int row) {
    Sheet sheet = getSheet(sheetName);/*from w  w  w  . j a  v  a2 s . c om*/
    if (sheet == null)
        return "";

    if (sheet.getRow(row).getCell(col) == null)
        return "";
    Cell cell = sheet.getRow(row).getCell(col);
    if (cell.getCellType() != Cell.CELL_TYPE_NUMERIC) {
        return cell.getStringCellValue();
    }
    // must be numeric
    // Date
    if (DateUtil.isCellDateFormatted(cell)) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(cell.getDateCellValue());
    }
    double value = cell.getNumericCellValue();
    if ((value % 1) == 0) {
        // integer
        return INTEGER_FORMATTER.format(value);
    }
    // float
    return DOUBLE_FORMATTER.format(value);
}

From source file:net.morphbank.mbsvc3.mapsheet.XlsFieldMapper.java

License:Open Source License

public void readHeaders() {

    Row row = views.getRow(0);//from  w ww  . ja va 2s .c  o  m
    numFields = views.getRow(0).getLastCellNum();
    if (numFields > 0) {
        headers = new String[numFields];
    } else {
        headers = new String[1];
    }
    for (Cell cell : row) {
        int index = cell.getColumnIndex();
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            headers[index] = cell.getStringCellValue().toLowerCase();
            break;
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                headers[index] = cell.getDateCellValue().toString();
            } else {
                headers[index] = Integer.toString((int) cell.getNumericCellValue());
            }
        }
    }
    currentLine = 0;
}

From source file:net.morphbank.mbsvc3.mapsheet.XlsFieldMapper.java

License:Open Source License

public String getValue(int index) {
    String retValue = "";
    Row row = views.getRow(currentLine);
    Cell cell = null;
    if (null != row) {
        cell = row.getCell(index);/*w  w  w  . j a v a 2  s  .c o  m*/
    }
    if (null == cell) {
        return retValue;
    }

    int cellType = cell.getCellType();
    // find cell type for formula
    if (cellType == Cell.CELL_TYPE_FORMULA) {
        cellType = cell.getCachedFormulaResultType();
    }
    switch (cellType) {
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            retValue = cell.getDateCellValue().toString();
        } else {
            double value = cell.getNumericCellValue();
            if (Math.floor(value) == value) {
                retValue = Integer.toString((int) value);
            } else {
                retValue = doubleFormatter.format(value);
            }
        }
        break;

    case Cell.CELL_TYPE_STRING:
        retValue = cell.getStringCellValue();
        break;

    }
    return retValue;
}