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

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

Introduction

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

Prototype

public HSSFRow getRow() 

Source Link

Document

Returns the HSSFRow this cell belongs to

Usage

From source file:com.elbeesee.poink.transreptor.HSSFCellToXML.java

License:Open Source License

public void onTransrept(INKFRequestContext aContext) throws Exception {
    IHSSFCellRepresentation aIHSSFCellRepresentation = (IHSSFCellRepresentation) aContext
            .sourcePrimary(IHSSFCellRepresentation.class);
    HSSFCell vCell = aIHSSFCellRepresentation.getCellReadOnly();
    String vSheetName = vCell.getSheet().getSheetName();

    StringBuilder vCellXML = new StringBuilder();

    vCellXML.append("<cell columnIndex=\"");
    vCellXML.append(vCell.getColumnIndex());
    vCellXML.append("\" rowIndex=\"");
    vCellXML.append(vCell.getRow().getRowNum());
    vCellXML.append("\" sheetIndex=\"");
    vCellXML.append(vCell.getSheet().getWorkbook().getSheetIndex(vSheetName));
    vCellXML.append("\">");

    int vCellType = vCell.getCellType();
    if (vCellType == Cell.CELL_TYPE_FORMULA) {
        vCellType = vCell.getCachedFormulaResultType();
    }//from w  ww .j a  v a 2s .  c  om

    if (vCellType == Cell.CELL_TYPE_NUMERIC) {
        if (HSSFDateUtil.isCellDateFormatted(vCell)) {
            vCellXML.append(vCell.getDateCellValue());
        } else {
            vCellXML.append(vCell.getNumericCellValue());
        }
    } else if (vCellType == Cell.CELL_TYPE_STRING) {
        vCellXML.append(XMLUtils.escape(vCell.getStringCellValue()));
    } else if (vCellType == Cell.CELL_TYPE_BOOLEAN) {
        vCellXML.append(vCell.getBooleanCellValue());
    } else if (vCellType == Cell.CELL_TYPE_BLANK) {
    } else if (vCellType == Cell.CELL_TYPE_ERROR) {
        vCellXML.append(vCell.getErrorCellValue());
    }
    //

    vCellXML.append("</cell>");

    INKFResponse vResponse = aContext.createResponseFrom(vCellXML.toString());
    vResponse.setExpiry(INKFResponse.EXPIRY_DEPENDENT);
}

From source file:com.haulmont.yarg.formatters.impl.xls.hints.CustomCellStyleHint.java

License:Apache License

private void fixNeighbourCellBorders(HSSFCellStyle cellStyle, HSSFCell resultCell) {
    HSSFSheet sheet = resultCell.getRow().getSheet();
    // disable neighboring cells border
    int columnIndex = resultCell.getColumnIndex();
    int rowIndex = resultCell.getRowIndex();
    // fix left border
    fixLeftBorder(cellStyle, sheet, columnIndex, resultCell);

    // fix right border
    fixRightBorder(cellStyle, sheet, columnIndex, resultCell);

    // fix up border
    fixUpBorder(cellStyle, sheet, columnIndex, rowIndex, resultCell);

    // fix down border
    fixDownBorder(cellStyle, sheet, columnIndex, rowIndex, resultCell);
}

From source file:com.haulmont.yarg.formatters.impl.XLSFormatter.java

License:Apache License

/**
 * Method writes horizontal band//from w ww  .  jav a 2  s .co  m
 * Note: Only one band for row is supported. Now we think that many bands for row aren't usable.
 *
 * @param band          - band to write
 * @param templateSheet - template sheet
 * @param resultSheet   - result sheet
 */
protected void writeHorizontalBand(BandData band, HSSFSheet templateSheet, HSSFSheet resultSheet) {
    String rangeName = band.getName();
    AreaReference templateRange = getAreaForRange(templateWorkbook, rangeName);
    if (templateRange == null) {
        throw wrapWithReportingException(String.format("No such named range in xls file: %s", rangeName));
    }
    CellReference[] crefs = templateRange.getAllReferencedCells();

    CellReference topLeft, bottomRight;
    AreaReference resultRange;

    int rowsAddedByHorizontalBandBackup = rowsAddedByHorizontalBand;
    int rownumBackup = rownum;

    if (crefs != null) {
        addRangeBounds(band, crefs);

        ArrayList<HSSFRow> resultRows = new ArrayList<HSSFRow>();

        int currentRowNum = -1;
        int currentRowCount = -1;
        int currentColumnCount = 0;
        int offset = 0;

        topLeft = new CellReference(rownum + rowsAddedByHorizontalBand, 0);
        // no child bands - merge regions now
        if (band.getChildrenList().isEmpty()) {
            copyMergeRegions(resultSheet, rangeName, rownum + rowsAddedByHorizontalBand,
                    getCellFromReference(crefs[0], templateSheet).getColumnIndex());
        }

        for (CellReference cellRef : crefs) {
            HSSFCell templateCell = getCellFromReference(cellRef, templateSheet);
            HSSFRow resultRow;
            if (templateCell.getRowIndex() != currentRowNum) { //create new row
                resultRow = resultSheet.createRow(rownum + rowsAddedByHorizontalBand);
                copyPageBreaks(templateSheet, resultSheet, templateCell.getRowIndex(), resultRow.getRowNum());
                rowsAddedByHorizontalBand += 1;

                //todo move to options
                if (templateCell.getCellStyle().getParentStyle() != null
                        && templateCell.getCellStyle().getParentStyle().getUserStyleName() != null
                        && templateCell.getCellStyle().getParentStyle().getUserStyleName()
                                .equals(DYNAMIC_HEIGHT_STYLE)) {
                    //resultRow.setHeight(templateCell.getRow().getHeight());
                } else {
                    resultRow.setHeight(templateCell.getRow().getHeight());
                }
                resultRows.add(resultRow);

                currentRowNum = templateCell.getRowIndex();
                currentRowCount++;
                currentColumnCount = 0;
                offset = templateCell.getColumnIndex();
            } else { // or write cell to current row
                resultRow = resultRows.get(currentRowCount);
                currentColumnCount++;
            }

            copyCellFromTemplate(templateCell, resultRow, offset + currentColumnCount, band);
        }

        bottomRight = new CellReference(rownum + rowsAddedByHorizontalBand - 1, offset + currentColumnCount);
        resultRange = new AreaReference(topLeft, bottomRight);

        areaDependencyManager.addDependency(new Area(band.getName(), Area.AreaAlign.HORIZONTAL, templateRange),
                new Area(band.getName(), Area.AreaAlign.HORIZONTAL, resultRange));
        bandsToResultRanges.put(band,
                new Range(resultSheet.getSheetName(), resultRange.getFirstCell().getCol() + 1,
                        resultRange.getFirstCell().getRow() + 1, resultRange.getLastCell().getCol() + 1,
                        resultRange.getLastCell().getRow() + 1));
    }

    for (BandData child : band.getChildrenList()) {
        writeBand(child);
    }

    // scheduled merge regions
    if (!band.getChildrenList().isEmpty() && crefs != null) {
        copyMergeRegions(resultSheet, rangeName, rownumBackup + rowsAddedByHorizontalBandBackup,
                getCellFromReference(crefs[0], templateSheet).getColumnIndex());
    }

    rownum += rowsAddedByHorizontalBand;
    rowsAddedByHorizontalBand = 0;
    rownum += rowsAddedByVerticalBand;
    rowsAddedByVerticalBand = 0;
}

From source file:com.report.excel.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  va 2  s. co m*/
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_STRING:
        // XXX: enrich
        value = cell.getRichStringCellValue().getString();
        break;
    case HSSFCell.CELL_TYPE_FORMULA:
        /*switch (evaluator.evaluateFormulaCell(cell)) {
            case Cell.CELL_TYPE_BOOLEAN:
          value = cell.getBooleanCellValue();
           break;
            case Cell.CELL_TYPE_NUMERIC:
          value = cell.getNumericCellValue();
           break;
            case Cell.CELL_TYPE_STRING:
           System.out.println(cell.getStringCellValue());
           break;
            case Cell.CELL_TYPE_BLANK:
           break;
            case Cell.CELL_TYPE_ERROR:
           System.out.println(cell.getErrorCellValue());
           break;
            case Cell.CELL_TYPE_FORMULA: 
           break;
        }*/

        switch (cell.getCachedFormulaResultType()) {
        case HSSFCell.CELL_TYPE_STRING:
            HSSFRichTextString str = cell.getRichStringCellValue();
            if (str != null && str.length() > 0) {
                value = (str.toString());
            } else {
                value = ExcelToHtmlUtils.EMPTY;
            }
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            HSSFCellStyle style = cellStyle;
            if (style == null) {
                value = String.valueOf(cell.getNumericCellValue());
            } else {
                value = (_formatter.formatRawCellContents(cell.getNumericCellValue(), style.getDataFormat(),
                        style.getDataFormatString()));
            }
            break;
        case HSSFCell.CELL_TYPE_BOOLEAN:
            value = String.valueOf(cell.getBooleanCellValue());
            break;
        case HSSFCell.CELL_TYPE_ERROR:
            value = ErrorEval.getText(cell.getErrorCellValue());
            break;
        default:
            logger.log(POILogger.WARN,
                    "Unexpected cell cachedFormulaResultType (" + cell.getCachedFormulaResultType() + ")");
            value = ExcelToHtmlUtils.EMPTY;
            break;
        }
        break;
    case HSSFCell.CELL_TYPE_BLANK:
        value = ExcelToHtmlUtils.EMPTY;
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        value = _formatter.formatCellValue(cell);
        break;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        value = String.valueOf(cell.getBooleanCellValue());
        break;
    case HSSFCell.CELL_TYPE_ERROR:
        value = ErrorEval.getText(cell.getErrorCellValue());
        break;
    default:
        logger.log(POILogger.WARN, "Unexpected cell type (" + cell.getCellType() + ")");
        return true;
    }

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

    final short cellStyleIndex = cellStyle.getIndex();
    if (cellStyleIndex != 0) {
        HSSFWorkbook workbook = cell.getRow().getSheet().getWorkbook();
        String mainCssClass = getStyleClassName(workbook, cellStyle);
        if (wrapInDivs) {
            tableCellElement.setAttribute("class", mainCssClass + " " + 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 (isOutputLeadingSpacesAsNonBreaking() && value.startsWith(" ")) {
        StringBuilder builder = new StringBuilder();
        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 = htmlDocumentFacade.createText(value);

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

        Element innerDiv = htmlDocumentFacade.createBlock();
        StringBuilder innerDivStyle = new StringBuilder();
        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());
        htmlDocumentFacade.addStyleClass(outerDiv, 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.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;/* w w w .j  a va2 s. co  m*/
    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:de.viaboxx.nlstools.formats.MBExcelPersistencer.java

License:Apache License

private Comment comment(HSSFCell cell, String text) {
    CreationHelper factory = wb.getCreationHelper();
    Drawing drawing = sheet.createDrawingPatriarch();
    // When the comment box is visible, have it show in a 1x3 space
    ClientAnchor anchor = factory.createClientAnchor();
    anchor.setCol1(cell.getColumnIndex());
    anchor.setCol2(cell.getColumnIndex() + 1);
    anchor.setRow1(cell.getRow().getRowNum());
    anchor.setRow2(cell.getRow().getRowNum() + 3);
    Comment comment = drawing.createCellComment(anchor);
    RichTextString str = factory.createRichTextString(text);
    comment.setString(str);/*from   ww w  .  j a va  2s .  c om*/
    cell.setCellComment(comment);
    return comment;
}