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:negocio.parser.ExcelReader.java

@Override
public IExcelContent leerArchivo(String ruta) throws Exception {
    java.util.Date date = new java.util.Date();
    Date entrada = new Date(date.getTime());
    IExcelContent ec = ExcelContent.getInstantiateExcelContent();
    try {//ww  w  . j ava 2 s .co m
        LogDAO dao = new LogDAO();
        LogDTO dto = new LogDTO("Leer archivo", "Comienzo de lectura de archivo", entrada.toString(),
                entrada.toString());
        dao.registrarLog(dto);
        File archivo = new File(ruta);
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(archivo)); //crear un libro excel
        XSSFSheet sheet = workbook.getSheetAt(0); //acceder a la primera hoja
        Iterator<Row> rowIterator = sheet.iterator();
        Row row;
        boolean sw = true;
        ArrayList<List<String>> datos = new ArrayList<>();
        while (rowIterator.hasNext()) {

            row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            Cell celda;
            List<String> fila = new ArrayList<>();
            while (cellIterator.hasNext()) {
                celda = cellIterator.next();
                String dato = "";
                switch (celda.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    if (HSSFDateUtil.isCellDateFormatted(celda)) {
                        dato = celda.getDateCellValue().toString();
                    } else {
                        dato = celda.getNumericCellValue() + "";
                    }
                    break;
                case Cell.CELL_TYPE_STRING:
                    dato = celda.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    dato = celda.getBooleanCellValue() + "";
                    break;
                }
                fila.add(dato);
            }
            if (sw) {
                sw = false;
                ec.setTitulos(fila);
            } else {
                datos.add(fila);
            }
        }
        ec.setDatos(datos);
        workbook.close();
        return ec;
    } catch (IOException ex) {
        Logger.getLogger(ExcelReader.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;

}

From source file:net.bafeimao.umbrella.support.data.entity.ExcelEntityParser.java

License:Apache License

private String getCellValue(Cell cell) {
    Preconditions.checkNotNull("cell");

    Object retVal = null;//from   www  .  ja  v  a  2 s  .  c  o m
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_FORMULA:
        // Get the type of Formula
        switch (cell.getCachedFormulaResultType()) {
        case Cell.CELL_TYPE_STRING:
            retVal = cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_NUMERIC:
            retVal = cell.getNumericCellValue();
            break;
        default:
        }
        // retVal = formulaEval.evaluate(cell).formatAsString();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell))
            retVal = cell.getDateCellValue();
        else
            retVal = cell.getNumericCellValue();
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        retVal = cell.getBooleanCellValue();
        break;
    case Cell.CELL_TYPE_STRING:
        retVal = cell.getStringCellValue();
        break;
    default:
        retVal = null;
    }
    return retVal == null ? null : retVal.toString();
}

From source file:net.codejava.excel.ExcelBean.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 .  ja v a 2 s  . co  m

    return null;
}

From source file:net.mcnewfamily.rmcnew.model.Util.java

License:Open Source License

public static String getCellValueAsStringOrEmptyString(Cell cell) {
    if (cell == null) {
        return "";
    }//from  ww w .  j a v a 2  s  .com
    String value;
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        value = cell.getRichStringCellValue().getString();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            value = cell.getDateCellValue().toString();
        } else {
            value = Integer.toString((int) cell.getNumericCellValue());
        }
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        value = Boolean.toString(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        value = cell.getCellFormula();
        break;
    default:
        value = "";
    }
    return value;
}

From source file:net.pcal.sqlsheet.XlsResultSet.java

License:Apache License

public boolean getBoolean(int jdbcColumn) throws SQLException {
    Cell cell = getCell(jdbcColumn);
    return cell != null && cell.getBooleanCellValue();
}

From source file:net.pcal.sqlsheet.XlsResultSet.java

License:Apache License

public boolean getBoolean(String jdbcColumn) throws SQLException {
    Cell cell = getCell(jdbcColumn);
    return cell != null && cell.getBooleanCellValue();
}

From source file:net.pcal.sqlsheet.XlsResultSet.java

License:Apache License

public Object getObject(int jdbcColumn) throws SQLException {
    Cell cell = getCell(jdbcColumn);
    int columnType = metadata.getColumnType(jdbcColumn);
    try {//  ww  w  .  j  a v a  2s  .  c  om
        if (cell == null) {
            return null;
        }
        switch (cell.getCellType()) {

        case Cell.CELL_TYPE_BOOLEAN:
            if (columnType == Types.VARCHAR) {
                return cell.getBooleanCellValue();
            } else {
                throw new RuntimeException("The cell (" + getCurrentRow() + "," + jdbcColumn
                        + ") is a boolean and cannot be cast to ("
                        + XlsResultSetMetaData.columnTypeNameMap.get(columnType) + ".");
            }
        case Cell.CELL_TYPE_STRING:
            if (columnType == Types.VARCHAR) {
                return cell.getStringCellValue();
            } else {
                throw new RuntimeException("The cell (" + getCurrentRow() + "," + jdbcColumn
                        + ") is a string cell and cannot be cast to ("
                        + XlsResultSetMetaData.columnTypeNameMap.get(columnType) + ".");
            }
        case Cell.CELL_TYPE_NUMERIC:
            if (columnType == Types.VARCHAR) {
                return String.valueOf(cell.getNumericCellValue());
            } else if (columnType == Types.DOUBLE) {
                return cell.getNumericCellValue();
            } else if (columnType == Types.DATE) {
                if (DateUtil.isCellDateFormatted(cell)) {
                    java.util.Date value = cell.getDateCellValue();
                    return new java.sql.Date(value.getTime());
                }
            } else {
                throw new RuntimeException("The cell (" + getCurrentRow() + "," + jdbcColumn
                        + ") is a numeric cell and cannot be cast to ("
                        + XlsResultSetMetaData.columnTypeNameMap.get(columnType) + ".");
            }

        default:
            return null;
        }
    } catch (Exception e) {
        throw wrapped(e);
    }
}

From source file:net.sf.excelutils.ExcelParser.java

License:Apache License

/**
 * parse the cell//from   w  w w. j  a v  a  2s  .co m
 * 
 * @param context data object
 * @param cell excel cell
 */
public static void parseCell(Object context, Sheet sheet, Row row, Cell cell) {

    String str = cell.getStringCellValue();
    if (null == str || "".equals(str)) {
        return;
    }

    if (str.indexOf(VALUED_DELIM) < 0)
        return;

    boolean bJustExpr = str.length() == (str.length() - str.lastIndexOf(VALUED_DELIM));
    boolean bMerge = "!".equals(str.substring(str.indexOf(VALUED_DELIM) + VALUED_DELIM.length(),
            str.indexOf(VALUED_DELIM) + VALUED_DELIM.length() + 1));

    if (str.indexOf(VALUED_DELIM) < 0)
        return;

    Object value = parseStr(context, str);

    // replace the cell
    if (null != value) {
        if (bJustExpr && "java.lang.Integer".equals(value.getClass().getName())) {
            cell.setCellValue(Double.parseDouble(value.toString()));
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);
        } else if (bJustExpr && "java.lang.Double".equals(value.getClass().getName())) {
            cell.setCellValue(((Double) value).doubleValue());
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);
        } else if (bJustExpr && "java.util.Date".equals(value.getClass().getName())) {
            cell.setCellValue((Date) value);
        } else if (bJustExpr && "java.lang.Boolean".equals(value.getClass().getName())) {
            cell.setCellValue(((Boolean) value).booleanValue());
            cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
        } else if (bJustExpr && Number.class.isAssignableFrom(value.getClass())) {
            cell.setCellValue(((Number) (value)).doubleValue());
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);
        } else {
            // cell.setEncoding(Workbook.ENCODING_UTF_16); POI3.2?
            cell.setCellValue(value.toString());
        }
    } else {
        cell.setCellValue("");
    }

    // merge the cell that has a "!" character at the expression
    if (row.getRowNum() - 1 >= sheet.getFirstRowNum() && bMerge) {
        Row lastRow = WorkbookUtils.getRow(row.getRowNum() - 1, sheet);
        Cell lastCell = WorkbookUtils.getCell(lastRow, cell.getColumnIndex());
        boolean canMerge = false;
        if (lastCell.getCellType() == cell.getCellType()) {
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                canMerge = lastCell.getStringCellValue().equals(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                canMerge = lastCell.getBooleanCellValue() == cell.getBooleanCellValue();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                canMerge = lastCell.getNumericCellValue() == cell.getNumericCellValue();
                break;
            }
        }
        if (canMerge) {
            CellRangeAddress region = new CellRangeAddress(lastRow.getRowNum(), row.getRowNum(),
                    lastCell.getColumnIndex(), cell.getColumnIndex());
            sheet.addMergedRegion(region);
        }
    }

}

From source file:net.sf.excelutils.WorkbookUtils.java

License:Apache License

/**
 * get value of the cell//from  w w  w. j  av  a  2  s . c  o m
 *
 * @param sheet
 * @param rowNum
 * @param colNum
 * @return boolean value
 */
public static boolean getBooleanCellValue(Sheet sheet, int rowNum, int colNum) {
    Row row = getRow(rowNum, sheet);
    Cell cell = getCell(row, colNum);
    return cell.getBooleanCellValue();
}

From source file:net.sf.excelutils.WorkbookUtils.java

License:Apache License

/**
 * copy row// www . j  a  va2s .c  om
 *
 * @param sheet
 * @param from begin of the row
 * @param to destination fo the row
 * @param count count of copy
 */
public static void copyRow(Sheet sheet, int from, int to, int count) {

    for (int rownum = from; rownum < from + count; rownum++) {
        Row fromRow = sheet.getRow(rownum);
        Row toRow = getRow(to + rownum - from, sheet);
        if (null == fromRow)
            return;
        toRow.setHeight(fromRow.getHeight());
        toRow.setHeightInPoints(fromRow.getHeightInPoints());
        int lastCellNum = fromRow.getLastCellNum();
        lastCellNum = lastCellNum > 255 ? 255 : lastCellNum;
        for (int i = fromRow.getFirstCellNum(); i <= lastCellNum && i >= 0; i++) {
            Cell fromCell = getCell(fromRow, i);
            Cell toCell = getCell(toRow, i);
            // toCell.setEncoding(fromCell.getEncoding());
            toCell.setCellStyle(fromCell.getCellStyle());
            toCell.setCellType(fromCell.getCellType());
            switch (fromCell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                toCell.setCellValue(fromCell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                toCell.setCellFormula(fromCell.getCellFormula());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                toCell.setCellValue(fromCell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_STRING:
                toCell.setCellValue(fromCell.getStringCellValue());
                break;
            default:
            }
        }
    }

    // copy merged region
    List shiftedRegions = new ArrayList();
    for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
        CellRangeAddress r = sheet.getMergedRegion(i);
        if (r.getFirstRow() >= from && r.getLastRow() < from + count) {
            CellRangeAddress n_r = new CellRangeAddress(r.getFirstRow() + to - from, r.getLastRow() + to - from,
                    r.getFirstColumn(), r.getLastColumn());
            shiftedRegions.add(n_r);
        }
    }

    // readd so it doesn't get shifted again
    Iterator iterator = shiftedRegions.iterator();
    while (iterator.hasNext()) {
        CellRangeAddress region = (CellRangeAddress) iterator.next();
        sheet.addMergedRegion(region);
    }
}