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

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

Introduction

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

Prototype

CellStyle getCellStyle();

Source Link

Document

Return the cell's style.

Usage

From source file:uk.ac.liverpool.spreadsheet.ToXML.java

License:Apache License

private void printSheetContent(Sheet sheet) {
    ensureColumnBounds(sheet);//from   w  w w .  j a  v  a2 s.  c om
    printColumnHeads();

    cellsToFormula = new HashMap<String, List<String>>();
    cellToFormulaConverted = new HashMap<String, String>();
    crToParent = new HashMap<String, List<String>>();
    FormulaParsingWorkbook fpwb;
    FormulaRenderingWorkbook frwb;
    if (xswb != null) {
        XSSFEvaluationWorkbook w = XSSFEvaluationWorkbook.create(xswb);
        frwb = w;
        fpwb = w;
    } else if (hswb != null) {
        HSSFEvaluationWorkbook w = HSSFEvaluationWorkbook.create(hswb);
        frwb = w;
        fpwb = w;
    }

    else
        return;
    // first we need to determine all the dependencies ofr each formula
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        Row row = rows.next();
        for (int i = firstColumn; i < endColumn; i++) {
            if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
                Cell cell = row.getCell(i);
                if (cell != null) {
                    if (cell.getCellType() == Cell.CELL_TYPE_FORMULA)
                        try {
                            parseFormula(cell, fpwb, frwb);

                        } catch (Exception x) {

                        }
                }
            }
        }
    }
    rows = sheet.rowIterator();

    while (rows.hasNext()) {
        Row row = rows.next();
        int rowNumber = row.getRowNum() + 1;
        out.format("  <TableRow>%n");
        out.format("    <RowHeader>%d</RowHeader>%n", rowNumber);
        out.format("  <TableCells>%n");
        for (int i = firstColumn; i < endColumn; i++) {
            String content = "0";
            String attrs = "";
            CellStyle style = null;
            String valueType = "float";
            Cell cell = row.getCell(i);
            CellReference c = new CellReference(rowNumber - 1, i);
            attrs += " cellID=\"." + c.formatAsString() + "\"";

            String cr = c.formatAsString();
            // if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {

            if (cell != null && cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
                attrs += " readOnly=\"readOnly\"";
                try {
                    attrs += " cellFormula=\"" + StringEscapeUtils.escapeXml(cell.getCellFormula()) + "\"";
                } catch (Exception x) {
                    attrs += " cellFormula=\"FORMULA ERROR\"";
                }
            } else {
                List<String> cfrl = cellsToFormula.get(cr);
                StringBuffer formula = new StringBuffer("");

                if (cfrl != null) {
                    List<String> refs = new LinkedList<String>();
                    visit(cfrl, refs);
                    System.out.println(refs);
                    cleanup(refs);
                    for (String s : refs) {
                        formula.append(StringEscapeUtils.escapeXml(cellToFormulaConverted.get(s)));
                        formula.append(" || ");
                    }
                }
                if (formula.length() > 0)
                    attrs += " formula=\"" + formula.substring(0, formula.length() - 4) + "\"";
            }
            if (cell != null) {
                style = cell.getCellStyle();
                // Set the value that is rendered for the cell
                // also applies the format

                try {
                    CellFormat cf = CellFormat.getInstance(style.getDataFormatString());
                    CellFormatResult result = cf.apply(cell);
                    content = result.text;
                } catch (Exception x) {
                    content = "DATA FORMULA ERROR ";
                }

            }
            // }
            attrs += " value_type=\"" + valueType + "\"";
            attrs += " value=\"" + StringEscapeUtils.escapeXml(content) + "\"";
            out.format("    <TableCell  %s>%s</TableCell>%n", // class=%s
                    // styleName(style),
                    attrs, StringEscapeUtils.escapeXml(content));
        }
        out.format(" </TableCells> </TableRow>%n%n");
    }
}

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

License:Apache License

@Override
public void renderCell(Element element, int rowIndex, int columnIndex) {
    Cell cell = sheet.getRow(rowIndex).createCell(columnIndex);

    Double numericValue;//w ww  .  j  a  va2s  .  co m

    if (isDateCell(element)) {
        DateFormat df = new SimpleDateFormat(getDateCellFormat(element));

        try {
            cell.setCellValue(df.parse(getElementText(element)));
        } catch (ParseException pex) {
            System.out.println("Invalid Usage");
        }
    } else if ((numericValue = getNumericValue(element)) != null) {
        cell.setCellValue(numericValue);
    } else {
        cell = sheet.getRow(rowIndex).createCell(columnIndex, Cell.CELL_TYPE_STRING);
        cell.setCellValue(getElementText(element));
    }

    Style style = styleMapper.getStyleForElement(element);
    cell.setCellStyle(styleGenerator.getStyle(cell, style));

    if (isDateCell(element)) {
        CreationHelper createHelper = sheet.getWorkbook().getCreationHelper();
        cell.getCellStyle()
                .setDataFormat(createHelper.createDataFormat().getFormat(getDateCellFormat(element)));
    }

    String commentText;

    if ((commentText = getCellCommentText(element)) != null) {
        ExcelCellCommentGenerator.addCellComment(cell, commentText, getCellCommentDimension(element));
    }

    if (definesFreezePane(element)) {
        sheet.createFreezePane(columnIndex, rowIndex);
    }
}

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

License:Apache License

public void doMerge(int rowIndex, int columnIndex, int rowSpan, int columnSpan) {
    Cell cell = sheet.getRow(rowIndex).getCell(columnIndex);
    CellRangeAddress range = new CellRangeAddress(rowIndex, rowIndex + rowSpan - 1, columnIndex,
            columnIndex + columnSpan - 1);

    sheet.addMergedRegion(range);//from   ww  w.  ja v  a  2  s  .c  o  m

    RegionUtil.setBorderBottom(cell.getCellStyle().getBorderBottom(), range, sheet, sheet.getWorkbook());
    RegionUtil.setBorderTop(cell.getCellStyle().getBorderTop(), range, sheet, sheet.getWorkbook());
    RegionUtil.setBorderLeft(cell.getCellStyle().getBorderLeft(), range, sheet, sheet.getWorkbook());
    RegionUtil.setBorderRight(cell.getCellStyle().getBorderRight(), range, sheet, sheet.getWorkbook());

    RegionUtil.setBottomBorderColor(cell.getCellStyle().getBottomBorderColor(), range, sheet,
            sheet.getWorkbook());
    RegionUtil.setTopBorderColor(cell.getCellStyle().getTopBorderColor(), range, sheet, sheet.getWorkbook());
    RegionUtil.setLeftBorderColor(cell.getCellStyle().getLeftBorderColor(), range, sheet, sheet.getWorkbook());
    RegionUtil.setRightBorderColor(cell.getCellStyle().getRightBorderColor(), range, sheet,
            sheet.getWorkbook());
}

From source file:uk.co.certait.test.ExcelToHtmlConverter.java

License:Apache License

public void printStyles() {
    ensureOut();/*from   w w  w  .j  ava 2 s .  c  o  m*/

    // First, copy the base css
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/excelStyle.css")));
        String line;
        while ((line = in.readLine()) != null) {
            out.format("%s%n", line);
        }
    } catch (IOException e) {
        throw new IllegalStateException("Reading standard css", e);
    } finally {
        IOUtils.closeQuietly(in);
    }

    // now add css for each used style
    Set<CellStyle> seen = new HashSet<CellStyle>();
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);
        Iterator<Row> rows = sheet.rowIterator();
        while (rows.hasNext()) {
            Row row = rows.next();
            for (Cell cell : row) {
                CellStyle style = cell.getCellStyle();
                if (!seen.contains(style)) {
                    printStyle(style);
                    seen.add(style);
                }
            }
        }
    }
}

From source file:uk.co.certait.test.ExcelToHtmlConverter.java

License:Apache License

private void printSheetContent(Sheet sheet) {
    printColumnHeads();/*w  ww. ja  v a 2 s. c om*/

    out.format("<tbody>%n");
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        Row row = rows.next();

        out.format("  <tr>%n");
        out.format("    <td class=%s>%d</td>%n", ROW_HEAD_CLASS, row.getRowNum() + 1);
        for (int i = firstColumn; i < endColumn; i++) {
            String content = "&nbsp;";
            String attrs = "";
            CellStyle style = null;
            if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
                Cell cell = row.getCell(i);
                if (cell != null) {
                    style = cell.getCellStyle();
                    attrs = tagStyle(cell, style);
                    // Set the value that is rendered for the cell
                    // also applies the format
                    CellFormat cf = CellFormat.getInstance(style.getDataFormatString());
                    CellFormatResult result = cf.apply(cell);
                    content = result.text;
                    if (content.equals("")) {
                        content = "&nbsp;";
                    }
                }
            }
            out.format("    <td class=%s %s>%s</td>%n", styleName(style), attrs, content);
        }
        out.format("  </tr>%n");
    }
    out.format("</tbody>%n");
}

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  www.j av  a2 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.StyleManagerUtils.java

License:Open Source License

/**
 * Check whether a cell is empty and unformatted.
 * @param cell//from ww  w .j  a  v a2 s.  com
 * The cell to consider.
 * @return
 * true is the cell is empty and has no style or has no background fill.
 */
public static boolean cellIsEmpty(Cell cell) {
    if (cell.getCellType() != Cell.CELL_TYPE_BLANK) {
        return false;
    }
    CellStyle cellStyle = cell.getCellStyle();
    if (cellStyle == null) {
        return true;
    }
    if (cellStyle.getFillPattern() == CellStyle.NO_FILL) {
        return true;
    }
    return false;
}

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

License:Open Source License

/**
 * Place a border around a region on the current sheet.
 * This is used to apply borders to entire rows or entire tables.
 * @param colStart// w  w  w  .j  a  v a 2  s  .co  m
 * The column marking the left-side boundary of the region.
 * @param colEnd
 * The column marking the right-side boundary of the region.
 * @param rowStart
 * The row marking the top boundary of the region.
 * @param rowEnd
 * The row marking the bottom boundary of the region.
 * @param borderStyle
 * The BIRT border style to apply to the region.
 */
public void applyBordersToArea(StyleManager sm, Sheet sheet, int colStart, int colEnd, int rowStart, int rowEnd,
        BirtStyle borderStyle) {
    StringBuilder borderMsg = new StringBuilder();
    borderMsg.append("applyBordersToArea [").append(colStart).append(",").append(rowStart).append("]-[")
            .append(colEnd).append(",").append(rowEnd).append("]");

    CSSValue borderStyleBottom = borderStyle.getProperty(StyleConstants.STYLE_BORDER_BOTTOM_STYLE);
    CSSValue borderWidthBottom = borderStyle.getProperty(StyleConstants.STYLE_BORDER_BOTTOM_WIDTH);
    CSSValue borderColourBottom = borderStyle.getProperty(StyleConstants.STYLE_BORDER_BOTTOM_COLOR);
    CSSValue borderStyleLeft = borderStyle.getProperty(StyleConstants.STYLE_BORDER_LEFT_STYLE);
    CSSValue borderWidthLeft = borderStyle.getProperty(StyleConstants.STYLE_BORDER_LEFT_WIDTH);
    CSSValue borderColourLeft = borderStyle.getProperty(StyleConstants.STYLE_BORDER_LEFT_COLOR);
    CSSValue borderStyleRight = borderStyle.getProperty(StyleConstants.STYLE_BORDER_RIGHT_STYLE);
    CSSValue borderWidthRight = borderStyle.getProperty(StyleConstants.STYLE_BORDER_RIGHT_WIDTH);
    CSSValue borderColourRight = borderStyle.getProperty(StyleConstants.STYLE_BORDER_RIGHT_COLOR);
    CSSValue borderStyleTop = borderStyle.getProperty(StyleConstants.STYLE_BORDER_TOP_STYLE);
    CSSValue borderWidthTop = borderStyle.getProperty(StyleConstants.STYLE_BORDER_TOP_WIDTH);
    CSSValue borderColourTop = borderStyle.getProperty(StyleConstants.STYLE_BORDER_TOP_COLOR);

    /*      borderMsg.append( ", Bottom:" ).append( borderStyleBottom ).append( "/" ).append( borderWidthBottom ).append( "/" + borderColourBottom );
          borderMsg.append( ", Left:" ).append( borderStyleLeft ).append( "/" ).append( borderWidthLeft ).append( "/" + borderColourLeft );
          borderMsg.append( ", Right:" ).append( borderStyleRight ).append( "/" ).append( borderWidthRight ).append( "/" ).append( borderColourRight );
          borderMsg.append( ", Top:" ).append( borderStyleTop ).append( "/" ).append( borderWidthTop ).append( "/" ).append( borderColourTop );
          log.debug( borderMsg.toString() );
    */
    if ((borderStyleBottom == null) || (CSSConstants.CSS_NONE_VALUE.equals(borderStyleBottom))
            || (borderWidthBottom == null) || ("0".equals(borderWidthBottom)) || (borderColourBottom == null)
            || (CSSConstants.CSS_TRANSPARENT_VALUE.equals(borderColourBottom.getCssText()))) {
        borderStyleBottom = null;
        borderWidthBottom = null;
        borderColourBottom = null;
    }

    if ((borderStyleLeft == null) || (CSSConstants.CSS_NONE_VALUE.equals(borderStyleLeft))
            || (borderWidthLeft == null) || ("0".equals(borderWidthLeft)) || (borderColourLeft == null)
            || (CSSConstants.CSS_TRANSPARENT_VALUE.equals(borderColourLeft.getCssText()))) {
        borderStyleLeft = null;
        borderWidthLeft = null;
        borderColourLeft = null;
    }

    if ((borderStyleRight == null) || (CSSConstants.CSS_NONE_VALUE.equals(borderStyleRight))
            || (borderWidthRight == null) || ("0".equals(borderWidthRight)) || (borderColourRight == null)
            || (CSSConstants.CSS_TRANSPARENT_VALUE.equals(borderColourRight.getCssText()))) {
        borderStyleRight = null;
        borderWidthRight = null;
        borderColourRight = null;
    }

    if ((borderStyleTop == null) || (CSSConstants.CSS_NONE_VALUE.equals(borderStyleTop))
            || (borderWidthTop == null) || ("0".equals(borderWidthTop)) || (borderColourTop == null)
            || (CSSConstants.CSS_TRANSPARENT_VALUE.equals(borderColourTop.getCssText()))) {
        borderStyleTop = null;
        borderWidthTop = null;
        borderColourTop = null;
    }

    if ((borderStyleBottom != null) || (borderWidthBottom != null) || (borderColourBottom != null)
            || (borderStyleLeft != null) || (borderWidthLeft != null) || (borderColourLeft != null)
            || (borderStyleRight != null) || (borderWidthRight != null) || (borderColourRight != null)
            || (borderStyleTop != null) || (borderWidthTop != null) || (borderColourTop != null)) {
        for (int row = rowStart; row <= rowEnd; ++row) {
            Row styleRow = sheet.getRow(row);
            if (styleRow != null) {
                for (int col = colStart; col <= colEnd; ++col) {
                    if ((col == colStart) || (col == colEnd) || (row == rowStart) || (row == rowEnd)) {
                        Cell styleCell = styleRow.getCell(col);
                        if (styleCell == null) {
                            log.debug("Creating cell[", row, ",", col, "]");
                            styleCell = styleRow.createCell(col);
                        }
                        if (styleCell != null) {
                            // log.debug( "Applying border to cell [R" + styleCell.getRowIndex() + "C" + styleCell.getColumnIndex() + "]");
                            CellStyle newStyle = sm.getStyleWithBorders(styleCell.getCellStyle(),
                                    ((row == rowEnd) ? borderStyleBottom : null),
                                    ((row == rowEnd) ? borderWidthBottom : null),
                                    ((row == rowEnd) ? borderColourBottom : null),
                                    ((col == colStart) ? borderStyleLeft : null),
                                    ((col == colStart) ? borderWidthLeft : null),
                                    ((col == colStart) ? borderColourLeft : null),
                                    ((col == colEnd) ? borderStyleRight : null),
                                    ((col == colEnd) ? borderWidthRight : null),
                                    ((col == colEnd) ? borderColourRight : null),
                                    ((row == rowStart) ? borderStyleTop : null),
                                    ((row == rowStart) ? borderWidthTop : null),
                                    ((row == rowStart) ? borderColourTop : null));
                            styleCell.setCellStyle(newStyle);
                        }
                    }
                }
            }
        }
    }
}

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

License:Open Source License

/**
 * Place a border around a region on the current sheet.
 * This is used to apply borders to entire rows or entire tables.
 * @param colStart/*from w w w.  j a  va  2  s.  co  m*/
 * The column marking the left-side boundary of the region.
 * @param colEnd
 * The column marking the right-side boundary of the region.
 * @param row
 * The row to get a bottom border.
 * @param borderStyle
 * The BIRT border style to apply to the region.
 */
public void applyBottomBorderToRow(StyleManager sm, Sheet sheet, int colStart, int colEnd, int row,
        BirtStyle borderStyle) {
    CSSValue borderStyleBottom = borderStyle.getProperty(StyleConstants.STYLE_BORDER_BOTTOM_STYLE);
    CSSValue borderWidthBottom = borderStyle.getProperty(StyleConstants.STYLE_BORDER_BOTTOM_WIDTH);
    CSSValue borderColourBottom = borderStyle.getProperty(StyleConstants.STYLE_BORDER_BOTTOM_COLOR);

    if ((borderStyleBottom == null) || (CSSConstants.CSS_NONE_VALUE.equals(borderStyleBottom.getCssText()))
            || (borderWidthBottom == null) || ("0".equals(borderWidthBottom)) || (borderColourBottom == null)
            || (CSSConstants.CSS_TRANSPARENT_VALUE.equals(borderColourBottom.getCssText()))) {
        borderStyleBottom = null;
        borderWidthBottom = null;
        borderColourBottom = null;
    }

    if ((borderStyleBottom != null) || (borderWidthBottom != null) || (borderColourBottom != null)) {
        Row styleRow = sheet.getRow(row);
        if (styleRow != null) {
            for (int col = colStart; col <= colEnd; ++col) {
                Cell styleCell = styleRow.getCell(col);
                if (styleCell == null) {
                    styleCell = styleRow.createCell(col);
                }
                if (styleCell != null) {
                    // log.debug( "Applying border to cell [R" + styleCell.getRowIndex() + "C" + styleCell.getColumnIndex() + "]");
                    CellStyle newStyle = sm.getStyleWithBorders(styleCell.getCellStyle(), borderStyleBottom,
                            borderWidthBottom, borderColourBottom, null, null, null, null, null, null, null,
                            null, null);
                    styleCell.setCellStyle(newStyle);
                }
            }
        }
    }
}

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

License:Open Source License

public void extendRows(HandlerState state, int startRow, int startCol, int endRow, int endCol) {
    for (int colNum = startCol; colNum < endCol; ++colNum) {
        Cell lastCell = null;
        for (int rowNum = startRow; rowNum < endRow; ++rowNum) {
            Row row = state.currentSheet.getRow(rowNum);
            if (row != null) {
                Cell cell = row.getCell(colNum);
                if (cell != null) {
                    lastCell = cell;/*from  w  w w.ja v a  2s.  c o m*/
                }
            }
        }
        if ((lastCell != null) && (lastCell.getRowIndex() < endRow - 1)) {
            CellRangeAddress range = new CellRangeAddress(lastCell.getRowIndex(), endRow - 1,
                    lastCell.getColumnIndex(), lastCell.getColumnIndex());
            log.debug("Extend: merging from [", range.getFirstRow(), ",", range.getFirstColumn(), "] to [",
                    range.getLastRow(), ",", range.getLastColumn(), "]");
            state.currentSheet.addMergedRegion(range);
            for (int rowNum = lastCell.getRowIndex() + 1; rowNum < endRow; ++rowNum) {
                Row row = state.currentSheet.getRow(rowNum);
                if (row == null) {
                    log.error(0,
                            "Creating a row (for column " + colNum + "), this really shouldn't be necessary",
                            null);
                    row = state.currentSheet.createRow(rowNum);
                }
                Cell cell = row.createCell(colNum);
                cell.setCellStyle(lastCell.getCellStyle());
            }
        }
    }
}