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:org.wso2.carbon.dataservices.sql.driver.processor.reader.ExcelDataReader.java

License:Open Source License

/**
 * Extracts the value of a particular cell depending on its type
 *
 * @param cell A populated Cell instance
 * @return Value of the cell/*from  ww w  .ja  va2 s  .c om*/
 */
private Object extractCellValue(Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:
        return cell.getNumericCellValue();
    case Cell.CELL_TYPE_BLANK:
    case Cell.CELL_TYPE_FORMULA:
    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case Cell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue();
    default:
        return cell.getStringCellValue();
    }
}

From source file:org.zafritech.zidingorms.io.excel.ExcelFunctions.java

@SuppressWarnings("deprecation")
public Object getExcelCellValue(Cell cell) {

    switch (cell.getCellTypeEnum()) {

    case STRING://from  w w w . j  a v a  2s  .  c  o m
        return cell.getStringCellValue();

    case BOOLEAN:
        return cell.getBooleanCellValue();

    case NUMERIC:
        return cell.getNumericCellValue();

    default:
        return null;
    }
}

From source file:plugins.excel.client.util.ExcelReader.java

License:Microsoft Reference Source License

private Object getCellValue(Cell c, int targetType) {
    int cellType = c.getCellType();
    Object val;

    try {/* w  ww  .ja v  a  2s .c o  m*/
        switch (cellType) {
        case (Cell.CELL_TYPE_STRING):
        case (Cell.CELL_TYPE_FORMULA):
            val = c.getStringCellValue();

            switch (targetType) {
            case Types.BOOLEAN:
                return Boolean.parseBoolean((String) val);
            case Types.DOUBLE:
                return Double.parseDouble((String) val);
            case Types.INTEGER:
                return Integer.parseInt((String) val);
            case Types.VARCHAR:
                return (String) val;
            case Types.DATE:
                SimpleDateFormat sdf = new SimpleDateFormat();
                try {
                    return sdf.parse((String) val);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            break;
        case (Cell.CELL_TYPE_NUMERIC):
            if (DateUtil.isCellDateFormatted(c)) {
                val = c.getDateCellValue();

                switch (targetType) {
                case Types.BOOLEAN:
                    return (((Date) val).getTime() > 0);
                case Types.DOUBLE:
                    return (double) ((Date) val).getTime();
                case Types.INTEGER:
                    return ((Date) val).getTime();
                case Types.VARCHAR:
                    DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
                    return df.format((Date) val);
                case Types.DATE:
                    return (Date) val;
                }
            } else {
                val = c.getNumericCellValue();

                switch (targetType) {
                case Types.BOOLEAN:
                    return ((double) val > 0.0);
                case Types.DOUBLE:
                    return (double) val;
                case Types.INTEGER:
                    return (long) val;
                case Types.VARCHAR:
                    return new Double((double) val).toString();
                case Types.DATE:
                    Date d = new Date();
                    d.setTime((long) val);
                    return d;
                }
            }
            break;
        case (Cell.CELL_TYPE_ERROR):
            val = c.getErrorCellValue();

            switch (targetType) {
            case Types.BOOLEAN:
                return ((int) val > 0);
            case Types.DOUBLE:
                return (double) val;
            case Types.INTEGER:
                return (int) val;
            case Types.VARCHAR:
                return new Integer((int) val).toString();
            case Types.DATE:
                Date d = new Date();
                d.setTime((long) val);
                return d;
            }
            break;
        case (Cell.CELL_TYPE_BOOLEAN):
            val = c.getBooleanCellValue();

            switch (targetType) {
            case Types.BOOLEAN:
                return (boolean) val;
            case Types.DOUBLE:
                return (double) (((boolean) val ? 1 : 0));
            case Types.INTEGER:
                return (int) (((boolean) val ? 1 : 0));
            case Types.VARCHAR:
                return new Boolean((boolean) val).toString();
            case Types.DATE:
                Date d = new Date();
                d.setTime((long) (((boolean) val ? 1 : 0)));
                return d;
            }
            break;
        }
    } catch (IllegalStateException e) {
        Dialog.msgBox(
                "Could not import cell r:" + c.getRowIndex() + " c: " + c.getColumnIndex()
                        + " because of data type errors in the sheet",
                "Import Excel File", Dialog.ERROR_MESSAGE);
    }
    return null;
}

From source file:Principal.Main.java

private static void showExelData(List sheetData) {
    ////from   w w w  .ja v  a2  s.com
    // Iterates the data and print it out to the console.
    //
    for (int i = 1; i < sheetData.size(); i++) {
        List list = (List) sheetData.get(i);
        for (int j = 0; j < list.size(); j++) {
            Cell cell = (Cell) list.get(j);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                System.out.println(cell.getNumericCellValue());
            } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                System.out.println(cell.getRichStringCellValue().getString().replaceAll(", ", ""));
            } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                System.out.println(cell.getBooleanCellValue());
            }
            if (j < list.size() - 1) {
                System.out.print(", ");
            }
        }
        System.out.println("");
    }
}

From source file:pruebaimportarexcel.excel.Excel.java

/**
 * Devuelve un objeto del tipo que contiene la celda.
 *
 * @return// w  ww  .  ja v  a2s  . c om
 */
public static Object getCellValue(Cell cell) {
    Object result = null;

    if (cell != null) {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                result = cell.getDateCellValue();
            } else {
                if (cell.getNumericCellValue() == (int) cell.getNumericCellValue()) {
                    result = new Integer((int) cell.getNumericCellValue());
                } else {
                    result = new Double(cell.getNumericCellValue());
                }
            }
            break;
        case Cell.CELL_TYPE_STRING:
            result = cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            result = new Boolean(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
        case Cell.CELL_TYPE_BLANK:
        case Cell.CELL_TYPE_ERROR:
            result = null;
            break;
        }
    }

    return result;
}

From source file:py.gov.datos.XlsToCsvConverter.java

License:GNU General Public License

/**
 * Convierte una celda de una planilla XLSX a un elemento de un archivo .csv.
 * @param cell celda a convertir.//from  w  w w.  jav a 2 s.c o m
 * @return elemento generado.
 */
private StringBuffer convertCell(Cell cell) {
    StringBuffer data = new StringBuffer();

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN:
        data.append(cell.getBooleanCellValue() + ";");

        break;
    case Cell.CELL_TYPE_NUMERIC:
        data.append(new DataFormatter().formatCellValue(cell) + ";");

        break;
    case Cell.CELL_TYPE_STRING:
        data.append(CustomStringEscapeUtils.escapeCsv(cell.getStringCellValue()) + ";");
        break;

    case Cell.CELL_TYPE_BLANK:
        data.append("" + ";");
        break;
    default:
        data.append(CustomStringEscapeUtils.escapeCsv(cell.getStringCellValue()) + ";");
    }

    return data;
}

From source file:regression.home.java

public void openData() {
    JFileChooser fileChooser = new JFileChooser();
    int returnValue = fileChooser.showOpenDialog(null);
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        try {//from  w w  w  .  j  a  v  a2s .  c  o  m
            FileInputStream file = new FileInputStream(selectedFile.getPath());
            HSSFWorkbook workbook = new HSSFWorkbook(file);
            HSSFSheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIteratorCount = sheet.iterator();
            Iterator<Row> rowIteratorValue = sheet.iterator();
            int rowCount = 0, colCount = 0, rowValue = 0, colValue = 0;

            while (rowIteratorCount.hasNext()) {
                Row row = rowIteratorCount.next();
                Iterator<Cell> cellIteratorCount = row.cellIterator();
                colCount = 0;
                while (cellIteratorCount.hasNext()) {
                    Cell cell = cellIteratorCount.next();
                    colCount++;
                }
                rowCount++;
            }
            di.header = new String[colCount];
            di.dataValue = new String[rowCount - 1][colCount];

            while (rowIteratorValue.hasNext()) {
                Row row = rowIteratorValue.next();
                Iterator<Cell> cellIteratorValue = row.cellIterator();
                colValue = 0;
                while (cellIteratorValue.hasNext()) {
                    Cell cell = cellIteratorValue.next();
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_BOOLEAN:
                        if (rowValue == 0) {
                            di.header[colValue] = String.valueOf(cell.getBooleanCellValue());
                        } else {
                            di.dataValue[rowValue - 1][colValue] = String.valueOf(cell.getBooleanCellValue());
                        }
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        if (rowValue == 0) {
                            di.header[colValue] = String.valueOf(cell.getNumericCellValue());
                        } else {
                            double cellVal = cell.getNumericCellValue();
                            String dt = null;
                            if (cellVal % 1 != 0) {
                                dt = new DecimalFormat("#0.00##").format(cell.getNumericCellValue());
                                di.dataValue[rowValue - 1][colValue] = dt;
                            } else {
                                dt = new DecimalFormat("#0").format(cell.getNumericCellValue());
                                di.dataValue[rowValue - 1][colValue] = dt;
                            }
                        }
                        break;
                    case Cell.CELL_TYPE_STRING:
                        if (rowValue == 0) {
                            di.header[colValue] = String.valueOf(cell.getStringCellValue());

                        } else {
                            di.dataValue[rowValue - 1][colValue] = String.valueOf(cell.getStringCellValue());
                        }
                        break;
                    }
                    colValue++;
                }
                rowValue++;
            }
            DefaultTableModel model = new DefaultTableModel(di.dataValue, di.header);
            di.jTable1.setModel(model);
            di.jTable1.getTableHeader().setFont(new Font("Tahoma", Font.PLAIN, 16));
            for (int c = 0; c < di.header.length; c++) {
                di.jTable1.getColumnModel().getColumn(c).setCellRenderer(new FTable());
            }

            file.close();
            di.setVisible(true);
            dispose();
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(fileChooser, "File Not Found");
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(fileChooser, "Ekstensi File yang Anda masukkan salah");
        }
    }
}

From source file:regression._dataImport.java

public void openData() {
    JFileChooser fileChooser = new JFileChooser();
    int returnValue = fileChooser.showOpenDialog(null);
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        try {/* ww w  .  j ava  2 s.c  om*/
            FileInputStream file = new FileInputStream(selectedFile.getPath());
            HSSFWorkbook workbook = new HSSFWorkbook(file);
            HSSFSheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIteratorCount = sheet.iterator();
            Iterator<Row> rowIteratorValue = sheet.iterator();
            int rowCount = 0, colCount = 0, rowValue = 0, colValue = 0;

            while (rowIteratorCount.hasNext()) {
                Row row = rowIteratorCount.next();
                Iterator<Cell> cellIteratorCount = row.cellIterator();
                colCount = 0;
                while (cellIteratorCount.hasNext()) {
                    Cell cell = cellIteratorCount.next();
                    colCount++;
                }
                rowCount++;
            }
            header = new String[colCount];
            dataValue = new String[rowCount - 1][colCount];

            while (rowIteratorValue.hasNext()) {
                Row row = rowIteratorValue.next();
                Iterator<Cell> cellIteratorValue = row.cellIterator();
                colValue = 0;
                while (cellIteratorValue.hasNext()) {
                    Cell cell = cellIteratorValue.next();
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_BOOLEAN:
                        if (rowValue == 0) {
                            header[colValue] = String.valueOf(cell.getBooleanCellValue());
                        } else {
                            dataValue[rowValue - 1][colValue] = String.valueOf(cell.getBooleanCellValue());
                        }
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        if (rowValue == 0) {
                            header[colValue] = String.valueOf(cell.getNumericCellValue());
                        } else {
                            double cellVal = cell.getNumericCellValue();
                            String dt = null;
                            if (cellVal % 1 != 0) {
                                dt = new DecimalFormat("#0.00##").format(cell.getNumericCellValue());
                                dataValue[rowValue - 1][colValue] = dt;
                            } else {
                                dt = new DecimalFormat("#0").format(cell.getNumericCellValue());
                                dataValue[rowValue - 1][colValue] = dt;
                            }
                        }
                        break;
                    case Cell.CELL_TYPE_STRING:
                        if (rowValue == 0) {
                            header[colValue] = String.valueOf(cell.getStringCellValue());

                        } else {
                            dataValue[rowValue - 1][colValue] = String.valueOf(cell.getStringCellValue());
                        }
                        break;
                    }
                    colValue++;
                }
                rowValue++;
            }
            DefaultTableModel model = new DefaultTableModel(dataValue, header);
            jTable1.setModel(model);
            for (int c = 0; c < header.length; c++) {
                jTable1.getColumnModel().getColumn(c).setCellRenderer(new FTable());
            }

            file.close();
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(fileChooser, "File Not Found");
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(fileChooser, "Wrong File Type Selected");
        }
    }
}

From source file:ro.dabuno.office.integration.Xlsx2Word.java

public static void main(String[] args) throws Exception {
    log.info("starting app");
    //        Workbook wb = new XSSFWorkbook(new FileInputStream(args[0]));
    Workbook wb = new XSSFWorkbook(new FileInputStream("office-files/Input.xlsx"));

    DataFormatter formatter = new DataFormatter();

    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);/*from   ww  w  .j a v  a  2s .co  m*/
        System.out.println(wb.getSheetName(i));
        int j = 4;
        for (Row row : sheet) {
            System.out.println("rownum: " + row.getRowNum());
            for (Cell cell : row) {
                CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
                System.out.print(cellRef.formatAsString());
                System.out.print(" - ");
                // get the text that appears in the cell by getting the cell value and applying any data formats (Date, 0.00, 1.23e9, $1.23, etc)
                String text = formatter.formatCellValue(cell);
                System.out.println(text);

                System.out.println("------------");
                // Alternatively, get the value and format it yourself
                switch (cell.getCellTypeEnum()) {
                case STRING:
                    System.out.println(cell.getRichStringCellValue().getString());
                    break;
                case NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        System.out.println(cell.getDateCellValue());
                    } else {
                        System.out.print(cellRef.formatAsString());
                        System.out.print(" - ");
                        System.out.println((long) cell.getNumericCellValue());
                    }
                    break;
                case BOOLEAN:
                    System.out.println(cell.getBooleanCellValue());
                    break;
                case FORMULA:
                    System.out.println(cell.getCellFormula());
                    break;
                case BLANK:
                    System.out.println();
                    break;
                default:
                    System.out.println();
                }

            }
            j--;
            if (j == 0) {
                break;
            }
        }
    }

    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p0 = doc.createParagraph();
    XWPFRun r0 = p0.createRun();
    r0.setBold(false);
    r0.setText("Domnule");
    XWPFRun r00 = p0.createRun();
    r00.setBold(true);
    r00.setText(" Ionescu Ion");

    FileOutputStream out = new FileOutputStream("out/xlsx2word.docx");
    doc.write(out);
    out.close();
}

From source file:ro.fortsoft.dataset.xls.XlsDataSet.java

License:Apache License

@Override
public Object getObject(int fieldIndex) {
    Sheet sheet = workbook.getSheetAt(sheetIndex);
    Cell cell = sheet.getRow(cursorPosition).getCell(fieldIndex);
    if (cell == null) {
        return null;
    }//from   w  w  w  .j  a  va  2 s . co m

    Class<?> valueClass = metaData.getFieldClass(fieldIndex);
    if (String.class.equals(valueClass)) {
        return cell.getStringCellValue();
    } else if (Boolean.class.equals(valueClass)) {
        return cell.getBooleanCellValue();
    } else if (Number.class.isAssignableFrom(valueClass)) {
        return cell.getNumericCellValue();
    } else if (Date.class.isAssignableFrom(valueClass)) {
        return cell.getDateCellValue();
    }

    throw new DataSetException("Cannot convert cell value of field '" + metaData.getFieldName(fieldIndex)
            + "' to '" + valueClass + "'");
}