Example usage for org.apache.poi.xssf.usermodel XSSFCell getBooleanCellValue

List of usage examples for org.apache.poi.xssf.usermodel XSSFCell getBooleanCellValue

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFCell getBooleanCellValue.

Prototype

@Override
public boolean getBooleanCellValue() 

Source Link

Document

Get the value of the cell as a boolean.

Usage

From source file:com.yanglb.utilitys.codegen.core.reader.BaseReader.java

License:Apache License

/**
 * ?Cell???/*from  w w  w . java2s .  c om*/
 * @param cell
 * @return
 */
public String getCellStringValue(XSSFCell cell) {
    String result = null;
    int type = cell.getCellType();
    if (type == Cell.CELL_TYPE_FORMULA)
        type = cell.getCachedFormulaResultType();
    if (type == Cell.CELL_TYPE_BLANK)
        return null;
    if (type == Cell.CELL_TYPE_ERROR) {
        return "#VALUE!";
    }

    switch (type) {
    case Cell.CELL_TYPE_BOOLEAN:
        result = String.valueOf(cell.getBooleanCellValue());
        break;

    case Cell.CELL_TYPE_STRING:
        result = cell.getStringCellValue();
        break;

    case Cell.CELL_TYPE_NUMERIC: {
        if (cell.getCellStyle().getDataFormat() == DataFormatType.FORMAT_DATE) {
            Date date = cell.getDateCellValue();
            String format = "yyyy/MM/dd";//cell.getCellStyle().getDataFormatString();
            if (cell.getCellStyle().getDataFormatString().contains(":")) {
                // 
                format = "yyyy/MM/dd HH:mm:ss";
            }
            SimpleDateFormat df = null;
            try {
                df = new SimpleDateFormat(format);
            } catch (IllegalArgumentException e) {
                //df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
            }
            result = df.format(date);
        } else {
            // ?? 0
            Number number = cell.getNumericCellValue();
            result = number.toString();
            if (result.indexOf('.') != -1) {
                result = result.replaceAll("[0]*$", "");
            }
            if (result.endsWith(".")) {
                result = result.substring(0, result.length() - 1);
            }
        }
    }
        break;
    }
    return result;
}

From source file:Creator.TaskManagerPanel.java

private void loadDefaultTasks() {

    String path = "/Creator/textFiles/tasks.xlsx";
    InputStream loc = this.getClass().getResourceAsStream(path);
    importedTasks = new ArrayList<>();
    try {//from ww  w .j av a 2s. c o  m

        XSSFWorkbook wb = new XSSFWorkbook(loc);
        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow row;
        XSSFCell cell;
        String[] rowData;
        int rows, cols; // No of rows
        rows = sheet.getPhysicalNumberOfRows();

        for (int i = 1; i < rows; i++) {

            row = sheet.getRow(i);
            if (row != null) {
                cols = row.getPhysicalNumberOfCells();
                rowData = new String[cols];

                for (int j = 0; j < cols; j++) {

                    cell = row.getCell(j);
                    if (cell != null) {
                        switch (cell.getCellType()) {
                        case 1: // string
                            rowData[j] = cell.getStringCellValue();
                            break;
                        case 2: // int
                            rowData[j] = String.valueOf(cell.getNumericCellValue());
                            break;
                        case 3: // blank
                            System.out.println("Blank data @ [" + i + "][" + j + "]");
                            rowData[j] = "no data @ [" + i + "][" + j + "]";
                            break;
                        case 4: // boolean
                            rowData[j] = String.valueOf(cell.getBooleanCellValue());
                            break;
                        case 5: // error
                            rowData[j] = String.valueOf(cell.getErrorCellString());
                            break;
                        default:
                            System.out.println("default @ [" + i + "][" + j + "]");
                            rowData[j] = "default @ [" + i + "][" + j + "]";
                            break;

                        }

                    } else {
                        System.out.println("null @ [" + i + "][" + j + "]");
                        rowData[j] = "nullValue @ [" + i + "][" + j + "]";
                    }
                }
                rowData[5] = "'" + rowData[5] + "'";
                importedTasks.add(rowData);

            }

        }

        wb.close();

    } catch (Exception e) {
        System.out.println("Error reading excel file " + e.getMessage());
    }

}

From source file:Creator.TaskManagerPanel.java

private void loadDefaultAlerts() {

    String path = "/Creator/textFiles/alerts.xlsx";
    InputStream loc = this.getClass().getResourceAsStream(path);
    importedAlerts = new ArrayList<>();
    try {//from w w w  .  j a  v  a2s  .  c o m

        XSSFWorkbook wb = new XSSFWorkbook(loc);
        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow row;
        XSSFCell cell;
        String[] rowData;
        int rows, cols; // No of rows
        rows = sheet.getPhysicalNumberOfRows();

        for (int i = 1; i < rows; i++) {

            row = sheet.getRow(i);
            if (row != null) {
                cols = row.getPhysicalNumberOfCells();
                rowData = new String[cols];

                for (int j = 0; j < cols; j++) {

                    cell = row.getCell(j);
                    if (cell != null) {
                        switch (cell.getCellType()) {
                        case 1: // string
                            rowData[j] = cell.getStringCellValue();
                            break;
                        case 2: // int
                            rowData[j] = String.valueOf(cell.getNumericCellValue());
                            break;
                        case 3: // blank
                            System.out.println("Blank data @ [" + i + "][" + j + "]");
                            rowData[j] = "no data @ [" + i + "][" + j + "]";
                            break;
                        case 4: // boolean
                            rowData[j] = String.valueOf(cell.getBooleanCellValue());
                            break;
                        case 5: // error
                            rowData[j] = String.valueOf(cell.getErrorCellString());
                            break;
                        default:
                            //System.out.println("default @ [" + i + "][" + j + "] = " + String.valueOf(cell.getRawValue()));
                            rowData[j] = String.valueOf(cell.getRawValue());
                            break;
                        }

                    } else {
                        System.out.println("null @ [" + i + "][" + j + "]");
                        rowData[j] = "nullValue @ [" + i + "][" + j + "]";
                    }
                }
                importedAlerts.add(rowData);

            }
        }

        wb.close();

    } catch (Exception e) {
        System.out.println("Error reading excel file " + e.getMessage());
    }

}

From source file:de.escnet.ExcelTable.java

License:Open Source License

private String getCellValue(XSSFCell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        return "";

    case Cell.CELL_TYPE_BOOLEAN:
        return Boolean.toString(cell.getBooleanCellValue());

    case Cell.CELL_TYPE_ERROR:
        return cell.getErrorCellString();

    case Cell.CELL_TYPE_FORMULA:
        CellValue cellValue = evaluator.evaluate(cell);
        switch (cellValue.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            return "";

        case Cell.CELL_TYPE_BOOLEAN:
            return Boolean.toString(cellValue.getBooleanValue());

        case Cell.CELL_TYPE_NUMERIC:
            return getNumericValue(cellValue.getNumberValue());

        case Cell.CELL_TYPE_STRING:
            return cellValue.getStringValue();

        case Cell.CELL_TYPE_ERROR:
        case Cell.CELL_TYPE_FORMULA:
        default:/*from w  w w  .  j  a  v a2 s .c om*/
            throw new IllegalStateException("Illegal cell type: " + cell.getCellType());
        }

    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue().toString();
        }

        return getNumericValue(cell.getNumericCellValue());

    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();

    default:
        throw new IllegalStateException("Illegal cell type: " + cell.getCellType());
    }
}

From source file:mx.infotec.dads.arq.excel.ImplementExcel.java

public String getStringCellValue(XSSFCell cell) {
    String value;//from w  w w  .java 2  s  . com
    switch (cell.getCellType()) {
    case XSSFCell.CELL_TYPE_STRING:
        value = cell.getStringCellValue() + " ";
        break;
    case XSSFCell.CELL_TYPE_NUMERIC:
        Double n = cell.getNumericCellValue();
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
            value = cell.getDateCellValue().toString();
        } else {
            value = n + " ";
        }
        break;
    case XSSFCell.CELL_TYPE_BOOLEAN:
        value = cell.getBooleanCellValue() + " ";
        break;
    case XSSFCell.CELL_TYPE_FORMULA:
        value = cell.getCellFormula();
        break;
    case XSSFCell.CELL_TYPE_ERROR:
        value = cell.getErrorCellString();
        break;
    default:
        value = "error";
        break;
    }

    return value;
}

From source file:mx.infotec.dads.arq.excel.ImplementExcel.java

public Object getCellValue(XSSFCell cell) {
    Object value;/* w w w .ja  va  2  s .c  o  m*/
    switch (cell.getCellType()) {
    case XSSFCell.CELL_TYPE_STRING:
        value = cell.getStringCellValue();
        break;
    case XSSFCell.CELL_TYPE_NUMERIC:
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
            value = cell.getDateCellValue();
        } else {
            value = cell.getNumericCellValue();
        }
        break;
    case XSSFCell.CELL_TYPE_BOOLEAN:
        value = cell.getBooleanCellValue();
        break;
    case XSSFCell.CELL_TYPE_FORMULA:
        value = cell.getCellFormula();
        break;
    case XSSFCell.CELL_TYPE_ERROR:
        value = cell.getErrorCellString();
        break;
    default:
        value = "error";
        break;
    }

    return value;
}

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

License:Open Source License

public static void copyXSSFCell(XSSFCell srcCell, XSSFCell destCell) {
    if (srcCell != null && destCell != null) {
        switch (srcCell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            destCell.setCellType(Cell.CELL_TYPE_STRING);
            destCell.setCellValue(srcCell.getRichStringCellValue());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            destCell.setCellType(Cell.CELL_TYPE_NUMERIC);
            if (DateUtil.isCellDateFormatted(srcCell)) {
                destCell.setCellValue(srcCell.getDateCellValue());
            } else {
                destCell.setCellValue(srcCell.getNumericCellValue());
            }/*w  w  w.ja  va 2 s  .  co  m*/
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            destCell.setCellType(Cell.CELL_TYPE_BOOLEAN);
            destCell.setCellValue(srcCell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            destCell.setCellType(Cell.CELL_TYPE_FORMULA);
            destCell.setCellValue(srcCell.getCellFormula());
            break;
        }
        copyXSSFCellStyle(srcCell, destCell);
    } else {
        throw new IllegalArgumentException("Cannot copy from / to null XSSFCell!");
    }
}

From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.jobs.LoadXlsxExcelJob.java

License:Open Source License

private void loadExcel(final String file) {

    final File fil = new File(file);
    if (fil.exists()) {
        canRead = true;/*  w  w  w.j ava2s. c  o m*/
        if (grid != null) {
            try {
                InputStream inp = new FileInputStream(file);
                try {
                    wb = new XSSFWorkbook(inp);
                } catch (Exception e) {
                    MsgDialog.message("Wrong format!\nOnly Excel *.xlsx (2007-2010) is supported!");
                    canRead = false;
                    e.printStackTrace();
                }
                // wb = new HSSFWorkbook(inp);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            if (canRead) {
                for (s = 0; s < wb.getNumberOfSheets(); s++) {
                    Display display = PlatformUI.getWorkbench().getDisplay();
                    display.syncExec(new Runnable() {

                        public void run() {

                            String name = fil.getName();
                            grid = new Spread().spread(SampleView.getTabFolder(), 0, 0, name);
                            SampleView.setGrid(grid);
                            XSSFSheet sheet = wb.getSheetAt(s);
                            int colCount = grid.getColumnCount();
                            int rowCount = grid.getItemCount();
                            int exelRow = endOfRow(sheet);
                            int exelColumn = endOfColumn(sheet);
                            // System.out.println(exelRow + " " + exelColumn
                            // + "---" + sheet.getPhysicalNumberOfRows() +
                            // " " +
                            // sheet.getRow(0).getPhysicalNumberOfCells());
                            if (colCount < exelColumn) {
                                int diff = exelColumn - colCount;
                                for (int i = 0; i < diff; i++) {
                                    GridColumn column = new GridColumn(grid, SWT.NONE);
                                    column.setText("C " + (i + 1 + colCount));
                                    column.setWidth(50);
                                }
                            }
                            if (rowCount < exelRow) {
                                int diff = exelRow - rowCount;
                                for (int i = 0; i < diff; i++) {
                                    new GridItem(grid, SWT.NONE).setHeight(16);
                                }
                            }
                            // Iterate over each row in the sheet
                            int rows = sheet.getPhysicalNumberOfRows();
                            for (int i = 0; i < exelRow; i++) {
                                XSSFRow row = sheet.getRow(i);
                                if (row == null) {
                                    for (int u = 0; u < exelColumn; u++) {
                                        grid.getItem(i).setText(u, " ");
                                    }
                                } else {
                                    for (int u = 0; u < exelColumn; u++) {
                                        XSSFCell cell = row.getCell(u);
                                        if (cell != null) {
                                            switch (cell.getCellType()) {
                                            case XSSFCell.CELL_TYPE_NUMERIC:
                                                String val = String.valueOf(cell.getNumericCellValue());
                                                grid.getItem(i).setText(u, val);
                                                break;
                                            case XSSFCell.CELL_TYPE_STRING:
                                                XSSFRichTextString st = cell.getRichStringCellValue();
                                                String val2 = st.getString();
                                                grid.getItem(i).setText(u, val2);
                                                break;
                                            case XSSFCell.CELL_TYPE_FORMULA:
                                                try {
                                                    String val3 = String.valueOf(cell.getRawValue());
                                                    grid.getItem(i).setText(u, val3);
                                                } catch (Exception e) {
                                                    // System.out.println(e.getMessage());
                                                    String s2 = cell.getCellFormula();
                                                    grid.getItem(i).setText(u, s2);
                                                }
                                                break;
                                            case XSSFCell.CELL_TYPE_BLANK:
                                                grid.getItem(i).setText(u, " ");
                                                break;
                                            case XSSFCell.CELL_TYPE_BOOLEAN:
                                                boolean s4 = cell.getBooleanCellValue();
                                                if (s4) {
                                                    grid.getItem(i).setText(u, "TRUE");
                                                } else {
                                                    grid.getItem(i).setText(u, "FALSE");
                                                }
                                                break;
                                            default:
                                                break;
                                            }
                                        } else {
                                            grid.getItem(i).setText(u, " ");
                                        }
                                    }
                                }
                            }
                        }
                    });
                }
                wb = null;
            }
        }
    } else {
        MsgDialog.message("File not found!");
    }
}

From source file:org.apache.ofbiz.pricat.AbstractPricatParser.java

License:Apache License

private void copyRow(XSSFRow sourceRow, XSSFRow targetRow, XSSFCreationHelper factory, XSSFDrawing patriarch) {
    for (int j = 0; j < sourceRow.getPhysicalNumberOfCells(); j++) {
        XSSFCell cell = sourceRow.getCell(j);
        if (cell != null) {
            XSSFCell newCell = targetRow.createCell(j);
            int cellType = cell.getCellType();
            newCell.setCellType(cellType);
            switch (cellType) {
            case XSSFCell.CELL_TYPE_BOOLEAN:
                newCell.setCellValue(cell.getBooleanCellValue());
                break;
            case XSSFCell.CELL_TYPE_ERROR:
                newCell.setCellErrorValue(cell.getErrorCellValue());
                break;
            case XSSFCell.CELL_TYPE_FORMULA:
                newCell.setCellFormula(cell.getCellFormula());
                break;
            case XSSFCell.CELL_TYPE_NUMERIC:
                newCell.setCellValue(cell.getNumericCellValue());
                break;
            case XSSFCell.CELL_TYPE_STRING:
                newCell.setCellValue(cell.getRichStringCellValue());
                break;
            default:
                newCell.setCellValue(formatter.formatCellValue(cell));
            }/* ww  w  .  ja  v  a  2 s  .  co  m*/
            if (cell.getCellComment() != null) {
                XSSFClientAnchor anchor = factory.createClientAnchor();
                anchor.setDx1(100);
                anchor.setDx2(100);
                anchor.setDy1(100);
                anchor.setDy2(100);
                anchor.setCol1(newCell.getColumnIndex());
                anchor.setCol2(newCell.getColumnIndex() + 4);
                anchor.setRow1(newCell.getRowIndex());
                anchor.setRow2(newCell.getRowIndex() + 4);
                anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);

                XSSFComment comment = patriarch.createCellComment(anchor);
                comment.setString(cell.getCellComment().getString());
                newCell.setCellComment(comment);
            }
            newCell.setCellStyle(cell.getCellStyle());
            newCell.getSheet().setColumnWidth(newCell.getColumnIndex(),
                    cell.getSheet().getColumnWidth(cell.getColumnIndex()));
        }
    }
}

From source file:org.exoplatform.services.document.impl.MSXExcelDocumentReader.java

License:Open Source License

/**
 * Returns only a text from .xlsx file content.
 * /*from w w  w .  jav a 2s. c om*/
 * @param is an input stream with .xls file content.
 * @return The string only with text from file content.
 */
public String getContentAsText(final InputStream is) throws IOException, DocumentReadException {
    if (is == null) {
        throw new IllegalArgumentException("InputStream is null.");
    }

    StringBuilder builder = new StringBuilder("");
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

    try {
        if (is.available() == 0) {
            return "";
        }

        XSSFWorkbook wb;
        try {
            wb = SecurityHelper.doPrivilegedIOExceptionAction(new PrivilegedExceptionAction<XSSFWorkbook>() {
                public XSSFWorkbook run() throws Exception {
                    return new XSSFWorkbook(is);
                }
            });
        } catch (IOException e) {
            throw new DocumentReadException("Can't open spreadsheet.", e);
        } catch (OpenXML4JRuntimeException e) {
            return builder.toString();
        }
        for (int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
            XSSFSheet sheet = wb.getSheetAt(sheetNum);
            if (sheet != null) {
                for (int rowNum = sheet.getFirstRowNum(); rowNum <= sheet.getLastRowNum(); rowNum++) {
                    XSSFRow row = sheet.getRow(rowNum);

                    if (row != null) {
                        int lastcell = row.getLastCellNum();
                        for (int k = 0; k < lastcell; k++) {
                            XSSFCell cell = row.getCell(k);
                            if (cell != null) {
                                switch (cell.getCellType()) {
                                case XSSFCell.CELL_TYPE_NUMERIC: {
                                    double d = cell.getNumericCellValue();
                                    if (isCellDateFormatted(cell)) {
                                        Date date = HSSFDateUtil.getJavaDate(d);
                                        String cellText = dateFormat.format(date);
                                        builder.append(cellText).append(" ");
                                    } else {
                                        builder.append(d).append(" ");
                                    }
                                    break;
                                }
                                case XSSFCell.CELL_TYPE_FORMULA:
                                    builder.append(cell.getCellFormula().toString()).append(" ");
                                    break;
                                case XSSFCell.CELL_TYPE_BOOLEAN:
                                    builder.append(cell.getBooleanCellValue()).append(" ");
                                    break;
                                case XSSFCell.CELL_TYPE_ERROR:
                                    builder.append(cell.getErrorCellValue()).append(" ");
                                    break;
                                case XSSFCell.CELL_TYPE_STRING:
                                    builder.append(cell.getStringCellValue().toString()).append(" ");
                                    break;
                                default:
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("An exception occurred: " + e.getMessage());
                }
            }
        }
    }
    return builder.toString();
}