Example usage for org.apache.poi.hssf.usermodel HSSFCell getBooleanCellValue

List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getBooleanCellValue

Introduction

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

Prototype

@Override
public boolean getBooleanCellValue() 

Source Link

Document

get the value of the cell as a boolean.

Usage

From source file:POI.Sheet.java

/**
 * ??/* w w w . ja  v a 2 s. c om*/
 *
 * @param index ?
 * @return ?
 * @see HSSFRow
 * @see HSSFCell
 */
public ArrayList<String> getRowAt(int index) {
    HSSFRow row = sheet.getRow(index);
    ArrayList<String> cells = new ArrayList<String>();
    int i = row.getFirstCellNum();
    while (i < this.getColumnSize()) {
        HSSFCell cell = row.getCell(i++);
        if (cell == null) {
            cells.add("");
        } else {
            Object val = null;
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                val = cell.getBooleanCellValue();
                break;
            case Cell.CELL_TYPE_FORMULA:
                val = cell.getCellFormula();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                val = cell.getNumericCellValue();
                break;
            case Cell.CELL_TYPE_STRING:
                val = cell.getStringCellValue();
            default:
                val = cell.getRichStringCellValue();
            }

            cells.add(String.valueOf(val));
        }
    }
    return cells;
}

From source file:ro.nextreports.engine.exporter.util.XlsUtil.java

License:Apache License

/**
 * Copy a cell to another cell/*from ww w.jav  a2 s . c  o m*/
 * 
 * @param oldCell cell to be copied
 * @param newCell cell to be created
 * @param styleMap style map
 */
public static void copyCell(HSSFCell oldCell, HSSFCell newCell, Map<Integer, HSSFCellStyle> styleMap) {
    if (styleMap != null) {
        if (oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()) {
            newCell.setCellStyle(oldCell.getCellStyle());
        } else {
            int stHashCode = oldCell.getCellStyle().hashCode();
            HSSFCellStyle newCellStyle = styleMap.get(stHashCode);
            if (newCellStyle == null) {
                newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
                newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
                styleMap.put(stHashCode, newCellStyle);
            }
            newCell.setCellStyle(newCellStyle);
        }
    }
    switch (oldCell.getCellType()) {
    case HSSFCell.CELL_TYPE_STRING:
        newCell.setCellValue(oldCell.getStringCellValue());
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        newCell.setCellValue(oldCell.getNumericCellValue());
        break;
    case HSSFCell.CELL_TYPE_BLANK:
        newCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
        break;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        newCell.setCellValue(oldCell.getBooleanCellValue());
        break;
    case HSSFCell.CELL_TYPE_ERROR:
        newCell.setCellErrorValue(oldCell.getErrorCellValue());
        break;
    case HSSFCell.CELL_TYPE_FORMULA:
        newCell.setCellFormula(oldCell.getCellFormula());
        break;
    default:
        break;
    }

}

From source file:shouQiSystem.user.ReadDriverEXL.java

private String getValue(HSSFCell hssfCell) {
    DecimalFormat df = new DecimalFormat("#");
    switch (hssfCell.getCellType()) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        if (HSSFDateUtil.isCellDateFormatted(hssfCell)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println(sdf.format(HSSFDateUtil.getJavaDate(hssfCell.getNumericCellValue())).toString());
            return sdf.format(HSSFDateUtil.getJavaDate(hssfCell.getNumericCellValue())).toString();
        }/*from   w ww.jav  a  2s  .  com*/
        System.out.println(df.format(hssfCell.getNumericCellValue()).toString());
        return df.format(hssfCell.getNumericCellValue()).toString();

    case HSSFCell.CELL_TYPE_STRING:
        System.out.println(hssfCell.getStringCellValue());
        return hssfCell.getStringCellValue();
    case HSSFCell.CELL_TYPE_FORMULA:
        return hssfCell.getCellFormula();
    case HSSFCell.CELL_TYPE_BLANK:
        return "";
    case HSSFCell.CELL_TYPE_BOOLEAN:
        return hssfCell.getBooleanCellValue() + "";
    case HSSFCell.CELL_TYPE_ERROR:
        return hssfCell.getErrorCellValue() + "";
    }
    return "";
}

From source file:swift.selenium.TransactionMapping.java

License:Open Source License

@SuppressWarnings("null")
public static String getCellData(String reqValue, HSSFSheet reqSheet, int rowIndex,
        HashMap<String, Object> inputHashTable) throws IOException {
    HSSFCell reqCell = null;
    Object actualvalue = null;//  w  w  w. j  a v  a 2 s  .com
    String req = "";
    DataFormatter fmt = new DataFormatter();
    if (inputHashTable.isEmpty() == true) {
        inputHashTable = getValueFromHashMap(reqSheet);
    }
    HSSFRow rowActual = reqSheet.getRow(rowIndex);
    if (inputHashTable.get(reqValue) == null) {
        report.setStrMessage("Column " + reqValue + " not Found. Please Check input Sheet");
        pauseFun("Column " + reqValue + " not Found. Please Check input Sheet");
    } else {
        actualvalue = inputHashTable.get(reqValue);// rowHeader.getCell(colIndex).toString();
        if (actualvalue != null) {
            int colIndex = Integer.parseInt(actualvalue.toString());
            reqCell = rowActual.getCell(colIndex);
            if (reqCell == null) {
                System.out.println(reqValue + " is Null");
            } else {
                int type = reqCell.getCellType();
                switch (type) {
                case HSSFCell.CELL_TYPE_BLANK:
                    req = "";
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    req = fmt.formatCellValue(reqCell);
                    break;
                case HSSFCell.CELL_TYPE_STRING:
                    req = reqCell.getStringCellValue();
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN:
                    req = Boolean.toString(reqCell.getBooleanCellValue());
                    break;
                case HSSFCell.CELL_TYPE_ERROR:
                    req = "error";
                    break;
                case HSSFCell.CELL_TYPE_FORMULA:
                    req = reqCell.getCellFormula();
                    break;
                }
            }
        }

        else {
            req = reqCell.getStringCellValue();
            System.out.println("null");
        }
    }
    return req;
}

From source file:swift.selenium.WebHelper.java

License:Open Source License

/**
 * This method reads and returns data from each cell of a provided worksheet
 * /*from w  ww  . ja va2 s .  c om*/
 * @param reqValue
 * @param reqSheet
 * @param rowIndex
 * @param inputHashTable
 * @return
 * @throws IOException
 */
@SuppressWarnings("null")
public static String getCellData(String reqValue, HSSFSheet reqSheet, int rowIndex,
        HashMap<String, Object> inputHashTable) throws IOException {
    HSSFCell reqCell = null;
    Object actualvalue = null;
    String req = "";
    DataFormatter fmt = new DataFormatter();

    if (inputHashTable.isEmpty() == true) {
        inputHashTable = getValueFromHashMap(reqSheet);
    }
    HSSFRow rowActual = reqSheet.getRow(rowIndex);
    if (inputHashTable.get(reqValue) == null) {

        TransactionMapping.report.setStrMessage("Column " + reqValue + " not Found. Please Check input Sheet");
        TransactionMapping.pauseFun("Column " + reqValue + " not Found. Please Check input Sheet");
    } else {
        actualvalue = inputHashTable.get(reqValue);//rowHeader.getCell(colIndex).toString();         
        if (actualvalue != null) {
            int colIndex = Integer.parseInt(actualvalue.toString());
            reqCell = rowActual.getCell(colIndex);
            //TM 27-04-2015: Updated the code for formula in cells
            if (reqCell == null) {
                System.out.println(reqValue + " is Null");
            } else {
                HSSFWorkbook wb = reqCell.getSheet().getWorkbook(); //TM-05/05/2015: Get workbook instance from the worksheet
                HSSFFormulaEvaluator.evaluateAllFormulaCells(wb); //TM-05/05/2015: To refresh all the formulas in the worksheet
                FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

                CellValue cellValue = evaluator.evaluate(reqCell);
                int type = 0;
                if (cellValue != null) {
                    type = cellValue.getCellType();
                } else {
                    type = reqCell.getCellType();
                }

                switch (type) {
                case HSSFCell.CELL_TYPE_BLANK:
                    req = "";
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(reqCell)) {
                        SimpleDateFormat form = new SimpleDateFormat(
                                Automation.configHashMap.get("DATEFORMAT").toString());
                        req = form.format(reqCell.getDateCellValue());
                    } else
                        req = fmt.formatCellValue(reqCell, evaluator);
                    break;
                case HSSFCell.CELL_TYPE_STRING:
                    req = reqCell.getStringCellValue();
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN:
                    req = Boolean.toString(reqCell.getBooleanCellValue());
                    break;
                case HSSFCell.CELL_TYPE_ERROR:
                    req = "error";
                    break;
                }
            }
        }

        else {
            req = reqCell.getStringCellValue();
            System.out.println("null");
        }
    }
    return req;
}

From source file:SwiftSeleniumWeb.WebHelper.java

License:Open Source License

@SuppressWarnings("null")
public static String getCellData(String reqValue, HSSFSheet reqSheet, int rowIndex,
        HashMap<String, Object> inputHashTable) throws IOException {
    HSSFCell reqCell = null;
    Object actualvalue = null;//from w  w w .  jav a2 s .  co m
    String req = "";
    DataFormatter fmt = new DataFormatter();
    if (inputHashTable.isEmpty() == true) {
        inputHashTable = getValueFromHashMap(reqSheet);
    }
    HSSFRow rowActual = reqSheet.getRow(rowIndex);
    if (inputHashTable.get(reqValue) == null) {
        SwiftSeleniumWeb.WebDriver.report
                .setStrMessage("Column " + reqValue + " not Found. Please Check input Sheet");
        MainController.pauseFun("Column " + reqValue + " not Found. Please Check input Sheet");
    } else {
        actualvalue = inputHashTable.get(reqValue);//rowHeader.getCell(colIndex).toString();         
        if (actualvalue != null) {
            int colIndex = Integer.parseInt(actualvalue.toString());
            reqCell = rowActual.getCell(colIndex);
            if (reqCell == null) {
                System.out.println(reqValue + " is Null");
            } else {
                int type = reqCell.getCellType();
                switch (type) {
                case HSSFCell.CELL_TYPE_BLANK:
                    req = "";
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    req = fmt.formatCellValue(reqCell);
                    break;
                case HSSFCell.CELL_TYPE_STRING:
                    req = reqCell.getStringCellValue();
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN:
                    req = Boolean.toString(reqCell.getBooleanCellValue());
                    break;
                case HSSFCell.CELL_TYPE_ERROR:
                    req = "error";
                    break;
                case HSSFCell.CELL_TYPE_FORMULA:
                    req = reqCell.getCellFormula();
                    break;
                }
            }
        }

        else {
            req = reqCell.getStringCellValue();
            System.out.println("null");
        }
    }
    return req;
}

From source file:test.ExcelUtil.java

License:Apache License

/**
 * , //from  w ww .j  av a 2  s. com
 * 
 * @param cell
 * @return
 */
public static String cell2string(HSSFCell cell, HSSFFormulaEvaluator evaluator) {
    if (cell == null) {
        return null;
    }
    String str = null;
    final int cellType = cell.getCellType();

    switch (cellType) {
    case HSSFCell.CELL_TYPE_STRING:
        str = "" + cell.getRichStringCellValue().getString().trim();
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
            str = "" + dateFormat.format(cell.getDateCellValue());
        } else {
            str = String.valueOf(cell.getNumericCellValue());

            //modify by cyyan 2008-09-23 19:17:28 
            //excelE, ;
            //E, 15, (15, )
            // 15, 0, 0
            //            str = "" + new BigDecimal(numberStr).setScale(15, BigDecimal.ROUND_HALF_UP);
            //            
            //            //modify yanchangyou 2008-09-26 18:01:43 
            //            // .0000* , 0, 
            //            if (str.indexOf('.') != -1) {
            //               str = str.replaceAll("(\\.)?0*$", "");
            //            }            

            /*
             * , 
             */
            //            if (str.indexOf('.') != -1) { //
            //               int index = str.length();
            //               for (int i = str.length()-1; i > -1; i--) {
            //                  if (str.charAt(i) == '0') {
            //                     index = i;
            //                  } else if (str.charAt(i) == '.'){
            //                     index = i;
            //                     break;
            //                  } else {
            //                     break;
            //                  }
            //               }
            //               str = str.substring(0, index);
            //            }
        }
        break;
    case HSSFCell.CELL_TYPE_BLANK:
        str = "";
        break;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        str = "" + cell.getBooleanCellValue();
        break;
    case HSSFCell.CELL_TYPE_ERROR:
        str = "" + cell.getErrorCellValue();
        break;
    case HSSFCell.CELL_TYPE_FORMULA:
        if (evaluator == null) {
            str = "" + cell.getRichStringCellValue().getString();
        } else {
            str = "" + evaluator.evaluate(cell).getNumberValue();
        }

        break;
    }

    return (str == null || str.trim().equals("")) ? null : str.trim();
}

From source file:utilesBD.servidoresDatos.JServerServidorDatosExcel.java

/**
 * Metodo que se encarga de leer los datos de un archivo en formato excel
 * @return Fila que contiene la informacion del archivo
 *//*from w ww. j a va  2s  . c om*/
public JFilaDatosDefecto leeLineaExcel(HSSFRow hssfRow) {
    JFilaDatosDefecto loLinea = new JFilaDatosDefecto();
    //Me barro todos los elementos de una fila
    for (int i = hssfRow.getFirstCellNum(); i < hssfRow.getLastCellNum(); i++) {
        HSSFCell hssfCell = hssfRow.getCell(i);
        if (hssfCell != null) {
            switch (hssfCell.getCellType()) {
            case HSSFCell.CELL_TYPE_BOOLEAN:
                loLinea.addCampo(String.valueOf(hssfCell.getBooleanCellValue()));
                break;
            case HSSFCell.CELL_TYPE_FORMULA:
                try {
                    loLinea.addCampo(hssfCell.getStringCellValue());
                } catch (Exception e) {
                    try {
                        loLinea.addCampo(String.valueOf(hssfCell.getNumericCellValue()));
                    } catch (Exception e1) {
                        try {
                            loLinea.addCampo(new JDateEdu(hssfCell.getDateCellValue()).toString()
                                    .replace("31/12/1899 ", ""));
                        } catch (Exception e2) {
                            try {
                                loLinea.addCampo(String.valueOf(hssfCell.getBooleanCellValue()));
                            } catch (Exception e3) {
                                loLinea.addCampo("");
                            }
                        }
                    }
                }
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(hssfCell)) {
                    loLinea.addCampo(
                            new JDateEdu(hssfCell.getDateCellValue()).toString().replace("31/12/1899 ", ""));
                } else {
                    double ldValor = hssfCell.getNumericCellValue();
                    loLinea.addCampo(
                            JFormat.msFormatearDouble(ldValor, "############.#########").replace(',', '.'));
                }
                break;
            case HSSFCell.CELL_TYPE_STRING:
                loLinea.addCampo(hssfCell.toString());
                break;
            default:
                loLinea.addCampo("");
            }
        } else {
            loLinea.addCampo("");
        }
    }
    return loLinea;
}

From source file:ypcnv.views.impl.FileXLS.java

License:Open Source License

/**
 * With respect to cell type get cell's content.
 * //from w  ww. ja va 2  s. c  om
 * @param cell
 *            - cell to be processed.
 * @return Cell's content.
 */
private String getCellContent(HSSFCell cell) {

    int foundCellType = cell.getCellType();
    String cellContent = null;

    switch (foundCellType) {
    case Cell.CELL_TYPE_STRING:
        cellContent = cell.getStringCellValue();
        break;
    case Cell.CELL_TYPE_FORMULA:
        cellContent = cell.getStringCellValue();
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        cellContent = cell.getBooleanCellValue() ? "TRUE" : "FALSE";
        break;
    case Cell.CELL_TYPE_ERROR:
        byte xlsErrorCode = cell.getErrorCellValue();
        if (xlsErrorCode == 0) {
            cellContent = "";
        } else {
            cellContent = "XLS error code '" + xlsErrorCode + "'.";
            String message = "XLS cell type is 'ERROR', the XLS error code is '" + xlsErrorCode + "'."
                    + " The cell row=" + cell.getRowIndex() + ", col=" + cell.getColumnIndex() + ".";
            LOG.info(message);
        }
        break;
    case Cell.CELL_TYPE_NUMERIC:
        HSSFDataFormatter numericFormat = new HSSFDataFormatter();
        cellContent = numericFormat.formatCellValue(cell);
        // cellContent = cell.getNumericCellValue();
        break;
    default:
        cellContent = "";
        break;
    }
    return cellContent;
}