Example usage for org.apache.poi.ss.usermodel Cell getSheet

List of usage examples for org.apache.poi.ss.usermodel Cell getSheet

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Cell getSheet.

Prototype

Sheet getSheet();

Source Link

Document

Returns the sheet this cell belongs to

Usage

From source file:uk.co.certait.htmlexporter.writer.excel.ExcelStyleGenerator.java

License:Apache License

protected void applyWidth(Cell cell, Style style) {
    if (style.getProperty(CssIntegerProperty.WIDTH) > 0) {
        cell.getSheet().setColumnWidth(cell.getColumnIndex(), style.getProperty(CssIntegerProperty.WIDTH) * 50);
    }/* w  ww.j  a va  2 s .c  o  m*/
}

From source file:uk.co.spudsoft.birt.emitters.excel.handlers.CellContentHandler.java

License:Open Source License

/**
 * Finish processing for the current (real) cell.
 * @param element/*from  w ww.ja va 2  s  . c o  m*/
 * The element that signifies the end of the cell (this may not be an ICellContent object if the 
 * cell is created for a label or text outside of a table). 
 */
protected void endCellContent(HandlerState state, ICellContent birtCell, IContent element, Cell cell,
        Area area) {
    StyleManager sm = state.getSm();
    StyleManagerUtils smu = state.getSmu();

    BirtStyle birtCellStyle = null;
    if (birtCell != null) {
        birtCellStyle = new BirtStyle(birtCell);
        if (element != null) {
            // log.debug( "Overlaying style from ", element );
            birtCellStyle.overlay(element);
        }
    } else if (element != null) {
        birtCellStyle = new BirtStyle(element);
    } else {
        birtCellStyle = new BirtStyle(state.getSm().getCssEngine());
    }
    if (preferredAlignment != null) {
        birtCellStyle.setProperty(StyleConstants.STYLE_TEXT_ALIGN, preferredAlignment);
    }
    if (CSSConstants.CSS_TRANSPARENT_VALUE
            .equals(birtCellStyle.getString(StyleConstants.STYLE_BACKGROUND_COLOR))) {
        if (parent != null) {
            birtCellStyle.setProperty(StyleConstants.STYLE_BACKGROUND_COLOR, parent.getBackgroundColour());
        }
    }
    if (hyperlinkUrl != null) {
        Hyperlink hyperlink = cell.getSheet().getWorkbook().getCreationHelper()
                .createHyperlink(Hyperlink.LINK_URL);
        hyperlink.setAddress(hyperlinkUrl);
        cell.setHyperlink(hyperlink);
    }
    if (hyperlinkBookmark != null) {
        Hyperlink hyperlink = cell.getSheet().getWorkbook().getCreationHelper()
                .createHyperlink(Hyperlink.LINK_DOCUMENT);
        hyperlink.setAddress(prepareName(hyperlinkBookmark));
        cell.setHyperlink(hyperlink);
    }

    if (lastValue != null) {
        if (lastValue instanceof String) {
            String lastString = (String) lastValue;

            smu.correctFontColorIfBackground(birtCellStyle);
            for (RichTextRun run : richTextRuns) {
                run.font = smu.correctFontColorIfBackground(sm.getFontManager(), state.getWb(), birtCellStyle,
                        run.font);
            }

            if (!richTextRuns.isEmpty()) {
                RichTextString rich = smu.createRichTextString(lastString);
                int runStart = richTextRuns.get(0).startIndex;
                Font lastFont = richTextRuns.get(0).font;
                for (int i = 0; i < richTextRuns.size(); ++i) {
                    RichTextRun run = richTextRuns.get(i);
                    log.debug("Run: ", run.startIndex, " font :", run.font);
                    if (!lastFont.equals(run.font)) {
                        log.debug("Applying ", runStart, " - ", run.startIndex);
                        rich.applyFont(runStart, run.startIndex, lastFont);
                        runStart = run.startIndex;
                        lastFont = richTextRuns.get(i).font;
                    }
                }

                log.debug("Finalising with ", runStart, " - ", lastString.length());
                rich.applyFont(runStart, lastString.length(), lastFont);

                setCellContents(cell, rich);
            } else {
                setCellContents(cell, lastString);
            }

            if (lastString.contains("\n")) {
                if (!CSSConstants.CSS_NOWRAP_VALUE.equals(lastElement.getStyle().getWhiteSpace())) {
                    birtCellStyle.setProperty(StyleConstants.STYLE_WHITE_SPACE,
                            new StringValue(StringValue.CSS_STRING, CSSConstants.CSS_PRE_VALUE));
                }
            }
            if (!richTextRuns.isEmpty()) {
                birtCellStyle.setProperty(StyleConstants.STYLE_VERTICAL_ALIGN,
                        new StringValue(StringValue.CSS_STRING, CSSConstants.CSS_TOP_VALUE));
            }
            if (preferredAlignment != null) {
                birtCellStyle.setProperty(StyleConstants.STYLE_TEXT_ALIGN, preferredAlignment);
            }

        } else {
            setCellContents(cell, lastValue);
        }
    }

    int colIndex = cell.getColumnIndex();
    state.getSmu().applyAreaBordersToCell(state.areaBorders, cell, birtCellStyle, state.rowNum, colIndex);

    if ((birtCell != null) && ((birtCell.getColSpan() > 1) || (birtCell.getRowSpan() > 1))) {
        AreaBorders mergedRegionBorders = AreaBorders.createForMergedCells(
                state.rowNum + birtCell.getRowSpan() - 1, colIndex, colIndex + birtCell.getColSpan() - 1,
                state.rowNum, birtCellStyle);
        if (mergedRegionBorders != null) {
            state.insertBorderOverload(mergedRegionBorders);
        }
    }

    String customNumberFormat = EmitterServices.stringOption(state.getRenderOptions(), element,
            ExcelEmitter.CUSTOM_NUMBER_FORMAT, null);
    if (customNumberFormat != null) {
        StyleManagerUtils.setNumberFormat(birtCellStyle, ExcelEmitter.CUSTOM_NUMBER_FORMAT + customNumberFormat,
                null);
    }

    setCellStyle(sm, cell, birtCellStyle, lastValue);

    // Excel auto calculates the row height (if it isn't specified) as long as the cell isn't merged - if it is merged I have to do it
    if (((colSpan > 1) || (state.rowHasSpans(state.rowNum)))
            && ((lastValue instanceof String) || (lastValue instanceof RichTextString))) {
        int spannedRowAlgorithm = EmitterServices.integerOption(state.getRenderOptions(), element,
                ExcelEmitter.SPANNED_ROW_HEIGHT, ExcelEmitter.SPANNED_ROW_HEIGHT_SPREAD);
        Font defaultFont = state.getWb().getFontAt(cell.getCellStyle().getFontIndex());
        double cellWidth = spanWidthMillimetres(state.currentSheet, cell.getColumnIndex(),
                cell.getColumnIndex() + colSpan - 1);
        float cellDesiredHeight = smu.calculateTextHeightPoints(cell.getStringCellValue(), defaultFont,
                cellWidth, richTextRuns);
        if (cellDesiredHeight > state.requiredRowHeightInPoints) {
            int rowSpan = birtCell.getRowSpan();
            if (rowSpan < 2) {
                state.requiredRowHeightInPoints = cellDesiredHeight;
            } else {
                switch (spannedRowAlgorithm) {
                case ExcelEmitter.SPANNED_ROW_HEIGHT_FIRST:
                    state.requiredRowHeightInPoints = cellDesiredHeight;
                    break;
                case ExcelEmitter.SPANNED_ROW_HEIGHT_IGNORED:
                    break;
                default:
                    if (area != null) {
                        area.setHeight(cellDesiredHeight);
                    }
                }
            }
        }
    }

    // Adjust the required row height for any relevant areas based on what's left 
    float rowSpanHeightRequirement = state.calculateRowSpanHeightRequirement(state.rowNum);
    if (rowSpanHeightRequirement > state.requiredRowHeightInPoints) {
        state.requiredRowHeightInPoints = rowSpanHeightRequirement;
    }

    if (EmitterServices.booleanOption(state.getRenderOptions(), element, ExcelEmitter.FREEZE_PANES, false)) {
        if (state.currentSheet.getPaneInformation() == null) {
            state.currentSheet.createFreezePane(state.colNum, state.rowNum);
        }
    }

    lastValue = null;
    lastElement = null;
    richTextRuns.clear();
}

From source file:uk.co.spudsoft.birt.emitters.excel.tests.CellRangeTester.java

License:Open Source License

protected void validateCellRange(Matcher matcher, Cell cell) {
    int desiredR1 = Integer.parseInt(matcher.group(1));
    int desiredC1 = Integer.parseInt(matcher.group(2));
    int desiredR2 = Integer.parseInt(matcher.group(3));
    int desiredC2 = Integer.parseInt(matcher.group(4));

    int actualR1 = cell.getRowIndex() + 1;
    int actualC1 = cell.getColumnIndex() + 1;
    int actualR2 = actualR1;
    int actualC2 = actualC1;

    for (int i = 0; i < cell.getSheet().getNumMergedRegions(); ++i) {
        CellRangeAddress cra = cell.getSheet().getMergedRegion(i);
        if ((cra.getFirstRow() == cell.getRowIndex()) && (cra.getFirstColumn() == cell.getColumnIndex())) {
            assertEquals(actualR1, actualR2);
            assertEquals(actualC1, actualC2);
            actualR2 = cra.getLastRow() + 1;
            actualC2 = cra.getLastColumn() + 1;
        }//from   w w w  .jav  a  2  s  . c  om
    }
    assertEquals(desiredR1, actualR1);
    assertEquals(desiredC1, actualC1);
    assertEquals(desiredR2, actualR2);
    assertEquals(desiredC2, actualC2);
}