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

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

Introduction

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

Prototype

boolean getBooleanCellValue();

Source Link

Document

Get the value of the cell as a boolean.

Usage

From source file:org.eclipse.emfforms.internal.spreadsheet.core.converter.EMFFormsSpreadsheetSingleAttributeConverter.java

License:Open Source License

private Object readCellValue(Cell cell, final EDataType attributeType) {
    if (isByte(attributeType.getInstanceClass())) {
        return Double.valueOf(cell.getNumericCellValue()).byteValue();
    } else if (isShort(attributeType.getInstanceClass())) {
        return Double.valueOf(cell.getNumericCellValue()).shortValue();
    } else if (isInteger(attributeType.getInstanceClass())) {
        return Double.valueOf(cell.getNumericCellValue()).intValue();
    } else if (isLong(attributeType.getInstanceClass())) {
        return Double.valueOf(cell.getNumericCellValue()).longValue();
    } else if (isFloat(attributeType.getInstanceClass())) {
        return Double.valueOf(cell.getNumericCellValue()).floatValue();
    } else if (isDouble(attributeType.getInstanceClass())) {
        return cell.getNumericCellValue();
    } else if (isBigInteger(attributeType.getInstanceClass())) {
        if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            return BigInteger.valueOf((long) cell.getNumericCellValue());
        }//from w  w  w  . j a v a 2s.co m
        return new BigInteger(cell.getStringCellValue());
    } else if (isBigDecimal(attributeType.getInstanceClass())) {
        if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            return BigDecimal.valueOf(cell.getNumericCellValue()).stripTrailingZeros();
        }
        final String value = cell.getStringCellValue();
        return new BigDecimal(value).stripTrailingZeros();
    } else if (isBoolean(attributeType.getInstanceClass())) {
        return cell.getBooleanCellValue();
    } else if (isDate(attributeType.getInstanceClass())) {
        return DateUtil.getJavaDate(cell.getNumericCellValue());
    } else if (isXMLDate(attributeType.getInstanceClass())) {
        final Calendar targetCal = DateUtil.getJavaCalendarUTC(cell.getNumericCellValue(), false);
        if (targetCal == null) {
            return null;
        }
        final XMLGregorianCalendar cal = new XMLCalendar(targetCal.getTime(), XMLCalendar.DATE);
        cal.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
        cal.setHour(DatatypeConstants.FIELD_UNDEFINED);
        cal.setMinute(DatatypeConstants.FIELD_UNDEFINED);
        cal.setSecond(DatatypeConstants.FIELD_UNDEFINED);
        cal.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
        return cal;
    } else {
        return EcoreUtil.createFromString(attributeType, cell.getStringCellValue());
    }
}

From source file:org.eclipse.emfforms.internal.spreadsheet.core.EMFFormsSpreadsheetExporterImpl_ITest.java

License:Open Source License

@Test
public void testRender() throws DatatypeConfigurationException {
    final EMFFormsSpreadsheetExporter viewRenderer = new EMFFormsSpreadsheetExporterImpl(new ViewProvider() {
        @Override/*from  w  w w  .  j  ava 2s.c  om*/
        public VView getViewModel(EObject viewEobject, VViewModelProperties properties) {
            return getView();
        }
    });
    final User user = getDomainModel();
    final Workbook wb = viewRenderer.render(Collections.singleton(user), null, null);
    final Sheet sheet = wb.getSheetAt(0);
    final Row row = sheet.getRow(3);
    assertEquals(11, row.getLastCellNum());
    for (int i = 0; i < 10; i++) {
        final Cell cell = row.getCell(i + 1);
        switch (i) {
        case 0:
            assertEquals(user.getFirstName(), cell.getStringCellValue());
            break;
        case 1:
            assertEquals(user.getLastName(), cell.getStringCellValue());
            break;
        case 2:
            assertEquals(user.getGender().toString(), cell.getStringCellValue());
            break;
        case 3:
            assertEquals(user.isActive(), cell.getBooleanCellValue());
            break;
        case 4:
            assertEquals(user.getTimeOfRegistration(), cell.getDateCellValue());
            break;
        case 5:
            assertEquals(user.getWeight(), cell.getNumericCellValue(), 0);
            break;
        case 6:
            assertEquals(user.getHeigth(), Double.valueOf(cell.getNumericCellValue()).intValue());
            break;
        case 7:
            assertEquals(user.getNationality().toString(), cell.getStringCellValue());
            break;
        case 8:
            assertEquals(user.getDateOfBirth().toGregorianCalendar().getTime(),
                    DateUtil.getJavaCalendarUTC(cell.getNumericCellValue(), false).getTime());
            break;
        case 9:
            assertEquals(user.getEmail(), cell.getStringCellValue());
            break;
        default:
            fail();
        }
    }
}

From source file:org.eclipse.rcptt.ecl.data.apache.poi.impl.internal.commands.ExcelFileService.java

License:Open Source License

public static String getCellValue(Cell cell) {
    if (cell == null) {
        return "";
    }//w w  w. ja  v  a 2 s . c  o m
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case Cell.CELL_TYPE_NUMERIC:
        String number = String.valueOf(cell.getNumericCellValue());
        number = number.replaceAll(".0$", "");
        return number;
    case Cell.CELL_TYPE_BOOLEAN:
        return String.valueOf(cell.getBooleanCellValue());
    case Cell.CELL_TYPE_FORMULA:
        return "=" + cell.getCellFormula();
    default:
        return "";
    }
}

From source file:org.efaps.esjp.common.file.FileUtil_Base.java

License:Apache License

/**
 * Copy cell.//w w w  .j av  a2 s.com
 *
 * @param _oldCell the old cell
 * @param _newCell the new cell
 * @param _styleMap the style map
 */
protected void copyCell(final Cell _oldCell, final Cell _newCell, final Map<Integer, CellStyle> _styleMap) {
    if (_styleMap != null) {
        if (_oldCell.getSheet().getWorkbook() == _newCell.getSheet().getWorkbook()) {
            _newCell.setCellStyle(_oldCell.getCellStyle());
        } else {
            final int stHashCode = _oldCell.getCellStyle().hashCode();
            CellStyle newCellStyle = _styleMap.get(stHashCode);
            if (newCellStyle == null) {
                newCellStyle = _newCell.getSheet().getWorkbook().createCellStyle();
                newCellStyle.cloneStyleFrom(_oldCell.getCellStyle());
                _styleMap.put(stHashCode, newCellStyle);
            }
            _newCell.setCellStyle(newCellStyle);
        }
    }
    switch (_oldCell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        _newCell.setCellValue(_oldCell.getStringCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        _newCell.setCellValue(_oldCell.getNumericCellValue());
        break;
    case Cell.CELL_TYPE_BLANK:
        _newCell.setCellType(Cell.CELL_TYPE_BLANK);
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        _newCell.setCellValue(_oldCell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        _newCell.setCellErrorValue(_oldCell.getErrorCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        _newCell.setCellFormula(_oldCell.getCellFormula());
        break;
    default:
        break;
    }

}

From source file:org.formulacompiler.spreadsheet.internal.excel.xls.loader.ExcelXLSLoader.java

License:Open Source License

private void loadCell(Cell _xlsCell, RowBuilder _rowBuilder) {
    final int xlsType = _xlsCell.getCellType();
    if (xlsType == Cell.CELL_TYPE_FORMULA) {
        final String expression;
        expression = _xlsCell.getCellFormula();
        _rowBuilder.addCellWithExpression(new LazySpreadsheetExpressionParser(expression, CellRefFormat.A1));

        if (this.config.loadAllCellValues) {
            final int cachedFormulaResultType = _xlsCell.getCachedFormulaResultType();
            if (Cell.CELL_TYPE_NUMERIC == cachedFormulaResultType) {
                _rowBuilder.setValue(getNumberValue(_xlsCell));
            } else if (Cell.CELL_TYPE_BOOLEAN == cachedFormulaResultType) {
                _rowBuilder.setValue(_xlsCell.getBooleanCellValue());
            } else if (Cell.CELL_TYPE_STRING == cachedFormulaResultType) {
                _rowBuilder.setValue(_xlsCell.getStringCellValue());
            }/*www  .j av  a2s .co m*/
        }
    } else if (Cell.CELL_TYPE_BLANK == xlsType) {
        _rowBuilder.addEmptyCell();
    } else if (Cell.CELL_TYPE_BOOLEAN == xlsType) {
        _rowBuilder.addCellWithConstant(_xlsCell.getBooleanCellValue());
    }

    else if (Cell.CELL_TYPE_NUMERIC == xlsType) {
        _rowBuilder.addCellWithConstant(getNumberValue(_xlsCell));

    } else if (Cell.CELL_TYPE_STRING == xlsType) {
        _rowBuilder.addCellWithConstant(_xlsCell.getStringCellValue());
    } else if (xlsType == Cell.CELL_TYPE_ERROR) {
        final int errorCode = _xlsCell.getErrorCellValue();
        switch (errorCode) {
        case 7:
            _rowBuilder.addCellWithError(CellWithError.DIV0);
            break;
        case 15:
            _rowBuilder.addCellWithError(CellWithError.VALUE);
            break;
        case 23:
            _rowBuilder.addCellWithError(CellWithError.REF);
            break;
        case 36:
            _rowBuilder.addCellWithError(CellWithError.NUM);
            break;
        case 42:
            _rowBuilder.addCellWithError(CellWithError.NA);
            break;
        default:
            _rowBuilder.addCellWithError("#ERR:" + errorCode);
        }
    }
}

From source file:org.generationcp.middleware.util.PoiUtil.java

License:Open Source License

public static Object getCellValue(final Cell cell) {
    if (cell == null) {
        return null;
    }//from  www.j  av a2s  .  c o  m
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue();
    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case Cell.CELL_TYPE_NUMERIC:
        return PoiUtil.getNumericValue(cell);
    case Cell.CELL_TYPE_FORMULA:
        return PoiUtil.getFormulaValue(cell);
    default:
        return null;
    }
}

From source file:org.generationcp.middleware.util.PoiUtil.java

License:Open Source License

private static void cloneCell(final Cell cNew, final Cell cOld) {
    cNew.setCellComment(cOld.getCellComment());
    cNew.setCellStyle(cOld.getCellStyle());

    switch (cNew.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN: {
        cNew.setCellValue(cOld.getBooleanCellValue());
        break;//from   w w w.  j  av  a2  s .  co  m
    }
    case Cell.CELL_TYPE_NUMERIC: {
        cNew.setCellValue(cOld.getNumericCellValue());
        break;
    }
    case Cell.CELL_TYPE_STRING: {
        cNew.setCellValue(cOld.getStringCellValue());
        break;
    }
    case Cell.CELL_TYPE_ERROR: {
        cNew.setCellValue(cOld.getErrorCellValue());
        break;
    }
    case Cell.CELL_TYPE_FORMULA: {
        cNew.setCellFormula(cOld.getCellFormula());
        break;
    }
    }

}

From source file:org.geoserver.wfs.response.ExcelOutputFormatTest.java

License:Open Source License

private void testExcelOutputFormat(Workbook wb) throws IOException {
    Sheet sheet = wb.getSheet("PrimitiveGeoFeature");
    assertNotNull(sheet);// w w  w  .j ava  2s .  c o m

    FeatureSource fs = getFeatureSource(MockData.PRIMITIVEGEOFEATURE);

    // check the number of rows in the output
    final int feautureRows = fs.getCount(Query.ALL);
    assertEquals(feautureRows + 1, sheet.getPhysicalNumberOfRows());

    // check the header is what we expect
    final SimpleFeatureType schema = (SimpleFeatureType) fs.getSchema();
    final Row header = sheet.getRow(0);
    assertEquals("FID", header.getCell(0).getRichStringCellValue().toString());
    for (int i = 0; i < schema.getAttributeCount(); i++) {
        assertEquals(schema.getDescriptor(i).getLocalName(),
                header.getCell(i + 1).getRichStringCellValue().toString());
    }

    // check some selected values to see if the content and data type is the one
    // we expect
    FeatureIterator fi = fs.getFeatures().features();
    SimpleFeature sf = (SimpleFeature) fi.next();
    fi.close();

    // ... a string cell
    Cell cell = sheet.getRow(1).getCell(1);
    assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
    assertEquals(sf.getAttribute(0), cell.getRichStringCellValue().toString());
    // ... a geom cell
    cell = sheet.getRow(1).getCell(4);
    assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
    assertEquals(sf.getAttribute(3).toString(), cell.getRichStringCellValue().toString());
    // ... a number cell
    cell = sheet.getRow(1).getCell(6);
    assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
    assertEquals(((Number) sf.getAttribute(5)).doubleValue(), cell.getNumericCellValue());
    // ... a date cell (they are mapped as numeric in xms?)
    cell = sheet.getRow(1).getCell(10);
    assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
    assertEquals(sf.getAttribute(9), cell.getDateCellValue());
    // ... a boolean cell (they are mapped as numeric in xms?)
    cell = sheet.getRow(1).getCell(12);
    assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType());
    assertEquals(sf.getAttribute(11), cell.getBooleanCellValue());
    // ... an empty cell (original value is null -> no cell)
    cell = sheet.getRow(1).getCell(3);
    assertNull(cell);
}

From source file:org.grible.excel.ExcelFile.java

License:Open Source License

private String getStringCellValue(Cell cell) {
    if (cell == null) {
        return "";
    }/*from   www .  j  a  v a 2  s.  c o  m*/
    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        String result = String.valueOf(cell.getNumericCellValue());
        if (result.endsWith(".0")) {
            result = StringUtils.substringBeforeLast(result, ".0");
        }
        return result;
    }
    if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        return String.valueOf(cell.getBooleanCellValue());
    }
    if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        return cell.getCellFormula();
    }
    return cell.getStringCellValue();
}

From source file:org.hellojavaer.poi.excel.utils.ExcelUtils.java

License:Apache License

private static Object _readCell(Cell cell) {
    if (cell == null) {
        return null;
    }// w  ww  . j av  a  2s  .com
    int cellType = cell.getCellType();
    Object value = null;
    switch (cellType) {
    case Cell.CELL_TYPE_BLANK:
        value = null;
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        boolean bool = cell.getBooleanCellValue();
        value = bool;
        break;
    case Cell.CELL_TYPE_ERROR:
        // cell.getErrorCellValue();
        ExcelReadException e = new ExcelReadException("Cell type error");
        e.setRowIndex(cell.getRowIndex());
        e.setColIndex(cell.getColumnIndex());
        e.setCode(ExcelReadException.CODE_OF_CELL_ERROR);
        throw e;
    case Cell.CELL_TYPE_FORMULA:
        value = cell.getCellFormula();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        Object inputValue = null;//
        double doubleVal = cell.getNumericCellValue();
        if (DateUtil.isCellDateFormatted(cell)) {
            inputValue = DateUtil.getJavaDate(doubleVal);
        } else {
            long longVal = Math.round(cell.getNumericCellValue());
            if (Double.parseDouble(longVal + ".0") == doubleVal) {
                inputValue = longVal;
            } else {
                inputValue = doubleVal;
            }
        }
        value = inputValue;
        break;
    case Cell.CELL_TYPE_STRING:
        value = cell.getStringCellValue();
        break;
    default:
        throw new RuntimeException("unsupport cell type " + cellType);
    }
    return value;
}