Example usage for org.apache.poi.ss.usermodel Cell getNumericCellValue

List of usage examples for org.apache.poi.ss.usermodel Cell getNumericCellValue

Introduction

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

Prototype

double getNumericCellValue();

Source Link

Document

Get the value of the cell as a number.

Usage

From source file:com.dickimawbooks.datatooltk.io.DatatoolExcel.java

License:Open Source License

private String getCellValue(Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:
    case Cell.CELL_TYPE_FORMULA:
        return "" + cell.getNumericCellValue();
    case Cell.CELL_TYPE_BLANK:
        return "";
    }//from  ww w .ja  v a  2 s .  c om

    String value = cell.toString();

    if (value == null) {
        return "\\@dtlnovalue ";
    }

    return mapFieldIfRequired(value);
}

From source file:com.dituiba.excel.BaseExcelService.java

License:Apache License

/**
 * ??hash?//  ww w. j a va 2 s.c o  m
 * @param sheet
 * @return
 */
public static long getHashVal(Sheet sheet) {
    Row sheetRow = sheet.getRow(HASH_ROW);
    Cell cell = sheetRow.getCell(0);
    return ((Double) cell.getNumericCellValue()).longValue();
}

From source file:com.dituiba.excel.DefaultInputAdapter.java

License:Apache License

/**
 * ?/*from w  ww .  j av  a  2s. c o  m*/
 *
 * @param type
 * @param fieldName
 * @param cell
 * @throws IllegalAccessException
 * @throws ParseException
 */
public Object inputDateAdapter(DataBean dataBean, Class type, String fieldName, Cell cell)
        throws AdapterException {
    log.debug("in DefaultInputAdapter:inputDateAdapter fieldName:{} type:{}", fieldName, type.getSimpleName());
    InputDateConfig inputDateConfig = dataBean.getInputConfig(fieldName);
    Object o = null;
    if (Cell.CELL_TYPE_BLANK == cell.getCellType()) {
        log.debug("cell is blank ");
        return o;
    } else if (Date.class.isAssignableFrom(type)) {
        if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
            Date date = cell.getDateCellValue();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int i = calendar.get(Calendar.YEAR);
            try { //???2014026??
                o = i > 2500 ? DateUtil.formatToDate(String.format("%.0f", cell.getNumericCellValue()),
                        inputDateConfig.format()) : date;
            } catch (ParseException e) {
                throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell);
            }
        } else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
            try {
                o = DateUtil.formatToDate(trim(cell.getStringCellValue()), inputDateConfig.format());
            } catch (ParseException e) {
                throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell);
            }
        } else if (Cell.CELL_TYPE_BLANK == cell.getCellType()) {
            return null;
        } else {
            throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell);
        }
    } else if (String.class.isAssignableFrom(type)) {
        if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
            Date dateCellValue = cell.getDateCellValue();
            o = DateUtil.format(dateCellValue, inputDateConfig.format());
        } else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
            OutputDateConfig outputDateConfig = dataBean.getOutputConfig(fieldName);
            try {
                o = DateUtil.format(trim(cell.getStringCellValue()), outputDateConfig.format(),
                        inputDateConfig.format());
            } catch (ParseException e) {
                throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell);
            }
        } else {
            throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell);
        }
    } else {
        throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell);
    }
    return o;
}

From source file:com.dituiba.excel.DefaultInputAdapter.java

License:Apache License

public static Object getCellValue(Cell cell, Class type) throws AdapterException {
    if (cell == null)
        return "";
    try {/* w w  w  .ja  v a 2s.c  om*/
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_BOOLEAN:
            return cell.getBooleanCellValue();
        case Cell.CELL_TYPE_BLANK:
            return "";
        case Cell.CELL_TYPE_NUMERIC:
            if (String.class.isAssignableFrom(type)) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
                return trim(cell.getStringCellValue());
            } else if (Date.class.isAssignableFrom(type)) {
                return cell.getDateCellValue();
            }
            return cell.getNumericCellValue();
        case Cell.CELL_TYPE_STRING:
            return trim(cell.getStringCellValue());
        case Cell.CELL_TYPE_FORMULA:
            try {
                return String.valueOf(cell.getNumericCellValue());
            } catch (IllegalStateException e) {
                return trim(String.valueOf(cell.getRichStringCellValue()));
            }
        }
    } catch (Exception e) {
        throw new AdapterException(Message.INPUT_CELL_DATA_ERROR, cell);
    }
    throw new AdapterException(Message.INPUT_CELL_DATA_ERROR, cell);
}

From source file:com.dituiba.excel.ExcelUtility.java

License:Apache License

public static void copyCell(Cell srcCell, Cell distCell) {
    distCell.setCellStyle(srcCell.getCellStyle());
    if (srcCell.getCellComment() != null) {
        distCell.setCellComment(srcCell.getCellComment());
    }//from  w ww.ja  v  a 2 s.  co  m
    int srcCellType = srcCell.getCellType();
    distCell.setCellType(srcCellType);

    if (srcCellType == Cell.CELL_TYPE_NUMERIC) {
        if (DateUtil.isCellDateFormatted(srcCell)) {
            distCell.setCellValue(srcCell.getDateCellValue());
        } else {
            distCell.setCellValue(srcCell.getNumericCellValue());
        }
    } else if (srcCellType == Cell.CELL_TYPE_STRING) {
        distCell.setCellValue(srcCell.getRichStringCellValue());
    } else if (srcCellType == Cell.CELL_TYPE_BLANK) {
        //nothing
    } else if (srcCellType == Cell.CELL_TYPE_BOOLEAN) {
        distCell.setCellValue(srcCell.getBooleanCellValue());
    } else if (srcCellType == Cell.CELL_TYPE_ERROR) {
        distCell.setCellErrorValue(srcCell.getErrorCellValue());
    } else if (srcCellType == Cell.CELL_TYPE_FORMULA) {
        distCell.setCellFormula(srcCell.getCellFormula());
    } else {
        //nothing
    }
}

From source file:com.dituiba.excel.ImportTableService.java

License:Apache License

/**
 * ExcelTableBean/*w  w  w  . j  a  va2s . c  o  m*/
 */
public void doImport() {
    int rowNum = sheet.getLastRowNum() + 1;
    int columnNum = 0;
    for (int i = 0; i < rowNum; i++) {
        if (sheet.getRow(i) != null) {
            int last = sheet.getRow(i).getLastCellNum();
            columnNum = last > columnNum ? last : columnNum;
        }
    }
    tableBean = new TableBean(rowNum, columnNum);
    Collection<CellBean> cellBeans = new ArrayList<CellBean>();
    for (int r = startRow; r < rowNum; r++) {
        Row row = sheet.getRow(r);
        if (row != null) {
            for (int c = 0; c < row.getLastCellNum(); c++) {
                Cell cell = row.getCell(c);
                if (cell != null) {
                    String cellValue = null;
                    if (cellHandlerMap.containsKey(c)) {
                        cellValue = cellHandlerMap.get(c).readCell(cell) + "";
                    } else {
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        Integer type = forceCellType.get(c);
                        if (type != null) {
                            cell.setCellType(type);
                        }
                        if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
                            cellValue = cell.getBooleanCellValue() + "";
                        } else if (Cell.CELL_TYPE_FORMULA == cell.getCellType()) {
                            try {
                                cellValue = String.valueOf(cell.getNumericCellValue());
                            } catch (IllegalStateException e) {
                                cellValue = String.valueOf(cell.getRichStringCellValue()).trim();
                            }
                        } else if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
                            if (DateUtil.isCellDateFormatted(cell)) {
                                Date date2 = cell.getDateCellValue();
                                SimpleDateFormat dff = new SimpleDateFormat(dateFormat);
                                cellValue = dff.format(date2); //
                            } else {
                                cellValue = String.valueOf(cell.getNumericCellValue());
                            }
                        } else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
                            cellValue = cell.getStringCellValue();
                        }
                        if (cellValue != null && cellValue instanceof String) {
                            cellValue = cellValue.toString().trim();
                        }
                    }
                    CellBean cellBean = new CellBean(cellValue, r, c);
                    cellBean.setCell(cell);
                    cellBeans.add(cellBean);
                }
            }
        }
    }
    tableBean.setCellBeans(cellBeans);
}

From source file:com.dotosoft.dotoquiz.tools.util.DotoQuizStructure.java

License:Apache License

private static String readCellAsString(Cell cell, String defaultValue) {
    String result = defaultValue;
    if (cell == null)
        return result;

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:
        result = String.valueOf(cell.getNumericCellValue()).replace(".0", "");
        break;//from  www.  j av  a  2 s . com
    case Cell.CELL_TYPE_STRING:
        result = cell.getStringCellValue();
        break;
    }

    return result;
}

From source file:com.ebay.xcelite.reader.SheetReaderAbs.java

License:Apache License

protected Object readValueFromCell(Cell cell) {
    if (cell == null)
        return null;
    Object cellValue = null;/*from ww  w.ja  v a  2s .c  om*/
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN:
        cellValue = cell.getBooleanCellValue();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        cellValue = cell.getNumericCellValue();
        break;
    default:
        cellValue = cell.getStringCellValue();
    }
    return cellValue;
}

From source file:com.efficio.fieldbook.web.nursery.service.impl.ExcelImportStudyServiceImpl.java

License:Open Source License

private void importDataToWorkbook(HSSFWorkbook xlsBook, Workbook workbook) {
    if (workbook.getObservations() != null) {
        HSSFSheet observationSheet = xlsBook.getSheetAt(1);
        int xlsRowIndex = 1; //row 0 is the header row
        for (MeasurementRow wRow : workbook.getObservations()) {
            HSSFRow xlsRow = observationSheet.getRow(xlsRowIndex);
            for (MeasurementData wData : wRow.getDataList()) {
                String label = wData.getLabel();
                int xlsColIndex = findColumn(observationSheet, label);
                Cell cell = xlsRow.getCell(xlsColIndex);
                String xlsValue = "";

                if (cell != null) {
                    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        Double doubleVal = Double.valueOf(cell.getNumericCellValue());
                        Integer intVal = Integer.valueOf(doubleVal.intValue());
                        if (Double.parseDouble(intVal.toString()) == doubleVal.doubleValue()) {
                            xlsValue = intVal.toString();
                        } else {
                            xlsValue = doubleVal.toString();
                        }//from   ww  w. j  ava 2  s.c o  m

                    } else
                        xlsValue = cell.getStringCellValue();
                }
                wData.setValue(xlsValue);
            }
            xlsRowIndex++;
        }
    }
}

From source file:com.efficio.fieldbook.web.nursery.service.impl.ExcelImportStudyServiceImpl.java

License:Open Source License

private Integer getExcelValueInt(HSSFRow row, int columnIndex) {
    Cell cell = row.getCell(columnIndex);
    String xlsStr = "";
    if (cell.getCellType() == Cell.CELL_TYPE_STRING)
        xlsStr = cell.getStringCellValue();
    else/*from   w w  w  . j  av a  2s  .  com*/
        xlsStr = String.valueOf((int) cell.getNumericCellValue());
    if (NumberUtils.isNumber(xlsStr)) {
        return Integer.valueOf(xlsStr);
    }
    return null;
}