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.is.jxlpoi.JXLPOIWorkbook.java

License:Apache License

public String getCellContentAsString(Cell c) {

    if (c == null)
        return null;

    switch (c.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN:
        if (c.getBooleanCellValue())
            return "Y";
        else/*from  w  w  w.  ja  v  a  2s .com*/
            return "N";
    case Cell.CELL_TYPE_NUMERIC:
        String result = "";
        int datatype = c.getCellStyle().getDataFormat();

        String formatString = c.getCellStyle().getDataFormatString();
        if (datatype == 174 && "yyyy/mm/dd".equals(formatString)) {
            java.util.Date date = c.getDateCellValue();
            return fmter.format(date);
        } else if (datatype == 49 || datatype == 0) {
            int d = (int) c.getNumericCellValue();
            result = Integer.toString(d);
        } else {
            result = Double.toString(c.getNumericCellValue());
        }

        //return Double.toString(c.getNumericCellValue());
        //System.out.println(" number = "+c.getNumericCellValue()+" *** value ="+twoPlaces.format(c.getNumericCellValue())+"");

        //return twoPlaces.format(c.getNumericCellValue())+"";
        return result;
    case Cell.CELL_TYPE_STRING:
        return c.getStringCellValue();
    case Cell.CELL_TYPE_BLANK:
        return "";
    case Cell.CELL_TYPE_ERROR:
        return "#ERROR" + c.getErrorCellValue();
    case Cell.CELL_TYPE_FORMULA:

        String formulaCellValue;

        if (formulaEvaluator == null) {
            formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
            //formulaEvaluator.setIgnoreFormulaException();
            //System.out.println(formulaEvaluator);
        }

        //formulaEvaluator.evaluateFormulaCell(c);
        //formulaEvaluator.evaluateInCell(c);

        CellValue cv = formulaEvaluator.evaluate(c);

        switch (cv.getCellType()) {
        //switch (formulaEvaluator.evaluateInCell(c).getCellType()) {
        case Cell.CELL_TYPE_BOOLEAN:
            if (cv.getBooleanValue())
                formulaCellValue = "Y";
            else
                formulaCellValue = "F";
            break;
        case Cell.CELL_TYPE_NUMERIC:
            formulaCellValue = Double.toString(cv.getNumberValue());
            break;
        case Cell.CELL_TYPE_STRING:
            formulaCellValue = cv.getStringValue();
            break;
        case Cell.CELL_TYPE_BLANK:
            formulaCellValue = "";
            break;
        case Cell.CELL_TYPE_ERROR:
            formulaCellValue = Byte.toString(cv.getErrorValue());
            break;
        default:
            formulaCellValue = "";
            break;
        }//switch

        return formulaCellValue;
    default:
        return "";
    }//switch

}

From source file:org.isisaddons.module.excel.dom.CellMarshaller.java

License:Apache License

@SuppressWarnings("unchecked")
private <T> T getCellValue(final Cell cell, final Class<T> requiredType) {
    final int cellType = cell.getCellType();

    if (requiredType == boolean.class || requiredType == Boolean.class) {
        if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
            boolean booleanCellValue = cell.getBooleanCellValue();
            return (T) Boolean.valueOf(booleanCellValue);
        } else {//from w ww.j a va2 s  .c  o m
            return null;
        }
    }

    // enum
    if (Enum.class.isAssignableFrom(requiredType)) {
        String stringCellValue = cell.getStringCellValue();
        @SuppressWarnings("rawtypes")
        Class rawType = requiredType;
        return (T) Enum.valueOf(rawType, stringCellValue);
    }

    // date
    if (requiredType == java.util.Date.class) {
        java.util.Date dateCellValue = cell.getDateCellValue();
        return (T) dateCellValue;
    }

    if (requiredType == org.apache.isis.applib.value.Date.class) {
        java.util.Date dateCellValue = cell.getDateCellValue();
        return (T) new org.apache.isis.applib.value.Date(dateCellValue);
    }

    if (requiredType == org.apache.isis.applib.value.DateTime.class) {
        java.util.Date dateCellValue = cell.getDateCellValue();
        return (T) new org.apache.isis.applib.value.DateTime(dateCellValue);
    }

    if (requiredType == LocalDate.class) {
        java.util.Date dateCellValue = cell.getDateCellValue();
        return (T) new LocalDate(dateCellValue.getTime());
    }

    if (requiredType == LocalDateTime.class) {
        java.util.Date dateCellValue = cell.getDateCellValue();
        return (T) new LocalDateTime(dateCellValue.getTime());
    }

    if (requiredType == DateTime.class) {
        java.util.Date dateCellValue = cell.getDateCellValue();
        return (T) new DateTime(dateCellValue.getTime());
    }

    // number
    if (requiredType == double.class || requiredType == Double.class) {
        if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
            double doubleValue = cell.getNumericCellValue();
            return (T) Double.valueOf(doubleValue);
        } else {
            return null;
        }
    }

    if (requiredType == float.class || requiredType == Float.class) {
        if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
            float floatValue = (float) cell.getNumericCellValue();
            return (T) Float.valueOf(floatValue);
        } else {
            return null;
        }
    }

    if (requiredType == BigDecimal.class) {
        if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
            double doubleValue = cell.getNumericCellValue();
            return (T) BigDecimal.valueOf(doubleValue);
        } else {
            return null;
        }
    }

    if (requiredType == BigInteger.class) {
        if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
            long longValue = (long) cell.getNumericCellValue();
            return (T) BigInteger.valueOf(longValue);
        } else {
            return null;
        }
    }

    if (requiredType == long.class || requiredType == Long.class) {
        if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
            long longValue = (long) cell.getNumericCellValue();
            return (T) Long.valueOf(longValue);
        } else {
            return null;
        }
    }

    if (requiredType == int.class || requiredType == Integer.class) {
        if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
            int intValue = (int) cell.getNumericCellValue();
            return (T) Integer.valueOf(intValue);
        } else {
            return null;
        }
    }

    if (requiredType == short.class || requiredType == Short.class) {
        if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
            short shortValue = (short) cell.getNumericCellValue();
            return (T) Short.valueOf(shortValue);
        } else {
            return null;
        }
    }

    if (requiredType == byte.class || requiredType == Byte.class) {
        if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
            byte byteValue = (byte) cell.getNumericCellValue();
            return (T) Byte.valueOf(byteValue);
        } else {
            return null;
        }
    }

    if (requiredType == String.class) {
        if (cellType == HSSFCell.CELL_TYPE_STRING) {
            return (T) cell.getStringCellValue();
        } else {
            return null;
        }
    }
    return null;
}

From source file:org.isource.util.CSVUtils.java

private static List<List> readWorkbook(HSSFWorkbook workbook) {

    List<List> lines = new ArrayList<List>();

    workbook = evaluateFormulas(workbook);

    HSSFSheet sheet = workbook.getSheetAt(0);

    Iterator<Row> rowIterator = sheet.iterator();

    while (rowIterator.hasNext()) {

        Row row = rowIterator.next();//from www. ja v a 2s .c  o m

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

        //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:
                line.add(new Boolean(cell.getBooleanCellValue()).toString());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
                    line.add(dateFormat.format(cell.getDateCellValue()));
                } else {
                    line.add(new Double(cell.getNumericCellValue()).toString());
                }
                break;
            case Cell.CELL_TYPE_STRING:
                line.add(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                switch (cell.getCachedFormulaResultType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    line.add(new Double(cell.getNumericCellValue()).toString());
                    break;
                case Cell.CELL_TYPE_STRING:
                    line.add(cell.getRichStringCellValue().toString());
                    break;
                }
                break;
            }
        }

        lines.add(line);
    }

    return lines;
}

From source file:org.jberet.support.io.ExcelUserModelItemReader.java

License:Open Source License

protected Object getCellValue(final Cell c, final int cellType) {
    final Object cellValue;
    switch (cellType) {
    case Cell.CELL_TYPE_STRING:
        cellValue = c.getStringCellValue();
        break;//from w  w w  .  j a  va2s  .c om
    case Cell.CELL_TYPE_BOOLEAN:
        cellValue = c.getBooleanCellValue();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        cellValue = DateUtil.isCellDateFormatted(c) ? c.getDateCellValue() : c.getNumericCellValue();
        break;
    case Cell.CELL_TYPE_BLANK:
        cellValue = null;
        break;
    case Cell.CELL_TYPE_FORMULA:
        if (formulaEvaluator == null) {
            formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
        }
        formulaEvaluator.evaluateFormulaCell(c);
        cellValue = getCellValue(c, c.getCachedFormulaResultType());
        break;
    default:
        cellValue = c.getStringCellValue();
        break;
    }
    return cellValue;
}

From source file:org.joeffice.spreadsheet.cell.CellUtils.java

License:Apache License

public static void copyCell(Cell oldCell, Cell newCell) {
    newCell.setCellStyle(oldCell.getCellStyle());

    if (newCell.getCellComment() != null) {
        newCell.setCellComment(oldCell.getCellComment());
    }//w  w w  . j a  v  a 2 s  . c om

    if (oldCell.getHyperlink() != null) {
        newCell.setHyperlink(oldCell.getHyperlink());
    }

    newCell.setCellType(oldCell.getCellType());

    // Set the cell data value
    switch (oldCell.getCellType()) {
    case 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;
    case Cell.CELL_TYPE_NUMERIC:
        newCell.setCellValue(oldCell.getNumericCellValue());
        break;
    case Cell.CELL_TYPE_STRING:
        newCell.setCellValue(oldCell.getRichStringCellValue());
        break;
    }
}

From source file:org.joeffice.spreadsheet.cell.CellUtils.java

License:Apache License

public static String getFormattedText(Cell cell) {
    if (cell == null) {
        return "";
    }//from w w  w  . j a v  a  2  s. co m
    int type = cell.getCellType();
    if (type == Cell.CELL_TYPE_STRING) {
        return cell.getStringCellValue();
    } else if (type == Cell.CELL_TYPE_NUMERIC) {
        if (DateUtil.isCellDateFormatted(cell)) {
            return DATE_FORMATTER.format(cell.getDateCellValue());
        } else {
            return NUMBER_FORMATTER.format(cell.getNumericCellValue());
        }
    } else if (type == Cell.CELL_TYPE_BOOLEAN) {
        return String.valueOf(cell.getBooleanCellValue());
    } else {
        return "";
    }
}

From source file:org.kopsox.spreadsheet.util.POIUtil.java

License:Open Source License

/**
 * Gets a Value from the given Cell//  w  w w.  j  a  v  a  2s .c om
 * 
 * @param cell
 * @return Value
 */
@SuppressWarnings("boxing")
public static final Value getValueFormCell(Cell cell) {

    //NUMERIC
    if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
        //Numeric can be a Date or a Double

        return getValueFromNumeric(cell, null);

    } //STRING
    else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {

        return new StringValue(cell.getRichStringCellValue().getString());
    } //FORMEL
    else if (Cell.CELL_TYPE_FORMULA == cell.getCellType()) {
        String formula = cell.getCellFormula();

        return getValueFromFormula(formula, cell);
    } //BOOLEAN
    else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) {

        return new BooleanValue(cell.getBooleanCellValue());
    } //BLANK
    else if (Cell.CELL_TYPE_BLANK == cell.getCellType()) {

        return new BlankValue();
    } //ERROR
    else if (Cell.CELL_TYPE_ERROR == cell.getCellType()) {
        //TODO
        //NOT SUPPORTED AT THE MOMENT
    }

    return new BlankValue();
}

From source file:org.kopsox.spreadsheet.util.POIUtil.java

License:Open Source License

@SuppressWarnings("boxing")
private static final Value getValueFromFormula(String formula, Cell cell) {

    Value ret = null;/*w w w  .j a va2  s.  c  om*/

    if (Cell.CELL_TYPE_NUMERIC == cell.getCachedFormulaResultType()) {
        ret = getValueFromNumeric(cell, formula);

    } else if (Cell.CELL_TYPE_STRING == cell.getCachedFormulaResultType()) {
        StringValue tmp = new StringValue(cell.getRichStringCellValue().getString());
        tmp.setFormula(formula);
        ret = tmp;

    } else if (Cell.CELL_TYPE_BOOLEAN == cell.getCachedFormulaResultType()) {
        BooleanValue tmp = new BooleanValue(cell.getBooleanCellValue());
        tmp.setFormula(formula);
        ret = tmp;

    }

    return ret;
}

From source file:org.laukvik.excel.ExcelTableModel.java

License:Apache License

public String getString(Cell cell) {
    if (cell == null) {
        return null;
    }// w  w  w. j  a  va  2 s .c  om
    try {
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
            Date date = cell.getDateCellValue();
            return dateFormat.format(date);
        }
    } catch (Exception e) {
    }
    try {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            return null;
        case Cell.CELL_TYPE_BOOLEAN:
            return cell.getBooleanCellValue() + "";
        case Cell.CELL_TYPE_ERROR:
            return null;
        case Cell.CELL_TYPE_FORMULA:
            return cell.getCellFormula();
        case Cell.CELL_TYPE_NUMERIC:
            Double d = cell.getNumericCellValue();
            return d.intValue() + "";
        case Cell.CELL_TYPE_STRING:
            return cell.getStringCellValue();

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.lisapark.octopus.util.json.ExcelSardineUtils.java

License:Open Source License

/**
 * /*from  ww  w.  j a va2s  .c  o m*/
 * @param row
 * @param cellIndex
 * @return 
 */
public static Object cellValue(Row row, int cellIndex) {
    Cell cell = row.getCell(cellIndex);

    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        return cell.getStringCellValue();
    } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
        return null;
    } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        return cell.getNumericCellValue();
    } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        return cell.getBooleanCellValue();
    } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
        return cell.getErrorCellValue();
    } else {
        return null;
    }
}