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:de.tum.in.socket.server.ReadExcel.java

License:Apache License

/**
 * Returns the type of value from a cell
 *//*from   w w  w .  j ava2s.c  o  m*/
private static Object getTypeValue(final Class<?> type, final Cell cell) {
    Object typedValue = null;
    final DataFormatter formatter = new DataFormatter();
    if (type == int.class) {
        typedValue = (int) cell.getNumericCellValue();
    } else if (type == double.class) {
        typedValue = cell.getNumericCellValue();
    } else if (type == boolean.class) {
        typedValue = cell.getBooleanCellValue();
    } else if (type == String.class) {
        typedValue = formatter.formatCellValue(cell);
    }
    return typedValue;
}

From source file:demons.studentsmanagesystem.excel.poi.PoiSheet.java

License:Apache License

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

    for (int i = 0; i < getNumberOfColumns(); i++) {
        Cell cell = row.getCell(i);
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                cells.add(String.valueOf(date.getTime()));
            } else {
                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;
        case Cell.CELL_TYPE_FORMULA:
            cells.add(getFormulaEvaluator().evaluate(cell).formatAsString());
            break;
        default:
            throw new IllegalArgumentException("Cannot handle cells of type " + cell.getCellType());
        }
    }
    return cells.toArray(new String[cells.size()]);
}

From source file:domain.Excel.java

private static void showExelData(List sheetData) {

        ///*from w  ww  .  j a  v  a2 s.  c  om*/
        // Iterates the data and print it out to the console.
        //
        for (int i = 0; i < sheetData.size(); i++) {

            List list = (List) sheetData.get(i);
            for (int j = 0; j < list.size(); j++) {

                Cell cell = (Cell) list.get(j);

                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {

                    System.out.print(cell.getNumericCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {

                    System.out.print(cell.getRichStringCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {

                    System.out.print(cell.getBooleanCellValue());
                }
                if (j < list.size() - 1) {
                    System.out.print(", ");
                }
            }
            System.out.println("");
        }
    }

From source file:ec.mil.he1.mbeans.JSFManagedBeanFileUpload.java

public String convertjava() {
    grabar = "0";
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    try {/*  w ww.jav a 2  s .c o  m*/
        connection = he1_pool.getConnection();

        String sql = "Insert into CODIGO_MIFIN(CEDULA, CODIGO, MES , MES_NUMERO, ANIO , DESCRIPCION, ARCHIVO)  Values   "
                + " (?, ?, ?, ? , ?, ? , ? )";
        int columna = 0;

        /*PrepareStatement*/
        preparedStatement = connection.prepareStatement(sql);

        //variables donde cargar los datos por cada celda
        String cc = "";
        String codigo = "";
        String mes = "";
        String mes_numero = "";
        String anio = "";
        String descripcion = "";

        file = (FileInputStream) inputstream;

        // Get the workbook instance for XLS file
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        sheet = workbook.getSheetAt(0);
        // Iterate through each rows from first sheet
        Iterator<org.apache.poi.ss.usermodel.Row> rowIterator = sheet.rowIterator();
        //aca se barre todas las filas
        while (rowIterator.hasNext()) {

            org.apache.poi.ss.usermodel.Row row = rowIterator.next();
            // For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();
            //aca se tiene las columnas por ello encero
            columna = 0;
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();

                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    cell.getBooleanCellValue();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    cell.getNumericCellValue();
                    break;
                case Cell.CELL_TYPE_STRING:
                    cell.getStringCellValue();
                    break;
                }

                if (columna == 0) {
                    cc = cell.getStringCellValue();

                } else if (columna == 1) {
                    codigo = cell.getStringCellValue();

                } else if (columna == 2) {
                    mes = cell.getStringCellValue();

                } else if (columna == 3) {
                    mes_numero = cell.getStringCellValue();

                } else if (columna == 4) {
                    anio = cell.getStringCellValue();

                } else if (columna == 5) {
                    descripcion = cell.getStringCellValue();

                }

                columna++;
            }
            preparedStatement.setString(1, cc);
            preparedStatement.setString(2, codigo);
            preparedStatement.setString(3, mes);
            preparedStatement.setString(4, mes_numero);
            preparedStatement.setString(5, anio);
            preparedStatement.setString(6, descripcion);
            preparedStatement.setString(7, nombre_archivo);
            preparedStatement.addBatch();
            cc = "";
            codigo = "";
            mes = "";
            mes_numero = "";
            anio = "";
            descripcion = "";
            System.out.println("");
        }
        file.close();
        int[] affectedRecords = preparedStatement.executeBatch();
        addMessage("Se ha cargado la informacin en el sistema");

    } catch (IOException ex) {
        Logger.getLogger(JSFManagedBeanFileUpload.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(JSFManagedBeanFileUpload.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
                preparedStatement = null;
            } catch (SQLException ex) {
                Logger.getLogger(JSFManagedBeanFileUpload.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (connection != null) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException ex) {
                Logger.getLogger(JSFManagedBeanFileUpload.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    return null;
}

From source file:edms.core.Config.java

License:Open Source License

public static void convertToXlsx(InputStream inStream, java.io.File outputFile) {
    // For storing data into CSV files
    StringBuffer cellValue = new StringBuffer();
    try {//from   www. j a va  2  s  .  c  o  m
        FileOutputStream fos = new FileOutputStream(outputFile);

        // Get the workbook instance for XLSX file
        XSSFWorkbook wb = new XSSFWorkbook(inStream);

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

        Row row;
        Cell cell;

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

        while (rowIterator.hasNext()) {
            row = rowIterator.next();

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

                switch (cell.getCellType()) {

                case Cell.CELL_TYPE_BOOLEAN:
                    cellValue.append(cell.getBooleanCellValue() + ",");
                    break;

                case Cell.CELL_TYPE_NUMERIC:
                    cellValue.append(cell.getNumericCellValue() + ",");
                    break;

                case Cell.CELL_TYPE_STRING:
                    cellValue.append(cell.getStringCellValue() + ",");
                    break;

                case Cell.CELL_TYPE_BLANK:
                    cellValue.append("" + ",");
                    break;

                default:
                    cellValue.append(cell + ",");

                }
            }
        }

        fos.write(cellValue.toString().getBytes());

        fos.close();

    } catch (Exception e) {
        System.err.println("Exception :" + e.getMessage());
    }

}

From source file:edms.core.Config.java

License:Open Source License

public static void convertToXls(InputStream inStream, java.io.File outputFile) {
    // For storing data into CSV files
    StringBuffer cellDData = new StringBuffer();
    try {/*from  w ww.j av  a 2 s.co  m*/
        FileOutputStream fos = new FileOutputStream(outputFile);

        // Get the workbook instance for XLS file
        HSSFWorkbook workbook = new HSSFWorkbook(inStream);
        // Get first sheet from the workbook
        HSSFSheet sheet = workbook.getSheetAt(0);
        Cell cell;
        Row row;

        // Iterate through each rows from first sheet
        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) {
            row = rowIterator.next();

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

                switch (cell.getCellType()) {

                case Cell.CELL_TYPE_BOOLEAN:
                    cellDData.append(cell.getBooleanCellValue() + ",");
                    break;

                case Cell.CELL_TYPE_NUMERIC:
                    cellDData.append(cell.getNumericCellValue() + ",");
                    break;

                case Cell.CELL_TYPE_STRING:
                    cellDData.append(cell.getStringCellValue() + ",");
                    break;

                case Cell.CELL_TYPE_BLANK:
                    cellDData.append("" + ",");
                    break;

                default:
                    cellDData.append(cell + ",");
                }
            }
        }

        fos.write(cellDData.toString().getBytes());
        fos.close();

    } catch (FileNotFoundException e) {
        System.err.println("Exception" + e.getMessage());
    } catch (IOException e) {
        System.err.println("Exception" + e.getMessage());
    }

}

From source file:edu.emory.cci.aiw.cvrg.eureka.etl.spreadsheet.XlsxDataProvider.java

License:Open Source License

/**
 * Read a string value from the given cell.
 *
 * @param cell The cell to read value from.
 * @return A String containing the cell value, if valid, null otherwise.
 *///from  w ww .  j  a  va  2  s  .  c  o  m
private String readStringValue(String sheetName, Cell cell) throws DataProviderException {
    String result = null;
    if (cell != null) {
        try {
            int cellType = cell.getCellType();
            if (cellType == Cell.CELL_TYPE_STRING) {
                result = cell.getStringCellValue();
            } else if (cellType == Cell.CELL_TYPE_NUMERIC) {
                result = Double.toString(cell.getNumericCellValue());
            } else if (cellType == Cell.CELL_TYPE_BOOLEAN) {
                result = Boolean.toString(cell.getBooleanCellValue());
            } else {
                throwException(sheetName, cell, "Cell type must be a number, string, boolean or blank");
            }
        } catch (Exception e) {
            throwException(sheetName, cell, e.getMessage(), e);
        }
    }
    return result;
}

From source file:edu.ucsd.bioeng.coreplugin.tableImport.reader.ExcelAttributeSheetReader.java

License:Open Source License

/**
 * For a given Excell row, convert the cells into String.
 *
 * @param row/*from w w w  .j av a2  s.  com*/
 * @return
 */
private String[] createElementStringArray(Row row) {
    String[] cells = new String[mapping.getColumnCount()];
    Cell cell = null;

    for (short i = 0; i < mapping.getColumnCount(); i++) {
        cell = row.getCell(i);

        if (cell == null) {
            cells[i] = null;
        } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
            cells[i] = cell.getRichStringCellValue().getString();
        } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            if (mapping.getAttributeTypes()[i] == CyAttributes.TYPE_INTEGER) {
                Double dblValue = cell.getNumericCellValue();
                Integer intValue = dblValue.intValue();
                cells[i] = intValue.toString();
            } else {
                cells[i] = Double.toString(cell.getNumericCellValue());
            }
        } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            cells[i] = Boolean.toString(cell.getBooleanCellValue());
        } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
            cells[i] = null;
        } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
            cells[i] = null;
            logger.warn("Error found when reading a cell!");
        }
    }

    return cells;
}

From source file:edu.ucsd.bioeng.coreplugin.tableImport.reader.ExcelNetworkSheetReader.java

License:Open Source License

/**
 * For a given Excell row, convert the cells into String.
 *
 * @param row/* ww w . ja v  a  2 s .co m*/
 * @return
 */
private String[] createElementStringArray(final Row row) {
    String[] cells = new String[nmp.getColumnCount()];
    Cell cell = null;

    for (short i = 0; i < nmp.getColumnCount(); i++) {
        cell = row.getCell(i);

        if (cell == null) {
            cells[i] = null;
        } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
            cells[i] = cell.getRichStringCellValue().getString();
        } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            if (nmp.getAttributeTypes()[i] == CyAttributes.TYPE_INTEGER) {
                Double dblValue = cell.getNumericCellValue();
                Integer intValue = dblValue.intValue();
                cells[i] = intValue.toString();
            } else {
                cells[i] = Double.toString(cell.getNumericCellValue());
            }
        } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            cells[i] = Boolean.toString(cell.getBooleanCellValue());
        } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
            cells[i] = null;
        } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
            cells[i] = null;
            logger.warn("Error found when reading a cell!");
        }
    }

    return cells;
}

From source file:edu.ucsd.bioeng.coreplugin.tableImport.ui.PreviewTablePanel.java

License:Open Source License

private TableModel parseExcel(final URL sourceURL, int size, TableCellRenderer renderer, final Sheet sheet,
        int startLine) throws IOException {

    if (size == -1)
        size = Integer.MAX_VALUE;

    int maxCol = 0;
    final Vector<Object> data = new Vector<Object>();

    int rowCount = 0;
    Row row;//from  www  .j  a va2  s.c o m

    while (((row = sheet.getRow(rowCount)) != null) && (rowCount < size)) {
        if (rowCount >= startLine) {
            Vector<Object> rowVector = new Vector<Object>();

            if (maxCol < row.getPhysicalNumberOfCells()) {
                maxCol = row.getPhysicalNumberOfCells();
            }

            for (short j = 0; j < maxCol; j++) {
                Cell cell = row.getCell(j);

                if (cell == null) {
                    rowVector.add(null);
                } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    rowVector.add(cell.getRichStringCellValue().getString());
                } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    final Double dblValue = cell.getNumericCellValue();
                    final Integer intValue = dblValue.intValue();

                    if (intValue.doubleValue() == dblValue) {
                        rowVector.add(intValue.toString());
                    } else {
                        rowVector.add(dblValue.toString());
                    }
                } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                    rowVector.add(Boolean.toString(cell.getBooleanCellValue()));
                } else if ((cell.getCellType() == Cell.CELL_TYPE_BLANK)
                        || (cell.getCellType() == Cell.CELL_TYPE_ERROR)) {
                    rowVector.add(null);
                } else {
                    rowVector.add(null);
                }
            }

            data.add(rowVector);
        }

        rowCount++;
    }

    return new DefaultTableModel(data, this.getDefaultColumnNames(maxCol, sourceURL));
}