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:com.exilant.exility.core.XLSReader.java

License:Open Source License

/**
 * purpose of this method to create ValueList along with types and column
 * name. Simple design followed : Just have ColumnMetaData object which
 * contains everything. For a Cell we will have one columnMetaData object
 * and it will have values across the// ww  w. j  a  va  2s  . c  om
 * 
 * @param row
 * @throws Exception
 */
private void readARow(Row row, int nbrColumnsInARow) throws ExilityException {
    Value[] columnValues = new Value[nbrColumnsInARow];
    Value aColumnValue = null;
    String rawValue = null;

    for (int c = 0; c < nbrColumnsInARow; c++) {
        Cell cell = row.getCell(c, Row.CREATE_NULL_AS_BLANK);

        ColumnMetaData columnInfo = this.columnsData.get(new Integer(c));
        int xlsColumnDataType = columnInfo.getXlsDataType();
        DataValueType exilDataType = null;

        int cellType = cell.getCellType();
        if (xlsColumnDataType != XLSReader.UNKNOWN_TYPE) {
            cellType = xlsColumnDataType;
        }

        try {

            switch (cellType) {
            case Cell.CELL_TYPE_NUMERIC:

                if (DateUtil.isCellDateFormatted(cell)) {
                    rawValue = DateUtility.formatDate(cell.getDateCellValue());
                    /*
                     * returns yyyy-mm-dd hh:mm:ss.sss full date with time.
                     */

                    exilDataType = DataValueType.DATE;
                } else {
                    double decimalNumber = cell.getNumericCellValue();
                    rawValue = NumberToTextConverter.toText(decimalNumber);

                    boolean isDecimal = rawValue.contains(".");
                    if (isDecimal) {
                        exilDataType = DataValueType.DECIMAL;
                    } else {
                        exilDataType = DataValueType.INTEGRAL;
                    }
                }

                break;

            case Cell.CELL_TYPE_STRING:

                rawValue = cell.getStringCellValue().trim();
                exilDataType = DataValueType.TEXT;
                break;

            case Cell.CELL_TYPE_FORMULA:

                rawValue = cell.getStringCellValue().trim();
                exilDataType = DataValueType.TEXT;
                break;

            case Cell.CELL_TYPE_BLANK:

                rawValue = cell.getStringCellValue();
                exilDataType = DataValueType.NULL;
                columnInfo.setExilDataType(exilDataType);
                break;

            case Cell.CELL_TYPE_BOOLEAN:

                rawValue = cell.getBooleanCellValue() ? BooleanValue.TRUE : BooleanValue.FALSE;
                exilDataType = DataValueType.BOOLEAN;
                break;
            default:
                String msg = columnInfo.getColumnName() + XLSReader.INVALID_COLUMN_TYPE + row.getRowNum();
                Spit.out(msg);

            }

        } catch (Exception e) {
            // Trying to set valueType value and expected valueType value
            // for column in row
            String[] params = { this.getXlsTypeAsText(cell.getCellType()), this.getXlsTypeAsText(cellType),
                    columnInfo.getColumnName(), "" + row.getRowNum() };

            String message = this.replaceMessageParams(XLSReader.DATATYPE_MISMATCH, params);
            throw new ExilityException(message);
        }

        if (xlsColumnDataType == XLSReader.UNKNOWN_TYPE && cellType != Cell.CELL_TYPE_BLANK) {
            columnInfo.setXlsDataType(cellType);
            columnInfo.setExilDataType(exilDataType);
        }

        exilDataType = columnInfo.getExilDataType();

        aColumnValue = Value.newValue(rawValue, exilDataType);

        columnValues[c] = aColumnValue;
        this.columnsData.put(new Integer(c), columnInfo);
    }

    this.rows.add(columnValues);
}

From source file:com.exilant.exility.core.XlxUtil.java

License:Open Source License

/***
 * get text value of cell irrespective of its content type
 * /*from  ww  w.jav  a2s  .  c o  m*/
 * @param cell
 * @return
 */
private String getTextValue(Cell cell) {
    if (cell == null) {
        return EMPTY_STRING;
    }

    int cellType = cell.getCellType();
    if (cellType == Cell.CELL_TYPE_BLANK) {
        return EMPTY_STRING;
    }

    if (cellType == Cell.CELL_TYPE_FORMULA) {
        cellType = cell.getCachedFormulaResultType();
    }

    if (cellType == Cell.CELL_TYPE_STRING) {
        return cell.getStringCellValue().trim();
    }

    /*
     * dates are internally stored as decimal..
     */
    if (cellType == Cell.CELL_TYPE_NUMERIC) {
        if (DateUtil.isCellDateFormatted(cell)) {
            return DateUtility.formatDate(cell.getDateCellValue());
        }

        return NumberFormat.getInstance().format(cell.getNumericCellValue());
    }

    if (cellType == Cell.CELL_TYPE_BOOLEAN) {
        if (cell.getBooleanCellValue()) {
            return "1";
        }
        return "0";
    }
    return EMPTY_STRING;
}

From source file:com.fanniemae.ezpie.data.connectors.ExcelConnector.java

License:Open Source License

protected Object[] readExcelData(Row excelDataRow) {
    Object[] data = new Object[_columnCount];
    String cellAddress = "";
    int dataIndex = 0;
    try {/*from   www . j  a  v  a  2  s .  c om*/
        for (Cell cell : excelDataRow) {
            cellAddress = cell.getAddress().toString();
            String columnLetter = CellReference.convertNumToColString(cell.getColumnIndex());
            int columnIndex = _columnAddress.indexOf(columnLetter);
            if (columnIndex == -1) {
                continue;
            }

            CellType ct = cell.getCellTypeEnum();
            if (ct == CellType.FORMULA)
                ct = cell.getCachedFormulaResultTypeEnum();
            switch (ct) {
            case STRING:
                data[dataIndex] = cell.getStringCellValue();
                break;
            case BOOLEAN:
                data[dataIndex] = _allTypesStrings ? Boolean.toString(cell.getBooleanCellValue())
                        : cell.getBooleanCellValue();
                break;
            case NUMERIC:
                data[dataIndex] = _allTypesStrings ? Double.toString(cell.getNumericCellValue())
                        : cell.getNumericCellValue();
                break;
            default:
                data[dataIndex] = _allTypesStrings ? "" : null;
                break;
            }
            dataIndex++;
        }
        if (_addFilename)
            data[data.length - 1] = _filenameOnly;
    } catch (Exception ex) {
        throw new PieException(
                String.format("Error while reading Excel data from cell %s. %s", cellAddress, ex.getMessage()),
                ex);
    }

    return data;
}

From source file:com.fjn.helper.common.io.file.office.excel.ListExcelSheetReader.java

License:Apache License

private <T> T getBean(Class<T> clazz, Row row, List<String> fieldList) {
    Field[] fields = new Field[fieldList.size()];
    for (int i = 0; i < fields.length; i++) {
        Field field = null;//from   w  w w  .  java 2s.  co  m
        try {
            field = clazz.getDeclaredField(fieldList.get(i));
            // field.getAnnotation();
            fields[i] = field;
            field.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    T t = null;
    try {
        t = clazz.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }

    for (int i = 0; i < fields.length; i++) {
        Cell cell = null;
        cell = row.getCell(i);
        if (cell != null) {
            int cellType = cell.getCellType();
            Object value = null;
            switch (cellType) {
            case Cell.CELL_TYPE_STRING:
                value = cell.getRichStringCellValue().getString();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    value = cell.getDateCellValue();
                } else {
                    value = cell.getNumericCellValue();
                    Class fieldClass = fields[i].getType();
                    if (fieldClass == Integer.class || fieldClass == int.class) {
                        value = ((Double) value).intValue();
                    } else if (fieldClass == Short.class || fieldClass == short.class) {
                        value = ((Double) value).shortValue();
                    } else if (fieldClass == Byte.class || fieldClass == byte.class) {
                        value = ((Double) value).byteValue();
                    } else if (fieldClass == Long.class || fieldClass == long.class) {
                        value = ((Double) value).longValue();
                    } else if (fieldClass == Float.class || fieldClass == float.class) {
                        value = ((Double) value).floatValue();
                    } else if (fieldClass == Double.class || fieldClass == double.class) {
                        value = (Double) value;
                    }
                }
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                value = cell.getBooleanCellValue();
                break;
            case Cell.CELL_TYPE_FORMULA:
                value = cell.getCellFormula();
                break;
            default:
                break;
            }

            try {
                fields[i].set(t, value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return t;
}

From source file:com.framework.common.ExcelSpreadsheet.java

public List<String> getSpreadSheetAsArray() {

    List<String> cellValue = new ArrayList<String>();

    // Iterate through each rows from first sheet
    Iterator<Row> rowIterator = requiredWorksheet.iterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();//from  ww w  .java 2 s . co  m

        // For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext()) {

            Cell cell = cellIterator.next();

            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                //                        System.out.print(cell.getBooleanCellValue() + "\t\t");
                if (cell.getBooleanCellValue()) {
                    cellValue.add("TRUE");
                } else {
                    cellValue.add("FALSE");
                }

                break;
            case Cell.CELL_TYPE_NUMERIC:
                //                        System.out.print(cell.getNumericCellValue() + "\t\t");

                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    Format formatter = new SimpleDateFormat("yyyy-MM-dd");
                    String s = formatter.format(cell.getDateCellValue());
                    cellValue.add(s);
                } else {
                    double value = cell.getNumericCellValue();
                    cellValue.add(Double.toString(value));
                }

                break;
            case Cell.CELL_TYPE_STRING:
                //                        System.out.print(cell.getStringCellValue() + "\t\t");
                cellValue.add(cell.getStringCellValue());
                break;
            }
        }
        //            System.out.println("");
    }
    return cellValue;
}

From source file:com.FuntionLibrary.java

public Boolean isCellContentPresent(XSSFSheet sheet, String content) {
    Boo = false;//from   w w w  .ja v  a 2  s .co  m
    try {
        String CellContent = null;
        // Get iterator to all the rows in current sheet
        Iterator<Row> rowIterator = sheet.iterator();
        // Traversing over each row of XLSX file
        while (rowIterator.hasNext()) {
            if (Boo == true)
                break;
            Row row = rowIterator.next();
            // For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                if (Boo == true)
                    break;
                Cell cell = cellIterator.next();

                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t");
                    CellContent = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t");
                    CellContent = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue() + "\t");
                    CellContent = cell.getStringCellValue();
                    break;
                default:
                    //CellContent = null;
                }
                if (CellContent.equalsIgnoreCase(content)) {
                    if (MainGui.EditExistingButtonClicked == true) {
                        break;

                    }
                    Boo = true;
                    break;
                    //else {
                    //                         throw new Exception("Data Already exists");
                    //                            }
                } else {
                    Boo = false;
                }
            }
            System.out.println("");
            if (MainGui.EditExistingButtonClicked == true && CellContent.equalsIgnoreCase(content)) {
                break;
            }
        }

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage());
    }
    return Boo;
}

From source file:com.funtl.framework.smoke.core.commons.excel.ImportExcel.java

License:Apache License

/**
 * ??//from w w w. j a v  a 2 s .c  o  m
 *
 * @param row    ?
 * @param column ???
 * @return ?
 */
@SuppressWarnings("deprecation")
public Object getCellValue(Row row, int column) {
    Object val = "";
    try {
        Cell cell = row.getCell(column);
        if (cell != null) {
            if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                val = cell.getNumericCellValue();
            } else if (cell.getCellTypeEnum() == CellType.STRING) {
                val = cell.getStringCellValue();
            } else if (cell.getCellTypeEnum() == CellType.FORMULA) {
                val = cell.getCellFormula();
            } else if (cell.getCellTypeEnum() == CellType.BOOLEAN) {
                val = cell.getBooleanCellValue();
            } else if (cell.getCellTypeEnum() == CellType.ERROR) {
                val = cell.getErrorCellValue();
            }
        }
    } catch (Exception e) {
        return val;
    }
    return val;
}

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

License:Apache License

private static Object getCellValue(final Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        }/*from w ww  .  j a  v a  2  s  .  c o  m*/
        return cell.getNumericCellValue();
    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue();
    case Cell.CELL_TYPE_ERROR:
        return cell.getErrorCellValue();
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    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.  j a v a  2  s .c  o  m*/
        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.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  w ww .j ava 2s .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");
    }
}