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:au.com.onegeek.lambda.parser.XslxUtil.java

License:Apache License

public static Object objectFrom(final Workbook workbook, final Cell cell) {
    Object cellValue = null;//from  w w w  . ja  va  2  s. c om

    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        cellValue = cell.getRichStringCellValue().getString();
    } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        cell.setCellType(Cell.CELL_TYPE_STRING);
        cellValue = cell.getRichStringCellValue().getString();
        //            cellValue = getNumericCellValue(cell);
    } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        cellValue = cell.getBooleanCellValue();
    } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        cellValue = evaluateCellFormula(workbook, cell);
    }

    return cellValue;

}

From source file:bad.robot.excel.row.CopyRow.java

License:Apache License

private static void setCellDataValue(Cell oldCell, Cell newCell) {
    switch (oldCell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        newCell.setCellValue(oldCell.getStringCellValue());
        break;/*  w  ww.  j a v a  2s .  co m*/
    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:biz.deinum.multitenant.batch.item.excel.poi.PoiSheet.java

License:Apache License

/**
 * {@inheritDoc}//from   ww  w.  ja  v  a2  s.  c  om
 */
public String[] getRow(final int rowNumber) {
    if (rowNumber > this.delegate.getLastRowNum()) {
        return null;
    }
    final Row row = this.delegate.getRow(rowNumber);
    final List<String> cells = new LinkedList<String>();

    final Iterator<Cell> cellIter = row.iterator();
    while (cellIter.hasNext()) {
        final Cell cell = cellIter.next();
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            cells.add(String.valueOf(cell.getNumericCellValue()));
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            cells.add(String.valueOf(cell.getBooleanCellValue()));
            break;
        case Cell.CELL_TYPE_STRING:
        case Cell.CELL_TYPE_BLANK:
            cells.add(cell.getStringCellValue());
            break;
        default:
            throw new IllegalArgumentException("Cannot handle cells of type " + cell.getCellType());

        }
    }
    return cells.toArray(new String[cells.size()]);
}

From source file:blanco.commons.calc.parser.AbstractBlancoCalcParser.java

License:Open Source License

public static String getCellValue(Cell cell) {
    // 2016.01.20 j.amano
    // ?jxl to poi ?????
    //------------------------
    //??:\-1,000//from w w w  . j a  v  a 2s  . co m
    //jxl:($1,000)?$?????????
    //poi:-1000
    //------------------------
    //??:2016/1/20
    //jxl:0020, 1 20, 2016
    //poi:2016/01/20 00:00:00
    //------------------------
    //??:#REF!???
    //jxl:#REF!
    //poi:#REF!
    //------------------------
    //??:1,000
    //jxl:" "1,000
    //poi:-1000
    //------------------------

    if (cell != null) {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            return "";
        case Cell.CELL_TYPE_STRING:
            return cell.getRichStringCellValue().getString();
        case Cell.CELL_TYPE_BOOLEAN:
            return String.valueOf(cell.getBooleanCellValue());
        case Cell.CELL_TYPE_NUMERIC:
            // ??
            if (DateUtil.isCellDateFormatted(cell)) {
                // ????
                Date dt = cell.getDateCellValue();
                // ????
                DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                String sDate = df.format(dt);
                return sDate;
            }
            // ???.0
            DecimalFormat format = new DecimalFormat("0.#");
            return format.format(cell.getNumericCellValue());
        case Cell.CELL_TYPE_FORMULA:
            Workbook wb = cell.getSheet().getWorkbook();
            CreationHelper crateHelper = wb.getCreationHelper();
            FormulaEvaluator evaluator = crateHelper.createFormulaEvaluator();
            return getCellValue(evaluator.evaluateInCell(cell));
        case Cell.CELL_TYPE_ERROR:
            byte errorCode = cell.getErrorCellValue();
            FormulaError error = FormulaError.forInt(errorCode);
            String errorText = error.getString();
            return errorText;
        default:
            return "";
        }
    }
    return "";
}

From source file:bo.com.offercruzmail.utils.HojaExcelHelper.java

/**
 * *//from w  w  w.  j  a v a  2  s.  co  m
 * Devuelve el valor de la celda en cadena
 *
 * @param celda La celda que contien el valor
 * @return El valor de la celda en cadena, si la celda es nula, devuelve una
 * cadena vaca
 */
public static String getValorCelda(Cell celda) {
    if (celda == null) {
        return "";
    }
    switch (celda.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return celda.getStringCellValue();
    case Cell.CELL_TYPE_BOOLEAN:
        return String.valueOf(celda.getBooleanCellValue());
    case Cell.CELL_TYPE_NUMERIC:
        double valor = celda.getNumericCellValue();
        if (valor % 1 == 0) {
            return String.valueOf((int) valor);
        }
        return String.valueOf(valor);
    case Cell.CELL_TYPE_FORMULA:
    case Cell.CELL_TYPE_ERROR:
    case Cell.CELL_TYPE_BLANK:
    default:
        return "";
    }
}

From source file:br.com.gartech.nfse.integrador.util.ExcelHelper.java

private String cellToString(Cell cell) {
    String result = null;//from   w  w  w  .  j  av a 2  s.  c  om

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        result = cell.getStringCellValue();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            result = cell.getDateCellValue().toString();
        } else {
            int i = (int) cell.getNumericCellValue();
            double d = cell.getNumericCellValue();

            if (i == d) {
                result = String.valueOf(i);
            } else {
                result = String.valueOf(d);
            }
        }
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        result = Boolean.toString(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        result = cell.getCellFormula();
        break;
    default:
        result = null;
    }

    return result;
}

From source file:br.com.gartech.nfse.integrador.view.ImportarView.java

private Object getCellValue(Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();

    case Cell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue();

    case Cell.CELL_TYPE_NUMERIC:
        return cell.getNumericCellValue();
    }//from w  w w  . java 2 s  . c  om
    return null;
}

From source file:br.sp.telesul.service.ExportServiceImpl.java

private Object getCellValue(Cell cell) {
    Object o = "";
    try {//from w  ww  . j  a  va 2s. com
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            o = cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            o = cell.getBooleanCellValue();
            break;
        case Cell.CELL_TYPE_NUMERIC:
            o = cell.getNumericCellValue();
            break;
        case Cell.CELL_TYPE_BLANK:
            o = "";
            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e.getMessage());
    }
    return o;
}

From source file:browsermator.com.MyTable.java

MyTable(String csvFile) {
    DataFile = csvFile;//w  w  w . j  a v  a  2  s . c om
    DataTable = new JTable();
    myEntries = new ArrayList<>();
    File filecheck = new File(csvFile);
    if (filecheck.isAbsolute()) {

        String[] left_right_side_of_dot = csvFile.split("\\.");
        String file_extension = left_right_side_of_dot[left_right_side_of_dot.length - 1];

        switch (file_extension) {
        case "xls":
            try {
                FileInputStream file = new FileInputStream(new File(DataFile));

                HSSFWorkbook workbook = new HSSFWorkbook(file);

                //Get first sheet from the workbook
                HSSFSheet sheet = workbook.getSheetAt(0);

                //Iterate through each rows from first sheet
                Iterator<Row> rowIterator = sheet.iterator();

                int number_of_cells = 0;
                while (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    int number_of_thesecells = row.getPhysicalNumberOfCells();
                    if (number_of_thesecells > number_of_cells) {
                        number_of_cells = number_of_thesecells;
                    }
                }
                Iterator<Row> rowIterator2 = sheet.iterator();
                while (rowIterator2.hasNext()) {
                    Row row = rowIterator2.next();

                    String[] myRow = new String[number_of_cells];
                    Iterator<Cell> cellIterator = row.cellIterator();

                    int cell_index = 0;
                    while (cellIterator.hasNext()) {

                        Cell cell = cellIterator.next();

                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            Boolean boolvalue = cell.getBooleanCellValue();
                            String cellvalue = "false";
                            if (boolvalue) {
                                cellvalue = "true";
                            } else

                                myRow[cell_index] = cellvalue;
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            myRow[cell_index] = Double.toString(cell.getNumericCellValue());
                            break;
                        case Cell.CELL_TYPE_STRING:
                            myRow[cell_index] = cell.getRichStringCellValue().getString();
                            break;
                        case Cell.CELL_TYPE_BLANK:
                            myRow[cell_index] = "";
                            break;
                        case Cell.CELL_TYPE_ERROR:
                            myRow[cell_index] = "";
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            myRow[cell_index] = cell.getCellFormula();
                            break;
                        }
                        cell_index++;
                    }
                    if (cell_index != number_of_cells) {
                        for (int x = cell_index; x < number_of_cells; x++)
                            myRow[cell_index] = "";
                    }

                    myEntries.add(myRow);
                }

                file.close();
            }

            catch (Exception e) {
                System.out.println("Error occurred while reading XLS file: " + e.toString());
            }
            break;
        case "xlsx":
            try {
                FileInputStream file = new FileInputStream(new File(DataFile));

                XSSFWorkbook workbook = new XSSFWorkbook(file);

                //Get first sheet from the workbook
                XSSFSheet sheet = workbook.getSheetAt(0);

                //Iterate through each rows from first sheet
                Iterator<Row> rowIterator = sheet.iterator();

                int number_of_cells = 0;
                while (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    int number_of_thesecells = row.getPhysicalNumberOfCells();
                    if (number_of_thesecells > number_of_cells) {
                        number_of_cells = number_of_thesecells;
                    }
                }
                Iterator<Row> rowIterator2 = sheet.iterator();
                while (rowIterator2.hasNext()) {
                    Row row = rowIterator2.next();

                    String[] myRow = new String[number_of_cells];
                    Iterator<Cell> cellIterator = row.cellIterator();

                    int cell_index = 0;
                    while (cellIterator.hasNext()) {

                        Cell cell = cellIterator.next();

                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            Boolean boolvalue = cell.getBooleanCellValue();
                            String cellvalue = "false";
                            if (boolvalue) {
                                cellvalue = "true";
                            } else

                                myRow[cell_index] = cellvalue;
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            myRow[cell_index] = Double.toString(cell.getNumericCellValue());
                            break;
                        case Cell.CELL_TYPE_STRING:
                            myRow[cell_index] = cell.getRichStringCellValue().getString();
                            break;
                        case Cell.CELL_TYPE_BLANK:
                            myRow[cell_index] = "";
                            break;
                        case Cell.CELL_TYPE_ERROR:
                            myRow[cell_index] = "";
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            myRow[cell_index] = cell.getCellFormula();
                            break;
                        }
                        cell_index++;
                    }
                    if (cell_index != number_of_cells) {
                        for (int x = cell_index; x < number_of_cells; x++)
                            myRow[cell_index] = "";
                    }

                    myEntries.add(myRow);
                }

                file.close();
            }

            catch (Exception ex) {
                System.out.print("Exception during XLSX import: " + ex.toString());
            }

            break;
        case "csv":
            try {
                CSVFileReader = new CSVReader(new FileReader(DataFile), ',', '"', '\0');
                myEntries = CSVFileReader.readAll();
            } catch (Exception e) {

            }

        }

        columnnames = (String[]) myEntries.get(0);
        DefaultTableModel tableModel = new DefaultTableModel(columnnames, myEntries.size() - 1);
        rowcount = tableModel.getRowCount();
        this.number_of_records = rowcount;
        for (int x = 0; x < rowcount + 1; x++) {

            int columnnumber = 0;
            if (x > 0) {
                for (String thiscellvalue : (String[]) myEntries.get(x)) {
                    tableModel.setValueAt(thiscellvalue, x - 1, columnnumber);
                    columnnumber++;
                }
            }

        }

        DataTable = new JTable(tableModel);

        int number_of_rows = DataTable.getRowCount();
        if (number_of_rows < 20) {
            DataTable.setPreferredScrollableViewportSize(
                    new Dimension(1200, number_of_rows * DataTable.getRowHeight()));
        }

    } else {

        columnnames[0] = "Stored URL List:" + csvFile;
        DefaultTableModel tableModel = new DefaultTableModel(columnnames, 0);

        DataTable = new JTable(tableModel);
        DataTable.getColumnModel().getColumn(0).setPreferredWidth(200);
        DataTable.setPreferredScrollableViewportSize(new Dimension(20, 0));

    }

}

From source file:cherry.goods.excel.ExcelReader.java

License:Apache License

/**
 * ??????<br />/*from  w  w  w  .j  a  va  2  s  . c  om*/
 * 
 * @param cell ??
 * @return ?
 */
private String getCellValueAsString(Cell cell) {
    switch (cell.getCellType()) {
    case CELL_TYPE_NUMERIC:
        return getNumericCellValueAsString(cell);
    case CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case CELL_TYPE_FORMULA:
        switch (cell.getCachedFormulaResultType()) {
        case CELL_TYPE_NUMERIC:
            return getNumericCellValueAsString(cell);
        case CELL_TYPE_STRING:
            return cell.getStringCellValue();
        case CELL_TYPE_BOOLEAN:
            return String.valueOf(cell.getBooleanCellValue());
        default:
            return null;
        }
    case CELL_TYPE_BLANK:
        return "";
    case CELL_TYPE_BOOLEAN:
        return String.valueOf(cell.getBooleanCellValue());
    default:
        return null;
    }
}