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.read.main.LeerPDF.java

/**
 * @param args the command line arguments
 *//*from www. j  a  va  2 s  .  c  o m*/
public static void main(String[] args) throws IOException {
    try {

        FileInputStream file = new FileInputStream(new File("/home/aaron/Escritorio/Example.xlsx"));
        XSSFWorkbook workbook2 = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook2.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();

            System.out.println("Numero de Columnas: " + row.getLastCellNum());

            System.out.println(row.getRowNum());

            if (row.getRowNum() == 0) {
                System.out.println("Fila Cero");
            } else {

                int numColumna = 0;

                while (numColumna < row.getLastCellNum()) {

                    Cell cell = row.getCell(numColumna);

                    try {
                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            System.out.print(numColumna + ".- BOOLEAN: ");
                            System.out.print(cell.getBooleanCellValue() + "\t\t");
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(numColumna + ".- NUMERIC: ");
                            System.out.print(cell.getNumericCellValue() + "\t\t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(numColumna + ".- STRING: ");
                            System.out.print(cell.getStringCellValue() + "\t\t");
                            break;
                        }
                    } catch (Exception e) {
                        System.err.println(e);
                    }
                    ;

                    numColumna++;
                }
            }

            System.out.println("");
        }
        file.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.runwaysdk.dataaccess.io.excel.ErrorSheet.java

License:Open Source License

public void addRow(Row _row) {
    Row row = this.errorSheet.createRow(count++);
    row.setZeroHeight(_row.getZeroHeight());
    row.setHeight(_row.getHeight());/*from  w  w  w .java2 s . c o  m*/

    CellStyle style = _row.getRowStyle();

    if (style != null) {
        Workbook workbook = row.getSheet().getWorkbook();

        CellStyle clone = workbook.createCellStyle();
        clone.cloneStyleFrom(style);

        row.setRowStyle(clone);
    }

    Iterator<Cell> cellIterator = _row.cellIterator();
    while (cellIterator.hasNext()) {
        Cell oldCell = cellIterator.next();
        Cell newCell = row.createCell(oldCell.getColumnIndex());

        int cellType = oldCell.getCellType();

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

        switch (cellType) {
        case Cell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case Cell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        }
    }
}

From source file:com.runwaysdk.dataaccess.io.excel.ExcelUtil.java

License:Open Source License

private static Boolean getBooleanInternal(Cell cell, String positiveLabel, String negativeLabel) {
    if (cell == null) {
        return Boolean.FALSE;
    }/*  ww  w  . j  a v a 2 s  .  c o  m*/

    int cellType = cell.getCellType();
    // In the case of formula, find out what type the formula will produce
    if (cellType == Cell.CELL_TYPE_FORMULA) {
        cellType = cell.getCachedFormulaResultType();
    }

    if (cellType == Cell.CELL_TYPE_STRING) {
        String value = cell.getRichStringCellValue().getString().trim();

        if (value.equalsIgnoreCase(positiveLabel) || value.equalsIgnoreCase("y")
                || value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("t")
                || value.equalsIgnoreCase("true") || value.equalsIgnoreCase("x")) {
            return Boolean.TRUE;
        }
        if (value.equalsIgnoreCase(negativeLabel) || value.equalsIgnoreCase("false")
                || value.equalsIgnoreCase("f") || value.equalsIgnoreCase("no") || value.equalsIgnoreCase("n")
                || value.length() == 0) {
            return Boolean.FALSE;
        }

        throw new AttributeValueException("[" + value + "] is not a recognized boolean in excel", value);
    } else if (cellType == Cell.CELL_TYPE_NUMERIC) {
        Double value = new Double(cell.getNumericCellValue());

        return value.equals(new Double(1));
    } else {
        return Boolean.valueOf(cell.getBooleanCellValue());
    }
}

From source file:com.runwaysdk.dataaccess.io.excel.ExcelUtil.java

License:Open Source License

public static String getString(Cell cell) {
    if (cell == null) {
        return null;
    }/*w ww .  ja v a2s.c  o m*/

    int cellType = cell.getCellType();

    // In the case of formula, find out what type the formula will produce
    if (cellType == Cell.CELL_TYPE_FORMULA) {
        cellType = cell.getCachedFormulaResultType();
    }

    if (cellType == Cell.CELL_TYPE_BLANK) {
        return "";
    } else if (cellType == Cell.CELL_TYPE_NUMERIC) {
        return (new BigDecimal(cell.getNumericCellValue())).toString();
    } else if (cellType == Cell.CELL_TYPE_BOOLEAN) {
        return new Boolean(cell.getBooleanCellValue()).toString();
    } else {
        return cell.getRichStringCellValue().getString().trim();
    }
}

From source file:com.siberhus.tdfl.excel.DefaultExcelRowReader.java

License:Apache License

private Object getCellValue(Cell cell) {
    if (cell == null)
        return null;
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        RichTextString rts = cell.getRichStringCellValue();
        if (rts != null) {
            return rts.getString();
        }//from  www .j  av  a 2  s . c  o  m
        return null;
    case Cell.CELL_TYPE_NUMERIC:
        String value = cell.toString();
        /*
         * In POI we cannot know which cell is date or number because both
         * cells have numeric type To fix this problem we need to call
         * toString if it's number cell we can parse it but if it's date
         * cell we cannot parse the value with number parser
         */
        try {
            return new BigDecimal(value);
        } catch (Exception e) {
            return cell.getDateCellValue();
        }
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue();
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    }
    return null;
}

From source file:com.ssy.havefun.f3d.F3DDaoImpl.java

public String getCellValue(Cell cell) {
    String ret = "";
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        ret = "";
        break;/*w  w w  .  j a v a2s.  c o  m*/
    case Cell.CELL_TYPE_BOOLEAN:
        ret = String.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        ret = null;
        break;
    case Cell.CELL_TYPE_FORMULA:
        Workbook wb = cell.getSheet().getWorkbook();
        CreationHelper crateHelper = wb.getCreationHelper();
        FormulaEvaluator evaluator = crateHelper.createFormulaEvaluator();
        ret = getCellValue(evaluator.evaluateInCell(cell));
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            //                Date theDate = cell.getDateCellValue();  
            //                ret = simpleDateFormat.format(theDate);  
        } else {
            ret = NumberToTextConverter.toText(cell.getNumericCellValue());
        }
        break;
    case Cell.CELL_TYPE_STRING:
        ret = cell.getRichStringCellValue().getString();
        break;
    default:
        ret = null;
    }

    return ret; //?trim  
}

From source file:com.streamsets.pipeline.lib.parser.excel.Cells.java

License:Apache License

static Field parseCell(Cell cell, FormulaEvaluator evaluator) throws ExcelUnsupportedCellTypeException {
    CellType cellType = cell.getCellTypeEnum();
    // set the cellType of a formula cell to its cached formula result type in order to process it as its result type
    boolean isFormula = cell.getCellTypeEnum().equals(CellType.FORMULA);
    if (isFormula) {
        cellType = cell.getCachedFormulaResultTypeEnum();
    }//from w  w w.  j  a v a  2  s .c om

    switch (cellType) {
    case STRING:
        return Field.create(cell.getStringCellValue());
    case NUMERIC:
        Double rawValue = cell.getNumericCellValue(); // resolves formulas automatically and gets value without cell formatting
        String displayValue = isFormula ? evaluator.evaluate(cell).formatAsString()
                : dataFormatter.formatCellValue(cell);
        boolean numericallyEquivalent = false;
        try {
            numericallyEquivalent = Double.parseDouble(displayValue) == rawValue;
        } catch (NumberFormatException e) {
        }

        if (DateUtil.isCellDateFormatted(cell)) {
            // It's a date, not a number
            java.util.Date dt = cell.getDateCellValue();
            // if raw number is < 1 then it's a time component only, otherwise date.
            return rawValue < 1 ? Field.createTime(dt) : Field.createDate(dt);
        }

        // some machinations to handle integer values going in without decimal vs. with .0 for rawValue
        return Field
                .create(numericallyEquivalent ? new BigDecimal(displayValue) : BigDecimal.valueOf(rawValue));

    case BOOLEAN:
        return Field.create(cell.getBooleanCellValue());
    case BLANK:
        return Field.create("");
    default:
        throw new ExcelUnsupportedCellTypeException(cell, cellType);
    }
}

From source file:com.tecacet.jflat.excel.PoiExcelReader.java

License:Apache License

private String getCellContentAsString(Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue().getString();
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue().toString();
        } else {/* w w  w  .  java2 s .  co  m*/
            double d = cell.getNumericCellValue();
            // TODO find a flexible enough format for all numeric types
            return numberFormat.format(d);
            // return Double.toString(d);
        }
    case Cell.CELL_TYPE_BOOLEAN:
        boolean b = cell.getBooleanCellValue();
        return Boolean.toString(b);
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    case Cell.CELL_TYPE_BLANK:
        return "";
    case Cell.CELL_TYPE_ERROR:
        byte bt = cell.getErrorCellValue();
        return Byte.toString(bt);
    default:
        return cell.getStringCellValue();

    }
}

From source file:com.vaadin.addon.spreadsheet.CellSelectionShifter.java

License:Open Source License

/**
 * "Shifts" cell value. Shifting here is an Excel term and means the
 * situation where the user has selected one or more cells, and grabs the
 * bottom right hand square of the selected area to extend or curtail the
 * selection and fill the new area with values determined from the existing
 * values./* w  w w . jav  a2s  .c om*/
 * 
 * @param shiftedCell
 *            Source cell
 * @param newCell
 *            Resulting new cell
 * @param removeShifted
 *            true to remove the source cell at the end
 * @param sequenceIncrement
 *            increment added to shifted cell value
 */
protected void shiftCellValue(Cell shiftedCell, Cell newCell, boolean removeShifted, Double sequenceIncrement) {
    // clear the new cell first because it might have errors which prevent
    // it from being set to a new type
    if (newCell.getCellType() != Cell.CELL_TYPE_BLANK || shiftedCell.getCellType() == Cell.CELL_TYPE_BLANK) {
        newCell.setCellType(Cell.CELL_TYPE_BLANK);
    }
    newCell.setCellType(shiftedCell.getCellType());
    newCell.setCellStyle(shiftedCell.getCellStyle());
    spreadsheet.getSpreadsheetStyleFactory().cellStyleUpdated(newCell, true);
    switch (shiftedCell.getCellType()) {
    case Cell.CELL_TYPE_FORMULA:
        shiftFormula(shiftedCell, newCell);
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        newCell.setCellValue(shiftedCell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        newCell.setCellValue(shiftedCell.getErrorCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        shiftNumeric(shiftedCell, newCell, sequenceIncrement);
        break;
    case Cell.CELL_TYPE_STRING:
        shiftString(shiftedCell, newCell, sequenceIncrement);
        break;
    case Cell.CELL_TYPE_BLANK:
        // cell is cleared when type is set
    default:
        break;
    }
    spreadsheet.getCellValueManager().cellUpdated(newCell);
    if (removeShifted) {
        shiftedCell.setCellValue((String) null);
        spreadsheet.getCellValueManager().cellDeleted(shiftedCell);
    }
}

From source file:com.vaadin.addon.spreadsheet.CellValueManager.java

License:Open Source License

private String getCachedFormulaCellValue(Cell formulaCell) {
    String result = null;//from w w  w.  ja va  2s .  com
    switch (formulaCell.getCachedFormulaResultType()) {
    case Cell.CELL_TYPE_STRING:
        result = formulaCell.getStringCellValue();
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        result = String.valueOf(formulaCell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        result = ErrorEval.getText(formulaCell.getErrorCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        CellStyle style = formulaCell.getCellStyle();
        result = formatter.formatRawCellContents(formulaCell.getNumericCellValue(), style.getDataFormat(),
                style.getDataFormatString());
        break;
    }
    return result;
}