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.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 {/*from  w w  w. jav a 2 s .  com*/
            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 {//from ww  w . j av a2s. co  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 ww  w . j  a v a  2 s . co 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.cutstock.excel.ExcelWriterSupport.java

License:Apache License

protected String getCellValue(Cell cell) {
    if (cell != null) {
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_FORMULA:
            return "  " + cell.getCellFormula();
        case HSSFCell.CELL_TYPE_NUMERIC:
            return "" + cell.getNumericCellValue();
        case HSSFCell.CELL_TYPE_STRING:
            return "" + cell.getRichStringCellValue();
        default://w ww.  jav a  2s. c  o  m
            return "";
        }
    }
    return "";
}

From source file:com.github.cutstock.utils.ProfileUtils.java

License:Apache License

public static Object getCellValue(Cell cell) {
    if (cell != null) {
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_FORMULA:
            return cell.getCellFormula();
        case HSSFCell.CELL_TYPE_NUMERIC:
            return cell.getNumericCellValue();
        case HSSFCell.CELL_TYPE_STRING:
            return "" + cell.getRichStringCellValue();
        default:/*from  ww w . j av a 2  s  .  c o  m*/
            return "";// ?empty
        }
    }
    return "";
}

From source file:com.github.drbookings.excel.FileFormatBookingXLS.java

License:Open Source License

public static int getBookingNumber(final Cell c) {
    if (c != null) {
        if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            return (int) c.getNumericCellValue();
        } else {/*from   www  .  j  a  va2 s  .c om*/
            final String cellContent = c.getStringCellValue();
            try {
                return Integer.parseInt(cellContent);
            } catch (final Exception e) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Failed to parse booking number from " + cellContent);
                }
            }
        }
    }
    return NA_INT;
}

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

License:Open Source License

/** {@inheritDoc} */
@Override//from www  . j  a v  a 2 s .  c om
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//w  w  w  .  ja v  a 2 s .  com
public/*@Nullable*/Double getDouble(final int r, final int c) {
    final Cell cell = this.getPOICell(r, c);
    if (cell == null || !this.isDouble(cell))
        return null;

    return cell.getNumericCellValue();
}

From source file:com.github.ko2ic.plugin.eclipse.taggen.core.domain.model.spreadsheet.SheetImpl.java

License:Open Source License

/**
 * Get the value of the cell as a string .
 * @param cell a cell in spreadsheet/*  w w w . j av a2  s.com*/
 * @return
 */
private String getStringCellValue(Cell cell) {
    int type = cell.getCellType();
    String result = null;
    if (type == Cell.CELL_TYPE_STRING) {
        result = cell.getStringCellValue();
    } else if (type == Cell.CELL_TYPE_NUMERIC) {
        result = String.valueOf((int) cell.getNumericCellValue());
    }
    return result;
}

From source file:com.github.xiilei.ecdiff.Processor.java

License:Apache License

public String getStringCellValue(Cell cell) {
    String value = "";
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        value = "";
        break;//from w ww.j a  v  a 2 s  . c o m
    case Cell.CELL_TYPE_BOOLEAN:
        value = String.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_STRING:
        value = cell.getStringCellValue();
        cell.getRichStringCellValue();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        value = String.valueOf(cell.getNumericCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        value = "";
        break;
    }
    return value.trim().replace(" ", "");
}