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:edu.vt.cs.irwin.etdscraper.retriever.excel.ExcelEtdSource.java

License:Apache License

private String getCellValue(Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue().getString();
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue().toString();
        } else {/*from w ww .j a v  a2  s .c  o  m*/
            return ((Double) cell.getNumericCellValue()).toString();
        }
    case Cell.CELL_TYPE_BOOLEAN:
        return ((Boolean) cell.getBooleanCellValue()).toString();
    case Cell.CELL_TYPE_BLANK:
        return "";
    }
    throw new UnsupportedOperationException("Don't know how to work with type: " + cell.getCellType());
}

From source file:egovframework.rte.fdl.excel.util.EgovExcelUtil.java

License:Apache License

/**
 * ? ? String   .//from w ww . j  a  va  2s.  c o m
 * 
 * @param cell <code>Cell</code>
 * @return  
 */
public static String getValue(Cell cell) {

    String result = "";

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

    if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        LOGGER.debug("### Cell.CELL_TYPE_BOOLEAN : {}", Cell.CELL_TYPE_BOOLEAN);
        result = String.valueOf(cell.getBooleanCellValue());

    } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
        LOGGER.debug("### Cell.CELL_TYPE_ERROR : {}", Cell.CELL_TYPE_ERROR);
        // byte errorValue =
        // cell.getErrorCellValue();

    } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        LOGGER.debug("### Cell.CELL_TYPE_FORMULA : {}", Cell.CELL_TYPE_FORMULA);

        String stringValue = null;
        String longValue = null;

        try {
            stringValue = cell.getRichStringCellValue().getString();
            longValue = doubleToString(cell.getNumericCellValue());
        } catch (Exception e) {
            LOGGER.debug("{}", e);
        }

        if (stringValue != null) {
            result = stringValue;
        } else if (longValue != null) {
            result = longValue;
        } else {
            result = cell.getCellFormula();
        }

    } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        LOGGER.debug("### Cell.CELL_TYPE_NUMERIC : {}", Cell.CELL_TYPE_NUMERIC);

        result = DateUtil.isCellDateFormatted(cell)
                ? EgovDateUtil.toString(cell.getDateCellValue(), "yyyy/MM/dd", null)
                : doubleToString(cell.getNumericCellValue());

    } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        LOGGER.debug("### Cell.CELL_TYPE_STRING : {}", Cell.CELL_TYPE_STRING);
        result = cell.getRichStringCellValue().getString();

    } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
        LOGGER.debug("### Cell.CELL_TYPE_BLANK : {}", Cell.CELL_TYPE_BLANK);
    }

    return result;
}

From source file:eionet.gdem.conversion.excel.reader.ExcelReader.java

License:Mozilla Public License

/**
 * Reads cell value and formats it according to element type defined in XML Schema. If the cell contains formula,
 * then calculated value is returned.//from  www. jav  a 2  s .c  o  m
 *
 * @param cell       Spreadsheet Cell object.
 * @param schemaType XML Schema data type for given cell.
 * @return string value of the cell.
 */
protected String cellValueToString(Cell cell, String schemaType) {
    String value = "";

    if (cell != null) {
        switch (evaluator.evaluateInCell(cell).getCellType()) {
        case HSSFCell.CELL_TYPE_NUMERIC:
            if (HSSFDateUtil.isCellDateFormatted(cell) && !isYearValue(cell.getNumericCellValue())) {
                Date dateValue = cell.getDateCellValue();
                value = Utils.getFormat(dateValue, DEFAULT_DATE_FORMAT);
            } else if (HSSFDateUtil.isValidExcelDate(cell.getNumericCellValue()) && schemaType != null
                    && schemaType.equals("xs:date") && !isYearValue(cell.getNumericCellValue())) {
                Date dateValue = cell.getDateCellValue();
                value = Utils.getFormat(dateValue, DEFAULT_DATE_FORMAT);
            } else {
                value = formatter.formatCellValue(cell);
            }
            break;
        case HSSFCell.CELL_TYPE_STRING:
            RichTextString richText = cell.getRichStringCellValue();
            value = richText.toString();
            break;
        case HSSFCell.CELL_TYPE_BOOLEAN:
            value = Boolean.toString(cell.getBooleanCellValue());
            break;
        case HSSFCell.CELL_TYPE_ERROR:
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            break;
        default:
            break;
        }
    }
    return StringUtils.strip(value.trim(), String.valueOf(NON_BREAKING_SPACE)).trim();
}

From source file:eu.learnpad.ontology.kpi.data.ExcelParser.java

public List<List<String>> getDataTable() throws IOException, InvalidFormatException {
    List<List<String>> dataTable = new ArrayList<>();
    Integer rowNumber = -2;// w  w  w .jav  a 2s  .  c om

    Workbook wb = WorkbookFactory.create(excelFile);

    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
    for (Sheet sheet : wb) {
        if (sheet.getSheetName().equals(SHEETNAME)) {
            for (Row row : sheet) {
                //stop with the first empty row
                if (row.getCell(0) == null) {
                    break;
                }
                if (rowNumber >= -1) {
                    rowNumber++;
                    dataTable.add(new ArrayList<String>());
                }
                for (Cell cell : row) {
                    String sheetName = sheet.getSheetName();
                    String cellRow = "Row:" + cell.getRowIndex();
                    String cellColumn = "Column:" + cell.getColumnIndex();
                    Object[] o = new Object[] { sheetName, cellRow, cellColumn };
                    LOGGER.log(Level.INFO, "Processing: Sheet={0} celladress={1}", o);
                    if (rowNumber <= -1 && cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                        continue;
                    }
                    if (rowNumber == -2 && cell.getCellType() == Cell.CELL_TYPE_STRING) {
                        if (cell.getRichStringCellValue().getString().equals(DATACELLNAME)) {
                            rowNumber = -1;
                            continue;
                        }
                    }
                    //Attributes (column headers)
                    if (rowNumber == 0) {
                        dataTable.get(rowNumber).add(cell.getRichStringCellValue().getString());
                    }

                    if (rowNumber >= 1) {

                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            dataTable.get(rowNumber).add(cell.getRichStringCellValue().getString());
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                                dataTable.get(rowNumber).add(cell.getDateCellValue().toString());
                            } else {
                                dataTable.get(rowNumber).add(Double.toString(cell.getNumericCellValue()));
                            }
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            dataTable.get(rowNumber).add(Boolean.toString(cell.getBooleanCellValue()));
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            switch (cell.getCachedFormulaResultType()) {
                            case Cell.CELL_TYPE_STRING:
                                dataTable.get(rowNumber).add(cell.getRichStringCellValue().getString());
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                if (DateUtil.isCellDateFormatted(cell)) {
                                    dataTable.get(rowNumber).add(cell.getDateCellValue().toString());
                                } else {
                                    dataTable.get(rowNumber).add(Double.toString(cell.getNumericCellValue()));
                                }
                                break;
                            case Cell.CELL_TYPE_BOOLEAN:
                                dataTable.get(rowNumber).add(Boolean.toString(cell.getBooleanCellValue()));
                                break;
                            default:
                                dataTable.get(rowNumber).add("");
                            }
                            break;
                        default:
                            dataTable.get(rowNumber).add("");
                        }
                    }
                }
            }
        }
    }

    return dataTable;
}

From source file:excel.Excel.java

/**
 * @param args the command line arguments
 * @throws java.io.IOException/*from w w w  .  j a va2  s  .  c  o  m*/
 */
public static void main(String args[]) throws IOException {
    Thread a;

    //        String nameFile = "C:\\Users\\dfcastellanosc.SOPORTECOS\\Downloads\\Files\\Informacin Etapa Productiva.xlsx";

    //        Process p = Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + nameFile);

    FileInputStream file = new FileInputStream(
            new File("C:\\Users\\dfcastellanosc.SOPORTECOS\\Documents\\registroempleados.xlsx"));

    try (XSSFWorkbook workbook = new XSSFWorkbook(file)) {
        XSSFSheet sheet = workbook.getSheetAt(0);

        Iterator<Row> rowIterator = sheet.iterator();

        Row row;

        while (rowIterator.hasNext()) {

            row = rowIterator.next();

            Iterator<Cell> cellIterator = row.cellIterator();

            Cell celda;

            while (cellIterator.hasNext()) {

                celda = cellIterator.next();

                switch (celda.getCellType()) {

                case Cell.CELL_TYPE_NUMERIC:

                    if (DateUtil.isCellDateFormatted(celda)) {

                        if (celda.getColumnIndex() == 17) {
                            System.out.println("|" + celda.getDateCellValue() + "|");
                        } else {
                            System.out.print("|" + celda.getDateCellValue() + "|");
                        }

                    } else {

                        Double ds = celda.getNumericCellValue();
                        Long pt = ds.longValue();

                        if (celda.getColumnIndex() == 17) {
                            System.out.println("|" + pt + "|");
                        } else {
                            System.out.print("|" + pt + "|");
                        }
                    }
                    break;

                case Cell.CELL_TYPE_STRING:
                    if (celda.getColumnIndex() == 17) {
                        System.out.println("|" + celda.getStringCellValue() + "|");
                    } else {
                        System.out.print("|" + celda.getStringCellValue() + "|");
                    }

                    break;

                case Cell.CELL_TYPE_BOOLEAN:

                    if (celda.getColumnIndex() == 17) {
                        System.out.println("|" + celda.getBooleanCellValue() + "|");
                    } else {
                        System.out.print("|" + celda.getBooleanCellValue() + "|");
                    }

                    break;

                }

            }

        }
        workbook.close();
    }

}

From source file:excel.Reader.java

public void print() {
    System.out.println("START PRINT");
    SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
    int columnWidth = 15;
    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
    Sheet sheet = wb.getSheetAt(0);//  w ww  . j ava 2  s  .  c om
    for (Row row : sheet) {
        //System.out.print("r");
        for (Cell cell : row) {
            //CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
            //System.out.print(cellRef.formatAsString());
            //System.out.print(" - ");
            // System.out.print("c");
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                //System.out.print("s");
                System.out.printf("%-" + columnWidth + "s", cell.getRichStringCellValue().getString());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                //System.out.print("d");
                if (DateUtil.isCellDateFormatted(cell)) {
                    System.out.printf("%-" + columnWidth + "s", df.format(cell.getDateCellValue()));
                } else {
                    if ((cell.getNumericCellValue() % 1.0) != 0.0)
                        System.out.printf("%-" + columnWidth + ".2f", cell.getNumericCellValue());
                    else
                        System.out.printf("%-" + columnWidth + ".0f", cell.getNumericCellValue());
                }
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                //System.out.print("b");
                System.out.printf("%-" + columnWidth + "s", cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                CellValue val = evaluator.evaluate(cell);
                //System.out.print("f");
                switch (val.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.printf("%-" + columnWidth + "s", val.getStringValue());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.printf("%-" + columnWidth + ".2f", val.getNumberValue());
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.printf("%-" + columnWidth + "s", val.getBooleanValue());
                    break;
                default:
                    System.out.printf("%-" + columnWidth + "s", "");
                }
                break;
            default:
                System.out.print("");
            }
        }
        System.out.println();
    }
}

From source file:ExcelReadFile.ExcellReadSpecialite.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 (int) Cell.CELL_TYPE_NUMERIC:
        return (int) Math.round(cell.getNumericCellValue());
    }/*from w  w w  . ja v a 2 s  . c  o m*/

    return null;
}

From source file:ExcelReadFile.ExcelReadMedecin.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 (int) Math.round(cell.getNumericCellValue());
    }/*from w  w  w  .  jav a  2  s .  c  o m*/

    return null;
}

From source file:FilesHandlers.ExcelHandler.java

/**
 * used for getting the content of the selected file
 *
 * @param file The name of the file to display
 * @param sheet The sheet number//from  w  w  w.  jav a 2 s  . co  m
 * @return The content of given sheet
 * @throws java.io.FileNotFoundException
 */
public ArrayList<String[]> getFileCtBySheet(String file, int sheet) throws FileNotFoundException, IOException {
    ArrayList<String[]> list = new ArrayList<String[]>();
    ArrayList<String> row = new ArrayList<>();
    File selectedFile = new File(this.workingDirectory.concat(file));
    FileInputStream inputStream = new FileInputStream(selectedFile);

    XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
    Sheet firstSheet = workbook.getSheetAt(sheet);
    Iterator<Row> iterator = firstSheet.iterator();

    while (iterator.hasNext()) {
        Row nextRow = iterator.next();
        Iterator<Cell> cellIterator = nextRow.cellIterator();

        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();

            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                row.add((String) cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                row.add("" + cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                row.add("" + cell.getNumericCellValue());
                break;
            }

        }
        list.add(row.toArray(new String[list.size()]));
        row = new ArrayList<>();
    }

    workbook.close();
    inputStream.close();

    return list;
}

From source file:gob.dp.sid.registro.controller.ImportarController.java

private void importar(File archivo) {
    List<Object[]> listaObjetos = new ArrayList<>();
    try {/*from   w  ww.j av a  2 s .  c om*/
        wb = WorkbookFactory.create(new FileInputStream(archivo));
        Sheet hoja = wb.getSheetAt(0);
        Iterator filaIterator = hoja.rowIterator();
        int indiceFila = -1;
        while (filaIterator.hasNext()) {
            indiceFila++;
            Row fila = (Row) filaIterator.next();
            Iterator columnaIterator = fila.cellIterator();
            Object[] listaColumna = new Object[7];

            int indiceColumna = -1;
            while (columnaIterator.hasNext()) {
                indiceColumna++;
                Cell celda = (Cell) columnaIterator.next();
                if (indiceFila == 0) {

                } else {
                    if (celda != null && indiceColumna < 7) {
                        switch (celda.getCellType()) {
                        case Cell.CELL_TYPE_NUMERIC:
                            //listaColumna[indiceColumna]= (int)Math.round(celda.getNumericCellValue());
                            listaColumna[indiceColumna] = celda.getDateCellValue();
                            break;
                        case Cell.CELL_TYPE_STRING:
                            listaColumna[indiceColumna] = celda.getStringCellValue();
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            listaColumna[indiceColumna] = celda.getBooleanCellValue();
                            break;
                        default:
                            listaColumna[indiceColumna] = null;
                            break;
                        }
                    }
                }
            }
            if (indiceFila != 0) {
                listaObjetos.add(listaColumna);
            }

        }
        cargarGestiones(listaObjetos);
    } catch (IOException | InvalidFormatException | EncryptedDocumentException e) {
        log.error("importar" + e);
    }
}