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.vaadin.addon.spreadsheet.CellValueManager.java

License:Open Source License

public String getOriginalCellValue(Cell cell) {
    if (cell == null) {
        return "";
    }//from  w  ww.  j ava2  s .  c o  m

    int cellType = cell.getCellType();
    switch (cellType) {
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            Date dateCellValue = cell.getDateCellValue();
            if (dateCellValue != null) {
                return new SimpleDateFormat().format(dateCellValue);
            }
            return "";
        }
        return originalValueDecimalFormat.format(cell.getNumericCellValue());
    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case Cell.CELL_TYPE_BOOLEAN:
        return String.valueOf(cell.getBooleanCellValue());
    case Cell.CELL_TYPE_BLANK:
        return "";
    case Cell.CELL_TYPE_ERROR:
        return String.valueOf(cell.getErrorCellValue());
    }
    return "";
}

From source file:com.vaadin.addon.spreadsheet.command.CellValueCommand.java

License:Open Source License

/**
 * Returns the current value of the given Cell
 * //w  w w.ja  va2 s.  c om
 * @param cell
 *            Target cell
 * @return Current value of the cell or null if not available
 */
protected Object getCellValue(Cell cell) {
    if (cell == null) {
        return null;
    } else {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_BOOLEAN:
            return cell.getBooleanCellValue();
        case Cell.CELL_TYPE_ERROR:
            return cell.getErrorCellValue();
        case Cell.CELL_TYPE_FORMULA:
            return "=" + cell.getCellFormula();
        case Cell.CELL_TYPE_NUMERIC:
            return cell.getNumericCellValue();
        case Cell.CELL_TYPE_STRING:
            return cell.getStringCellValue();
        default:
            return null;
        }
    }
}

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

/**
 * Checks if the given cell value matches a
 * {@link ConditionalFormattingRule} of <code>VALUE_IS</code> type. Covers
 * all cell types and comparison operations.
 *
 * @param cell//  ww w . j a  va2 s  . c  om
 *            Target cell
 * @param rule
 *            Conditional formatting rule to match against.
 * @return True if the given cells value matches the given
 *         <code>VALUE_IS</code> rule, false otherwise
 */
protected boolean matchesValue(Cell cell, ConditionalFormattingRule rule) {

    boolean isFormulaType = cell.getCellType() == Cell.CELL_TYPE_FORMULA;
    boolean isFormulaStringType = isFormulaType && cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING;
    boolean isFormulaBooleanType = isFormulaType && cell.getCachedFormulaResultType() == Cell.CELL_TYPE_BOOLEAN;
    boolean isFormulaNumericType = isFormulaType && cell.getCachedFormulaResultType() == Cell.CELL_TYPE_NUMERIC;

    if (isFormulaType) {
        try {
            // make sure we have the latest value for formula cells
            getFormulaEvaluator().evaluateFormulaCell(cell);
        } catch (NotImplementedException e) {
            LOGGER.log(Level.FINEST, e.getMessage(), e);
            return false;
        }
    }

    // other than numerical types
    if (cell.getCellType() == Cell.CELL_TYPE_STRING || isFormulaStringType) {

        // Excel stores conditional formatting strings surrounded with ", so
        // we must surround the cell value. String cell value from POI is
        // never null.
        String quotedStringValue = String.format("\"%s\"", cell.getStringCellValue());

        // Excel string comparison ignores case
        switch (rule.getComparisonOperation()) {
        case ComparisonOperator.EQUAL:
            return quotedStringValue.equalsIgnoreCase(rule.getFormula1());
        case ComparisonOperator.NOT_EQUAL:
            return !quotedStringValue.equalsIgnoreCase(rule.getFormula1());
        }
    }
    if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN || isFormulaBooleanType) {
        // not sure if this is used, since no boolean option exists in
        // Excel..

        Boolean formulaVal = Boolean.parseBoolean(rule.getFormula1());

        switch (rule.getComparisonOperation()) {
        case ComparisonOperator.EQUAL:
            return cell.getBooleanCellValue() == formulaVal;
        case ComparisonOperator.NOT_EQUAL:
            return cell.getBooleanCellValue() != formulaVal;
        }
    }

    // numerical types
    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC || isFormulaNumericType) {

        double formula1Val = -1;
        try {
            formula1Val = Double.valueOf(rule.getFormula1());

        } catch (NumberFormatException w) {
            // non-numeric formatting rules cannot match
            return false;
        }

        switch (rule.getComparisonOperation()) {

        case ComparisonOperator.EQUAL:
            return cell.getNumericCellValue() == formula1Val;
        case ComparisonOperator.NOT_EQUAL:
            return cell.getNumericCellValue() != formula1Val;

        case ComparisonOperator.LT:
            return cell.getNumericCellValue() < formula1Val;
        case ComparisonOperator.LE:
            return cell.getNumericCellValue() <= formula1Val;
        case ComparisonOperator.GT:
            return cell.getNumericCellValue() > formula1Val;
        case ComparisonOperator.GE:
            return cell.getNumericCellValue() >= formula1Val;

        case ComparisonOperator.BETWEEN:
            boolean lt = cell.getNumericCellValue() >= formula1Val;
            boolean gt = cell.getNumericCellValue() <= Double.valueOf(rule.getFormula2());
            return lt && gt;

        case ComparisonOperator.NOT_BETWEEN:
            lt = cell.getNumericCellValue() <= formula1Val;
            gt = cell.getNumericCellValue() >= Double.valueOf(rule.getFormula2());
            return lt && gt;
        }
    }

    return false;
}

From source file:com.veeduria.web.cargaarchivo.aut.th.CargaPlanta.java

public void cargarArchivoEmpleados(Path rutaArchivo) {
    //Get first sheet from the workbook
    try {//from   ww w .j av  a2s  .  co  m
        StringBuilder strBSql = new StringBuilder();
        HSSFWorkbook workbook = new HSSFWorkbook(Files.newInputStream(rutaArchivo, StandardOpenOption.READ));
        HSSFSheet sheet = workbook.getSheet("EMPLEADOS");
        if (sheet != null) {
            Iterator<org.apache.poi.ss.usermodel.Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                Row row = (Row) rowIterator.next();
                if (row.getRowNum() >= 10) {
                    if (row.getCell(0) != null) {
                        for (int i = 0; i < 42; i++) {
                            Cell cell = row.getCell(i);
                            switch (cell.getCellType()) {
                            case Cell.CELL_TYPE_BOOLEAN:
                                strBSql.append(cell.getBooleanCellValue());
                                strBSql.append(",");
                                //  System.out.print(cell.getBooleanCellValue() + "\t\t");
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                strBSql.append(cell.getNumericCellValue());
                                strBSql.append(",");
                                //   System.out.print(cell.getNumericCellValue() + "\t\t");
                                break;
                            case Cell.CELL_TYPE_STRING:
                                strBSql.append(",'");
                                strBSql.append(cell.getStringCellValue());
                                strBSql.append("',");
                                //  System.out.print(cell.getStringCellValue() + "\t\t");
                                break;

                            }

                        }
                        strBSql.replace(strBSql.length() - 1, strBSql.length(), "");
                    }

                }
                strBSql.append(System.getProperty("line.separator"));
            }

            System.out.println("");
        }

        //            FileOutputStream out = new FileOutputStream(new File(System.getProperty("user.home") + File.separator + "test.xls"));
        //            workbook.write(out);
        //            out.close();
    } catch (IOException e) {
        Logger.getLogger(VigilarCarpetaSLBean.class.getName()).log(Level.SEVERE, null, e);
    }
}

From source file:com.vermeg.convertisseur.service.ConvServiceImpl.java

/**
 *
 * @param file//from ww w . j  a  va2s  .com
 * @return
 * @throws FileNotFoundException
 * @throws InvalidFormatException
 * @throws IOException
 */
/*this method convert a multipart file to json object */
@Override
public JSONObject convert(MultipartFile file, String name)
        throws FileNotFoundException, InvalidFormatException, IOException {

    // File file = new File("C:\\Users\\Ramzi\\Documents\\PFE\\developpement\\avancement.xlsx");

    File filez = File.createTempFile("fichier", "xslx");
    file.transferTo(filez);
    FileInputStream inp = new FileInputStream(filez);
    Workbook workbook = WorkbookFactory.create(inp);
    //Sheet sheet = workbook.getSheetAt( 0 );
    Sheet sheet = workbook.getSheet(name);
    // Start constructing JSON.
    JSONObject json = new JSONObject();

    // Iterate through the rows.
    JSONArray rows = new JSONArray();
    for (Iterator<Row> rowsIT = sheet.rowIterator(); rowsIT.hasNext();) {
        Row row = rowsIT.next();
        JSONObject jRow = new JSONObject();

        // Iterate through the cells.
        JSONArray cells = new JSONArray();
        for (Iterator<Cell> cellsIT = row.cellIterator(); cellsIT.hasNext();) {
            Cell cell = cellsIT.next();
            // System.out.println(cell.getCellType());
            //           cells.put(cell.getDateCellValue());
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                cells.put(cell.getRichStringCellValue().getString());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    cells.put(cell.getDateCellValue());
                } else {
                    cells.put(cell.getNumericCellValue());
                }
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                cells.put(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                cells.put(cell.getCellFormula());
                break;
            default:
                System.out.println();
            }
        }
        jRow.put("cell", cells);
        rows.put(cells);
        //rows.put( jRow );
    }

    // Create the JSON.
    json.put("rows", rows);
    System.out.println(json.toString());
    return json;
}

From source file:com.vertec.daoimpl.AttendanceDAOImpl.java

public List<Object> readexcel(String path) {
    //        String data = "";
    List<Object> table = new ArrayList<Object>();
    try {/*from   www  .  ja v a 2 s. c  o  m*/
        // Get the workbook instance for XLSX file
        XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(path));

        // 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();
            List<String> rows = new ArrayList<String>();
            while (cellIterator.hasNext()) {
                cell = cellIterator.next();

                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    rows.add(cell.getRichStringCellValue().getString());
                    //                            data+=cell.getRichStringCellValue().getString();
                    //                            System.out.print(cell.getRichStringCellValue().getString());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {

                        rows.add(cell.getDateCellValue() + "");
                        //                                data+=cell.getDateCellValue();
                        //                                System.out.print(cell.getDateCellValue());
                    } else {
                        rows.add(cell.getNumericCellValue() + "");
                        //                                data+=cell.getNumericCellValue();
                        //                                System.out.print(cell.getNumericCellValue());
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    rows.add(cell.getBooleanCellValue() + "");
                    //                            data+=cell.getBooleanCellValue();
                    //                            System.out.print(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    rows.add(cell.getCellFormula() + "");
                    //                            data+=cell.getCellFormula();
                    //                            System.out.print(cell.getCellFormula());
                    break;
                default:
                    //                            System.out.print("");
                }
                //                    data += "-";
                //                    System.out.print(" - ");
            }
            table.add(rows);
            //                data += ";;;";
            //                System.out.println(";;;");
        }
    } catch (Exception e) {
        System.err.println("Exception :" + e.getMessage());
    }
    return table;
}

From source file:com.vertec.daoimpl.AttendanceDAOImpl.java

public String readexcel2(String path) {
    try {/*from   ww w  . j  av a2s . co m*/
        // Get the workbook instance for XLSX file
        XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(path));

        // 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_STRING:
                    System.out.print(cell.getRichStringCellValue().getString());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        System.out.print(cell.getDateCellValue());
                    } else {
                        System.out.print(cell.getNumericCellValue());
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    System.out.print(cell.getCellFormula());
                    break;
                default:
                    System.out.print("");
                }
                System.out.print(" - ");
            }
            System.out.println(";;;");
        }
    } catch (Exception e) {
        System.err.println("Exception :" + e.getMessage());
    }
    return null;
}

From source file:com.virtusa.isq.vtaf.runtime.SeleniumTestBase.java

License:Apache License

/**
 * Adds the values from excel.//from  w w  w.j  a v a  2  s. c om
 * 
 * @param path
 *            the path
 * @param index
 *            the index
 * @return the string[][]
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws InvalidFormatException
 *             the invalid format exception
 */
public final String[][] addValuesFromExcel(final String path, final String index)
        throws IOException, InvalidFormatException {

    String cellStringValue = null;
    double cellDoubleValue = 0;
    Boolean cellBooleanValue;
    byte cellErrorValue = 0;
    String[][] arrExcelContent;
    FileInputStream file = null;
    Workbook workbook = null;

    Sheet sheet = null;
    try {
        file = new FileInputStream(new File(path));
        workbook = WorkbookFactory.create(file);
        sheet = workbook.getSheetAt(Integer.parseInt(index));
        Iterator<Row> rowIterator = sheet.iterator();
        arrExcelContent = new String[sheet.getPhysicalNumberOfRows()][];
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            int rowNumber = row.getRowNum();
            Iterator<Cell> cellIterator = row.cellIterator();
            arrExcelContent[rowNumber] = new String[sheet.getRow(rowNumber).getPhysicalNumberOfCells()];
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                int cellNumber = cell.getColumnIndex();
                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    cellStringValue = cell.getStringCellValue();
                    arrExcelContent[rowNumber][cellNumber] = cellStringValue;
                } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                    cellBooleanValue = cell.getBooleanCellValue();
                    arrExcelContent[rowNumber][cellNumber] = cellBooleanValue.toString();
                } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
                    cellErrorValue = cell.getErrorCellValue();
                    arrExcelContent[rowNumber][cellNumber] = Byte.toString(cellErrorValue);
                } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
                    cellStringValue = cell.getCellFormula();
                    arrExcelContent[rowNumber][cellNumber] = cellStringValue;

                } else {

                    cellDoubleValue = cell.getNumericCellValue();
                    arrExcelContent[rowNumber][cellNumber] = Double.toString(cellDoubleValue);
                }
            }

        }
    } finally {
        if (((InputStream) workbook) != null) {
            ((InputStream) workbook).close();
        }
    }
    return arrExcelContent;
}

From source file:com.wabacus.system.dataimport.filetype.XlsFileProcessor.java

License:Open Source License

private Object getCellValue(Cell cell) {
    if (cell == null)
        return null;
    switch (cell.getCellType()) {
    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)) {
            return cell.getDateCellValue();
        } else {/*from  w ww. j ava2 s .c  om*/
            return String.valueOf(cell.getNumericCellValue());
            /*double d=cell.getNumericCellValue();
            if(d-(int)d<Double.MIN_VALUE)
            { // ?int  
                return (int)d;
            }else
            { 
                return cell.getNumericCellValue();
            }*/
        }
    case Cell.CELL_TYPE_BLANK:
        return "";
    default:
        return null;
    }
}

From source file:com.xn.interfacetest.service.impl.TestCaseServiceImpl.java

License:Open Source License

/**
 * ?Cell?/*from   w w w .j a  v  a  2s. com*/
 * @param cell
 * @return
 */
private Object getCellFormatValue(Cell cell) {
    if (null == cell) {
        return "";
    }
    DataFormatter formatter = new DataFormatter();
    switch (cell.getCellTypeEnum()) {
    case STRING:
        return cell.getRichStringCellValue().getString();
    case NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        } else {
            return Math.round(cell.getNumericCellValue());
        }
    case BOOLEAN:
        return cell.getBooleanCellValue();
    case FORMULA:
        return cell.getCellFormula();
    case BLANK:
        return "";
    default:
        return "";
    }

}