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

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

Introduction

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

Prototype

public double getNumericCellValue() 

Source Link

Document

Get the value of the cell as a number.

Usage

From source file:com.servoy.extensions.plugins.excelxport.ImportSelectSheetPanel.java

License:Open Source License

public Object getValueAt(int r, int c) {
    if (r == -1) {
        r = 0;//from   w  ww. j a  va  2s  .  com
    } else if (useHeaderRow) {
        r++;
    }
    HSSFRow row = sheet.getRow(r);
    if (row != null) {
        HSSFCell cell = row.getCell((short) c);
        if (cell != null) {
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                Number d = new Double(cell.getNumericCellValue());
                if (((int) d.doubleValue()) == Math.ceil(d.doubleValue())) {
                    d = new Integer(d.intValue());
                }
                return d;
            //               case HSSFCell.CELL_TYPE_NUMERIC:
            //               return cell.getStringCellValue();   

            default:
                return cell.getStringCellValue();
            }
        }
    }
    return null;
}

From source file:com.siva.javamultithreading.ExcelUtil.java

public static void copyCell(HSSFWorkbook newWorkbook, HSSFCell oldCell, HSSFCell newCell,
        Map<Integer, HSSFCellStyle> styleMap) {
    if (styleMap != null) {
        int stHashCode = oldCell.getCellStyle().hashCode();
        HSSFCellStyle newCellStyle = styleMap.get(stHashCode);
        if (newCellStyle == null) {
            newCellStyle = newWorkbook.createCellStyle();
            newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
            styleMap.put(stHashCode, newCellStyle);
        }/* ww  w  .  ja va 2  s .  co  m*/
        newCell.setCellStyle(newCellStyle);
    }
    switch (oldCell.getCellType()) {
    case HSSFCell.CELL_TYPE_STRING:
        newCell.setCellValue(oldCell.getRichStringCellValue());
        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:com.smanempat.controller.ControllerClassification.java

private void showXLS(JTextField txtFileDirectory, JTable tablePreview)
        throws FileNotFoundException, IOException {
    DefaultTableModel tableModel = new DefaultTableModel();
    File fileName = new File(txtFileDirectory.getText());
    FileInputStream inputStream = new FileInputStream(fileName);
    HSSFWorkbook workBook = new HSSFWorkbook(inputStream);
    HSSFSheet sheet = workBook.getSheetAt(0);

    int rowValue = sheet.getLastRowNum() + 1;
    int colValue = sheet.getRow(0).getLastCellNum();
    String[][] data = new String[rowValue][colValue];
    String[] colName = new String[colValue];
    for (int i = 0; i < rowValue; i++) {
        HSSFRow row = sheet.getRow(i);/*from  w w w. j  a  v a  2s. c  o  m*/
        for (int j = 0; j < colValue; j++) {
            HSSFCell cell = row.getCell(j);
            int type = cell.getCellType();
            Object returnCellValue = null;
            if (type == 0) {
                returnCellValue = cell.getNumericCellValue();
            } else if (type == 1) {
                returnCellValue = cell.getStringCellValue();
            }

            data[i][j] = returnCellValue.toString();
        }
    }

    for (int i = 0; i < colValue; i++) {
        colName[i] = data[0][i];
    }

    tableModel = new DefaultTableModel(data, colName);
    tablePreview.setModel(tableModel);
    tableModel.removeRow(0);
}

From source file:com.smanempat.view.ReadWorkbook.java

String nilaiCell(HSSFCell pCell) {
    int tipe = pCell.getCellType();
    Object nilaiBalik = null;//w  ww .  j  a v  a 2  s . c o  m

    if (tipe == 0) {
        nilaiBalik = pCell.getNumericCellValue();

    } else if (tipe == 1) {
        nilaiBalik = pCell.getStringCellValue();
    }

    return nilaiBalik.toString();
}

From source file:com.thingtrack.xbom.parser.XBomParserApplication.java

License:Apache License

private AssemblingPartNode getAssemblingPart(HSSFRow row)
        throws XbomParsingException, NoAssemblingPartException {

    AssemblingPartNode assemblingPartNode = new AssemblingPartNode();

    try {/*from  w w  w.  ja  va2 s .co  m*/

        Integer level = null;
        // Assembling part level
        for (int i = 0; i < 6; i++) {

            HSSFCell cell = row.getCell(i);

            if (cell.getCellType() == Cell.CELL_TYPE_BLANK)
                continue;

            level = (int) cell.getNumericCellValue();
            break;
        }

        if (level == null)
            throw new NoAssemblingPartException();

        assemblingPartNode.setLevel(level);
        assemblingPartNode.setPartName(row.getCell(XBOM_PART_NAME_COLUMN_INDEX).getStringCellValue());
        assemblingPartNode.setQuantity(row.getCell(XBOM_PART_QUANTITY_COLUMN_INDEX).getNumericCellValue());
        assemblingPartNode.setUnit(row.getCell(XBOM_PART_UNIT_COLUMN_INDEX).getStringCellValue());
        assemblingPartNode
                .setPartReferenceNumber(row.getCell(XBOM_PART_REFERENCE_NUM_COLUMN_INDEX).getStringCellValue());
        assemblingPartNode.setVersion(row.getCell(XBOM_PART_VERSION_COLUMN_INDEX).getStringCellValue());

    } catch (RuntimeException e) {
        throw new XbomParsingException(e);
    }

    return assemblingPartNode;

}

From source file:com.verticon.treatment.poi.handlers.PoiUtils.java

License:Open Source License

static String getStringValue(HSSFRow row, EStructuralFeature feature, int index)
        throws MissingCriticalDataException {
    String result = null;//www . j ava 2  s.  c om

    if (index != -1) {
        try {
            HSSFCell cellContents = row.getCell(index);
            if (cellContents != null) {
                switch (cellContents.getCellType()) {
                case HSSFCell.CELL_TYPE_STRING:
                    result = cellContents.getStringCellValue();
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    result = Double.toString(cellContents.getNumericCellValue());
                    result = result.replace(".0", "");
                    break;
                default:
                    throw new MissingCriticalDataException(
                            "The string value in a critical spreadsheet cell has the wrong data type (id: "
                                    + cellContents.getCellType()
                                    + "). Please make sure your spreadsheet column number " + index
                                    + " is set to the string datatype.",
                            index, feature, row.getRowNum());

                }

            }
        } catch (RuntimeException e) {
            // just fall through and return a null
        }
    }
    return result;
}

From source file:com.verticon.treatment.poi.handlers.PoiUtils.java

License:Open Source License

static String getValue(HSSFRow row, EStructuralFeature feature, int index) {
    String result = null;/* ww w .j  av  a  2  s.  com*/

    if (index != -1) {
        try {
            HSSFCell cellContents = row.getCell(index);
            if (cellContents != null) {
                switch (cellContents.getCellType()) {
                case HSSFCell.CELL_TYPE_STRING:
                    result = cellContents.getStringCellValue();
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    double num = cellContents.getNumericCellValue();
                    long l = (long) num;
                    result = Long.toString(l);
                    break;
                default:
                    break;
                }

            }
        } catch (RuntimeException e) {
            //just fall through and return a null
        }
    }
    return result;
}

From source file:com.verticon.treatment.poi.handlers.PoiUtils.java

License:Open Source License

private static BigDecimal getDecimalValue(HSSFRow row, EAttribute feature, int index) {
    BigDecimal result = null;//  ww  w  .j a  v a  2  s  . com

    if (index != -1) {
        try {
            HSSFCell cellContents = row.getCell(index);
            if (cellContents != null) {
                switch (cellContents.getCellType()) {
                case HSSFCell.CELL_TYPE_NUMERIC:
                    result = new BigDecimal(cellContents.getNumericCellValue());
                    break;
                default:
                    System.err.printf("Wrong type for decimal %s%n", cellContents.getCellType());
                    break;
                }

            }
        } catch (RuntimeException e) {
            // just fall through and return a null
        }
    }
    return result;
}

From source file:com.wangzhu.poi.ExcelToHtmlConverter.java

License:Apache License

protected boolean processCell(HSSFCell cell, Element tableCellElement, int normalWidthPx, int maxSpannedWidthPx,
        float normalHeightPt) {
    final HSSFCellStyle cellStyle = cell.getCellStyle();

    String value;//from   w w  w .  j  a  v a2  s . c om
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        // XXX: enrich
        value = cell.getRichStringCellValue().getString();
        break;
    case Cell.CELL_TYPE_FORMULA:
        switch (cell.getCachedFormulaResultType()) {
        case Cell.CELL_TYPE_STRING:
            HSSFRichTextString str = cell.getRichStringCellValue();
            if ((str != null) && (str.length() > 0)) {
                value = (str.toString());
            } else {
                value = ExcelToHtmlUtils.EMPTY;
            }
            break;
        case Cell.CELL_TYPE_NUMERIC:
            HSSFCellStyle style = cellStyle;
            if (style == null) {
                value = String.valueOf(cell.getNumericCellValue());
            } else {
                value = (this._formatter.formatRawCellContents(cell.getNumericCellValue(),
                        style.getDataFormat(), style.getDataFormatString()));
            }
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            value = String.valueOf(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_ERROR:
            value = ErrorEval.getText(cell.getErrorCellValue());
            break;
        default:
            ExcelToHtmlConverter.logger.log(POILogger.WARN,
                    "Unexpected cell cachedFormulaResultType (" + cell.getCachedFormulaResultType() + ")");
            value = ExcelToHtmlUtils.EMPTY;
            break;
        }
        break;
    case Cell.CELL_TYPE_BLANK:
        value = ExcelToHtmlUtils.EMPTY;
        break;
    case Cell.CELL_TYPE_NUMERIC:
        value = this._formatter.formatCellValue(cell);
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        value = String.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        value = ErrorEval.getText(cell.getErrorCellValue());
        break;
    default:
        ExcelToHtmlConverter.logger.log(POILogger.WARN, "Unexpected cell type (" + cell.getCellType() + ")");
        return true;
    }

    final boolean noText = ExcelToHtmlUtils.isEmpty(value);
    final boolean wrapInDivs = !noText && this.isUseDivsToSpan() && !cellStyle.getWrapText();

    final short cellStyleIndex = cellStyle.getIndex();
    if (cellStyleIndex != 0) {
        HSSFWorkbook workbook = cell.getRow().getSheet().getWorkbook();
        String mainCssClass = this.getStyleClassName(workbook, cellStyle);
        if (wrapInDivs) {
            tableCellElement.setAttribute("class", mainCssClass + " " + this.cssClassContainerCell);
        } else {
            tableCellElement.setAttribute("class", mainCssClass);
        }

        if (noText) {
            /*
             * if cell style is defined (like borders, etc.) but cell text
             * is empty, add "&nbsp;" to output, so browser won't collapse
             * and ignore cell
             */
            value = "\u00A0";
        }
    }

    if (this.isOutputLeadingSpacesAsNonBreaking() && value.startsWith(" ")) {
        StringBuffer builder = new StringBuffer();
        for (int c = 0; c < value.length(); c++) {
            if (value.charAt(c) != ' ') {
                break;
            }
            builder.append('\u00a0');
        }

        if (value.length() != builder.length()) {
            builder.append(value.substring(builder.length()));
        }

        value = builder.toString();
    }

    Text text = this.htmlDocumentFacade.createText(value);

    if (wrapInDivs) {
        Element outerDiv = this.htmlDocumentFacade.createBlock();
        outerDiv.setAttribute("class", this.cssClassContainerDiv);

        Element innerDiv = this.htmlDocumentFacade.createBlock();
        StringBuffer innerDivStyle = new StringBuffer();
        innerDivStyle.append("position:absolute;min-width:");
        innerDivStyle.append(normalWidthPx);
        innerDivStyle.append("px;");
        if (maxSpannedWidthPx != Integer.MAX_VALUE) {
            innerDivStyle.append("max-width:");
            innerDivStyle.append(maxSpannedWidthPx);
            innerDivStyle.append("px;");
        }
        innerDivStyle.append("overflow:hidden;max-height:");
        innerDivStyle.append(normalHeightPt);
        innerDivStyle.append("pt;white-space:nowrap;");
        ExcelToHtmlUtils.appendAlign(innerDivStyle, cellStyle.getAlignment());
        this.htmlDocumentFacade.addStyleClass(outerDiv, this.cssClassPrefixDiv, innerDivStyle.toString());

        innerDiv.appendChild(text);
        outerDiv.appendChild(innerDiv);
        tableCellElement.appendChild(outerDiv);
    } else {
        tableCellElement.appendChild(text);
    }

    return ExcelToHtmlUtils.isEmpty(value) && (cellStyleIndex == 0);
}

From source file:com.weibo.datasys.parser.office.extractor.ExcelParse.java

License:Open Source License

private FileData extractor(File filePath) {
    FileData fData = new FileData();
    fData.setName(filePath.getName());/*from   www .  j  a v  a2  s .  c  om*/
    StringBuffer sBuffer = new StringBuffer();
    HSSFWorkbook workbook = null;
    try {
        workbook = new HSSFWorkbook(new FileInputStream(filePath));
        for (int iSheets = 0; iSheets < workbook.getNumberOfSheets(); ++iSheets) {
            HSSFSheet sheet = workbook.getSheetAt(iSheets);
            for (int iRow = 0; iRow < sheet.getLastRowNum(); ++iRow) {
                HSSFRow row = sheet.getRow(iRow);
                for (int iCell = 0; iCell < row.getLastCellNum(); ++iCell) {
                    HSSFCell cell = row.getCell(iCell);
                    if (null != cell) {
                        if (0 == cell.getCellType()) {
                            sBuffer.append(String.valueOf(cell.getNumericCellValue()));
                            sBuffer.append(SEGMENT_CHAR);
                        } else if (1 == cell.getCellType()) {
                            sBuffer.append(cell.getStringCellValue().trim());
                            sBuffer.append(SEGMENT_CHAR);
                        }
                    }
                }
            }
        }
        fData.setContent(sBuffer.toString());
    } catch (Exception e) {
        LOG.error("", e);
    }
    return fData;
}