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:xlsxtocsv.java

static void convertToXlsx(File inputFile, File outputFile) {
    // For storing data into CSV files
    StringBuffer cellValue = new StringBuffer();
    try {//from w  w w.j a  v  a  2  s .  co m
        FileOutputStream fos = new FileOutputStream(outputFile);

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

        // 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 + ",");

                }
            }

            cellValue.append("\n");
        }

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

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

From source file:xlsxtocsv.java

static void convertToXls(File inputFile, File outputFile) {
    // For storing data into CSV files
    StringBuffer cellDData = new StringBuffer();
    try {//from   w  w  w .  ja  v  a2  s . c o m
        FileOutputStream fos = new FileOutputStream(outputFile);

        // Get the workbook instance for XLS file
        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));
        // 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 + ",");
                }
            }

            cellDData.append("\n");
        }

        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:ambit2.core.io.IteratingXLSReader.java

License:Open Source License

public Object next() {
    IAtomContainer mol = null;//from   www.j a  v a2 s .c o m
    Map properties = new Hashtable();
    try {
        Row row = (Row) iterator.next();

        for (int col = 0; col < getNumberOfColumns(); col++) {
            Cell cell = row.getCell(col);
            Object value = null;
            if (cell != null)
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    value = cell.getBooleanCellValue();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    value = cell.getNumericCellValue();
                    break;
                case Cell.CELL_TYPE_STRING:
                    value = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_BLANK:
                    value = "";
                    break;
                case Cell.CELL_TYPE_ERROR:
                    value = "";
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    try {
                        value = cell.getStringCellValue();
                        break;
                    } catch (Exception x) {
                        try {
                            value = cell.getNumericCellValue();
                        } catch (Exception z) {
                            logger.log(Level.WARNING, x.getMessage(), x);
                        }
                    }
                }
            else
                value = "";
            try {
                if (smilesIndex == col) {
                    try {
                        mol = sp.parseSmiles(value.toString());
                        properties.put(AmbitCONSTANTS.SMILES, value.toString());
                    } catch (InvalidSmilesException x) {
                        logger.warning("Invalid SMILES!\t" + value);
                        properties.put(AmbitCONSTANTS.SMILES, "Invalid SMILES");
                    }
                } else if (col < getNumberOfColumns())
                    properties.put(getHeaderColumn(col), value);
            } catch (Exception x) {
                logger.log(Level.WARNING, x.getMessage(), x);
            }

        }
        if (mol == null)
            mol = SilentChemObjectBuilder.getInstance().newInstance(IMolecule.class);
        mol.setProperties(properties);
        processRow(mol);
    } catch (Exception x) {
        logger.log(Level.SEVERE, x.getMessage(), x);
    }
    return mol;

}

From source file:android_connector.ExcelReader.java

/**
 * Gibt eine vernnftige Darstellung einer Zelle als String zurck.
 * @param cell die Zelle//from  www . j  a va 2  s. com
 * @return z.B. bei Zelle, die eine Gleichung enthlt, deren Ergebnis
 */
private String differCellType(Cell cell) {
    String returnValue = "";
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN:
        returnValue = String.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        returnValue = String.valueOf(cell.getNumericCellValue());
        break;
    case Cell.CELL_TYPE_STRING:
        returnValue = cell.getStringCellValue();
        break;
    case Cell.CELL_TYPE_FORMULA:
        FormulaEvaluator evaluator = this.wb.getCreationHelper().createFormulaEvaluator();
        CellValue cellValue = evaluator.evaluate(cell);
        returnValue = cellValue.getStringValue();
        break;
    case Cell.CELL_TYPE_ERROR:
        returnValue = String.valueOf(cell.getErrorCellValue());
        break;
    case Cell.CELL_TYPE_BLANK:
        returnValue = "";
        break;
    default:
        returnValue = "default value at (" + cell.getRowIndex() + ";" + cell.getColumnIndex() + ") !";
        break;
    }
    return returnValue;
}

From source file:apm.common.utils.excel.ImportExcel.java

License:Open Source License

/**
 * ??//from  www . jav a  2s . c  om
 * @param row ?
 * @param column ???
 * @return ?
 */
public Object getCellValue(Row row, int column) {
    Object val = "";
    try {
        Cell cell = row.getCell(column);
        if (cell != null) {
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                val = cell.getNumericCellValue();
            } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                val = cell.getStringCellValue();
            } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
                val = cell.getCellFormula();
            } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                val = cell.getBooleanCellValue();
            } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
                val = cell.getErrorCellValue();
            }
        }
    } catch (Exception e) {
        return val;
    }
    return val;
}

From source file:at.mukprojects.exclycore.dao.XLSXReader.java

License:Open Source License

private ExclyString readString(Cell cell, int type) throws Exception {
    ExclyString output = null;//from www.jav  a2 s  . c  o  m

    if (type == Cell.CELL_TYPE_STRING) {
        output = new ExclyString(cell.getStringCellValue());
    } else if (type == Cell.CELL_TYPE_ERROR) {
        output = new ExclyStringError();
    } else if (type == Cell.CELL_TYPE_FORMULA) {
        int formulaType = cell.getCachedFormulaResultType();
        output = readString(cell, formulaType);
    } else if (type == Cell.CELL_TYPE_BLANK) {
        output = new ExclyString("");
    } else if (type == Cell.CELL_TYPE_BOOLEAN) {
        Boolean data = cell.getBooleanCellValue();
        if (data) {
            output = new ExclyString("WAHR");
        } else {
            output = new ExclyString("FALSCH");
        }
    } else if (DateUtil.isCellDateFormatted(cell)) {
        Date data = cell.getDateCellValue();
        output = new ExclyString(data.toString());
    } else if (type == Cell.CELL_TYPE_NUMERIC) {
        double cellValue = cell.getNumericCellValue();
        String data = String.valueOf(cellValue);
        if (cellValue % 1 == 0 && data.endsWith(".0")) {
            data = data.substring(0, data.length() - 2);
        }
        output = new ExclyString(data);
    } else {
        log.warn("The reader was unable to find a valid parser for the cell [Row, Column] ("
                + cell.getRowIndex() + ", " + cell.getColumnIndex() + ")");
        output = new ExclyStringError();
    }

    return output;
}

From source file:at.mukprojects.exclycore.dao.XLSXReader.java

License:Open Source License

private ExclyDouble readDouble(Cell cell, int type) throws Exception {
    ExclyDouble output = null;//from  www.ja  v  a 2s  .com

    if (type == Cell.CELL_TYPE_STRING) {
        String data = cell.getStringCellValue();
        if (isNumericGerman(data)) {
            Number number = NumberFormat.getNumberInstance(Locale.GERMAN).parse(data);
            output = new ExclyDouble(number.doubleValue());
        } else if (isNumericUK(data)) {
            Number number = NumberFormat.getNumberInstance(Locale.UK).parse(data);
            output = new ExclyDouble(number.doubleValue());
        } else if (data.equals("") || data.equals(" ") || data.equals("-")) {
            output = new ExclyDoubleBlank();
        } else {
            output = new ExclyDoubleError();
            log.warn("The reader has expected a numeric value, but found a string value. [Row, Column] ("
                    + cell.getRowIndex() + ", " + cell.getColumnIndex() + ")");
        }
    } else if (type == Cell.CELL_TYPE_BLANK) {
        output = new ExclyDoubleBlank();
    } else if (type == Cell.CELL_TYPE_FORMULA) {
        int formulaType = cell.getCachedFormulaResultType();
        output = readDouble(cell, formulaType);
    } else if (type == Cell.CELL_TYPE_BOOLEAN) {
        Boolean data = cell.getBooleanCellValue();
        if (data) {
            output = new ExclyDouble(1);
        } else {
            output = new ExclyDouble(0);
        }
    } else if (type == Cell.CELL_TYPE_NUMERIC) {
        double data = cell.getNumericCellValue();
        output = new ExclyDouble(data);
    } else if (type == Cell.CELL_TYPE_ERROR) {
        output = new ExclyDoubleError();
    } else {
        log.warn("The reader was unable to find a valid parser for the cell [Row, Column] ("
                + cell.getRowIndex() + ", " + cell.getColumnIndex() + ")");
        output = new ExclyDoubleError();
    }

    return output;
}

From source file:at.mukprojects.exclycore.dao.XLSXReader.java

License:Open Source License

private ExclyInteger readInteger(Cell cell, int type) throws Exception {
    ExclyInteger output = null;/*from ww w.  j  a v  a2 s  . c  o  m*/

    if (type == Cell.CELL_TYPE_STRING) {
        String data = cell.getStringCellValue();
        if (isNumericGerman(data)) {
            Number number = NumberFormat.getNumberInstance(Locale.GERMAN).parse(data);
            output = new ExclyInteger(number.intValue());
        } else if (isNumericUK(data)) {
            Number number = NumberFormat.getNumberInstance(Locale.UK).parse(data);
            output = new ExclyInteger(number.intValue());
        } else if (data.equals("") || data.equals(" ") || data.trim().equals("-")) {
            output = new ExclyIntegerBlank();
        } else {
            output = new ExclyIntegerError();
            log.warn("The reader has expected a numeric value, but found a string value. [Row, Column] ("
                    + cell.getRowIndex() + ", " + cell.getColumnIndex() + ")");
        }
    } else if (type == Cell.CELL_TYPE_BLANK) {
        output = new ExclyIntegerBlank();
    } else if (type == Cell.CELL_TYPE_FORMULA) {
        int formulaType = cell.getCachedFormulaResultType();
        output = readInteger(cell, formulaType);
    } else if (type == Cell.CELL_TYPE_BOOLEAN) {
        Boolean data = cell.getBooleanCellValue();
        if (data) {
            output = new ExclyInteger(1);
        } else {
            output = new ExclyInteger(0);
        }
    } else if (type == Cell.CELL_TYPE_NUMERIC) {
        double data = cell.getNumericCellValue();
        output = new ExclyInteger(data);
    } else if (type == Cell.CELL_TYPE_ERROR) {
        output = new ExclyIntegerError();
    } else {
        log.warn("The reader was unable to find a valid parser for the cell [Row, Column] ("
                + cell.getRowIndex() + ", " + cell.getColumnIndex() + ")");
        output = new ExclyIntegerError();
    }

    return output;
}

From source file:at.mukprojects.exclycore.dao.XLSXReader.java

License:Open Source License

private ExclyLong readLong(Cell cell, int type) throws Exception {
    ExclyLong output = null;//from ww w.jav a  2  s  .  co m

    if (type == Cell.CELL_TYPE_STRING) {
        String data = cell.getStringCellValue();
        if (isNumericGerman(data)) {
            Number number = NumberFormat.getNumberInstance(Locale.GERMAN).parse(data);
            output = new ExclyLong(number.intValue());
        } else if (isNumericUK(data)) {
            Number number = NumberFormat.getNumberInstance(Locale.UK).parse(data);
            output = new ExclyLong(number.intValue());
        } else if (data.equals("") || data.equals(" ") || data.equals("-")) {
            output = new ExclyLongBlank();
        } else {
            output = new ExclyLongError();
            log.warn("The reader has expected a numeric value, but found a string value. [Row, Column] ("
                    + cell.getRowIndex() + ", " + cell.getColumnIndex() + ")");
        }
    } else if (type == Cell.CELL_TYPE_BLANK) {
        output = new ExclyLongBlank();
    } else if (type == Cell.CELL_TYPE_FORMULA) {
        int formulaType = cell.getCachedFormulaResultType();
        output = readLong(cell, formulaType);
    } else if (type == Cell.CELL_TYPE_BOOLEAN) {
        Boolean data = cell.getBooleanCellValue();
        if (data) {
            output = new ExclyLong(1);
        } else {
            output = new ExclyLong(0);
        }
    } else if (type == Cell.CELL_TYPE_NUMERIC) {
        double data = cell.getNumericCellValue();
        output = new ExclyLong(data);
    } else if (type == Cell.CELL_TYPE_ERROR) {
        output = new ExclyLongError();
    } else {
        log.warn("The reader was unable to find a valid parser for the cell [Row, Column] ("
                + cell.getRowIndex() + ", " + cell.getColumnIndex() + ")");
        output = new ExclyLongError();
    }

    return output;
}

From source file:attandance.standalone.utils.ExcelUtils.java

public static List<AttandanceRecord> readDataFromExcel(String fileName) {
    List<AttandanceRecord> result = new ArrayList<>();
    try {//from w  w w.  j  a v a 2  s.c om
        FileInputStream file = new FileInputStream(new File(fileName));
        //Get the workbook instance for XLS file 
        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 lineIndex = 1;
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            //For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();
            int columnIndex = 1;
            AttandanceRecord lineBean = new AttandanceRecord();
            //skip header
            if (lineIndex == 1) {
                lineIndex += 1;
                continue;
            }
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                String cellValue = "";
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    cellValue = "" + cell.getBooleanCellValue();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    cellValue = "" + cell.getNumericCellValue();
                    break;
                case Cell.CELL_TYPE_STRING:
                    cellValue = cell.getStringCellValue();
                    break;
                default:
                    cellValue = cell.getStringCellValue();
                }
                lineBean.setAttribute(columnIndex, cellValue);
                columnIndex = columnIndex + 1;
            }
            result.add(lineBean);
        }
        file.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}