Example usage for org.apache.poi.hssf.usermodel HSSFCellStyle getFont

List of usage examples for org.apache.poi.hssf.usermodel HSSFCellStyle getFont

Introduction

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

Prototype

public HSSFFont getFont(org.apache.poi.ss.usermodel.Workbook parentWorkbook) 

Source Link

Document

gets the font for this style

Usage

From source file:cc.landking.converter.office.excel.HSSFHtmlHelper.java

License:Apache License

public void colorStyles(CellStyle style, Formatter out) {
    HSSFCellStyle cs = (HSSFCellStyle) style;
    out.format("  /* fill pattern = %d */%n", cs.getFillPattern());
    styleColor(out, "background-color", cs.getFillForegroundColor());
    styleColor(out, "color", cs.getFont(wb).getColor());
    styleColor(out, "border-left-color", cs.getLeftBorderColor());
    styleColor(out, "border-right-color", cs.getRightBorderColor());
    styleColor(out, "border-top-color", cs.getTopBorderColor());
    styleColor(out, "border-bottom-color", cs.getBottomBorderColor());
}

From source file:cn.trymore.core.util.excel.PoiExcelParser.java

License:Open Source License

public String getCellFontStyle(HSSFCell cell) {
    HSSFCellStyle style = cell.getCellStyle();
    StringBuilder sb = new StringBuilder();
    if ((cell != null) && (style != null)) {
        HSSFFont font = style.getFont(this.book);

        sb.append(new StringBuilder().append("font-size:").append(font.getFontHeightInPoints()).append("pt;")
                .toString());//  w  ww. j a v  a2 s  .  co m
        sb.append(
                new StringBuilder().append("font-weight:").append(font.getBoldweight()).append(";").toString());

        if (tripleToRGBString(font.getColor()) != null) {
            sb.append(new StringBuilder().append("color:").append(tripleToRGBString(font.getColor()))
                    .append(";").toString());
        }

        sb.append("font-family:tahoma;");

        if (font.getItalic()) {
            sb.append("font-style:italic;");
        }
        return sb.toString();
    }
    return "";
}

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

License:Apache License

@Override
public void apply() {
    for (DataObject dataObject : data) {
        HSSFCell templateCell = dataObject.templateCell;
        HSSFCell resultCell = dataObject.resultCell;
        BandData bandData = dataObject.bandData;

        HSSFWorkbook resultWorkbook = resultCell.getSheet().getWorkbook();
        HSSFWorkbook templateWorkbook = templateCell.getSheet().getWorkbook();

        String templateCellValue = templateCell.getStringCellValue();

        Matcher matcher = pattern.matcher(templateCellValue);
        if (matcher.find()) {
            String paramName = matcher.group(1);
            String styleName = (String) bandData.getParameterValue(paramName);
            if (styleName == null)
                continue;

            HSSFCellStyle cellStyle = styleCache.getStyleByName(styleName);
            if (cellStyle == null)
                continue;

            HSSFCellStyle resultStyle = styleCache.getNamedCachedStyle(cellStyle);

            if (resultStyle == null) {
                HSSFCellStyle newStyle = resultWorkbook.createCellStyle();
                // color
                newStyle.setFillBackgroundColor(cellStyle.getFillBackgroundColor());
                newStyle.setFillForegroundColor(cellStyle.getFillForegroundColor());
                newStyle.setFillPattern(cellStyle.getFillPattern());

                // borders
                newStyle.setBorderLeft(cellStyle.getBorderLeft());
                newStyle.setBorderRight(cellStyle.getBorderRight());
                newStyle.setBorderTop(cellStyle.getBorderTop());
                newStyle.setBorderBottom(cellStyle.getBorderBottom());

                // border colors
                newStyle.setLeftBorderColor(cellStyle.getLeftBorderColor());
                newStyle.setRightBorderColor(cellStyle.getRightBorderColor());
                newStyle.setBottomBorderColor(cellStyle.getBottomBorderColor());
                newStyle.setTopBorderColor(cellStyle.getTopBorderColor());

                // alignment
                newStyle.setAlignment(cellStyle.getAlignment());
                newStyle.setVerticalAlignment(cellStyle.getVerticalAlignment());
                // misc
                DataFormat dataFormat = resultWorkbook.getCreationHelper().createDataFormat();
                newStyle.setDataFormat(dataFormat.getFormat(cellStyle.getDataFormatString()));
                newStyle.setHidden(cellStyle.getHidden());
                newStyle.setLocked(cellStyle.getLocked());
                newStyle.setIndention(cellStyle.getIndention());
                newStyle.setRotation(cellStyle.getRotation());
                newStyle.setWrapText(cellStyle.getWrapText());
                // font
                HSSFFont cellFont = cellStyle.getFont(templateWorkbook);
                HSSFFont newFont = fontCache.getFontByTemplate(cellFont);

                if (newFont == null) {
                    newFont = resultWorkbook.createFont();

                    newFont.setFontName(cellFont.getFontName());
                    newFont.setItalic(cellFont.getItalic());
                    newFont.setStrikeout(cellFont.getStrikeout());
                    newFont.setTypeOffset(cellFont.getTypeOffset());
                    newFont.setBoldweight(cellFont.getBoldweight());
                    newFont.setCharSet(cellFont.getCharSet());
                    newFont.setColor(cellFont.getColor());
                    newFont.setUnderline(cellFont.getUnderline());
                    newFont.setFontHeight(cellFont.getFontHeight());
                    newFont.setFontHeightInPoints(cellFont.getFontHeightInPoints());
                    fontCache.addCachedFont(cellFont, newFont);
                }//from  w w  w . j a v  a  2 s . c  o m
                newStyle.setFont(newFont);

                resultStyle = newStyle;
                styleCache.addCachedNamedStyle(cellStyle, resultStyle);
            }

            fixNeighbourCellBorders(cellStyle, resultCell);

            resultCell.setCellStyle(resultStyle);

            Sheet sheet = resultCell.getSheet();
            for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
                CellRangeAddress mergedRegion = sheet.getMergedRegion(i);
                if (mergedRegion.isInRange(resultCell.getRowIndex(), resultCell.getColumnIndex())) {

                    int firstRow = mergedRegion.getFirstRow();
                    int lastRow = mergedRegion.getLastRow();
                    int firstCol = mergedRegion.getFirstColumn();
                    int lastCol = mergedRegion.getLastColumn();

                    for (int row = firstRow; row <= lastRow; row++)
                        for (int col = firstCol; col <= lastCol; col++)
                            sheet.getRow(row).getCell(col).setCellStyle(resultStyle);

                    // cell includes only in one merged region
                    break;
                }
            }
        }
    }
}

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

License:Apache License

protected HSSFCellStyle copyCellStyle(HSSFCellStyle templateStyle) {
    HSSFCellStyle style = styleCache.getCellStyleByTemplate(templateStyle);

    if (style == null) {
        HSSFCellStyle newStyle = resultWorkbook.createCellStyle();

        XslStyleHelper.cloneStyleRelations(templateStyle, newStyle);
        HSSFFont templateFont = templateStyle.getFont(templateWorkbook);
        HSSFFont font = fontCache.getFontByTemplate(templateFont);
        if (font != null)
            newStyle.setFont(font);//from www  .  java  2 s .  c om
        else {
            XslStyleHelper.cloneFont(templateStyle, newStyle);
            fontCache.addCachedFont(templateFont, newStyle.getFont(resultWorkbook));
        }
        styleCache.addCachedStyle(templateStyle, newStyle);
        style = newStyle;
    }

    return style;
}

From source file:com.report.excel.ExcelToHtmlConverter.java

License:Apache License

protected String buildStyle(HSSFWorkbook workbook, HSSFCellStyle cellStyle) {
    StringBuilder style = new StringBuilder();

    style.append("white-space:pre-wrap;");
    ExcelToHtmlUtils.appendAlign(style, cellStyle.getAlignment());

    if (cellStyle.getFillPattern() == 0) {
        // no fill
    } else if (cellStyle.getFillPattern() == 1) {
        final HSSFColor foregroundColor = cellStyle.getFillForegroundColorColor();
        if (foregroundColor != null)
            style.append("background-color:" + ExcelToHtmlUtils.getColor(foregroundColor) + ";");
    } else {//from   w ww  .jav a  2 s  .  co m
        final HSSFColor backgroundColor = cellStyle.getFillBackgroundColorColor();
        if (backgroundColor != null)
            style.append("background-color:" + ExcelToHtmlUtils.getColor(backgroundColor) + ";");
    }

    buildStyle_border(workbook, style, "top", cellStyle.getBorderTop(), cellStyle.getTopBorderColor());
    buildStyle_border(workbook, style, "right", cellStyle.getBorderRight(), cellStyle.getRightBorderColor());
    buildStyle_border(workbook, style, "bottom", cellStyle.getBorderBottom(), cellStyle.getBottomBorderColor());
    buildStyle_border(workbook, style, "left", cellStyle.getBorderLeft(), cellStyle.getLeftBorderColor());

    HSSFFont font = cellStyle.getFont(workbook);
    buildStyle_font(workbook, style, font);

    return style.toString();
}

From source file:com.vaadin.addon.spreadsheet.HSSFColorConverter.java

License:Open Source License

@Override
public void colorStyles(final CellStyle cellStyle, final StringBuilder sb) {
    HSSFCellStyle cs = (HSSFCellStyle) cellStyle;
    // TODO Fill pattern not supported
    // out.format("  /* fill pattern = %d */%n", cs.getFillPattern());
    short fillForegroundColor = cs.getFillForegroundColor();
    short fillBackgroundColor = cs.getFillBackgroundColor();

    String backgroundColor = null;
    HSSFColor fillForegroundColorColor = cs.getFillForegroundColorColor();
    if (fillForegroundColorColor != null && fillForegroundColor != HSSFColor.AUTOMATIC.index) {
        backgroundColor = styleColor(fillForegroundColor);
    } else {/*from   w ww.j  a v  a  2s .co m*/
        HSSFColor fillBackgroundColorColor = cs.getFillBackgroundColorColor();
        if (fillBackgroundColorColor != null && fillBackgroundColor != HSSFColor.AUTOMATIC.index) {
            backgroundColor = styleColor(fillBackgroundColor);
        }
    }
    if (backgroundColor != null && !backgroundColor.equals(defaultBackgroundColor)) {
        sb.append("background-color:");
        sb.append(backgroundColor);
    }

    String color = styleColor(cs.getFont(wb).getColor());
    if (color != null && !color.equals(defaultColor)) {
        sb.append("color:");
        sb.append(color);
    }

}

From source file:com.vaadin.addon.spreadsheet.HSSFColorConverter.java

License:Open Source License

@Override
public void defaultColorStyles(CellStyle cellStyle, StringBuilder sb) {
    HSSFCellStyle cs = (HSSFCellStyle) cellStyle;
    defaultBackgroundColor = styleColor(cs.getFillBackgroundColor());
    if (defaultBackgroundColor == null) {
        defaultBackgroundColor = "#ffffff;";
    }//from  w ww.jav  a  2s . c  o  m
    sb.append("background-color:");
    sb.append(defaultBackgroundColor);
    defaultColor = styleColor(cs.getFont(wb).getColor());
    if (defaultColor == null) {
        defaultColor = "#000000;";
    }
    sb.append("color:");
    sb.append(defaultColor);
}

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

License:Apache License

protected String buildStyle(HSSFWorkbook workbook, HSSFCellStyle cellStyle) {
    StringBuffer style = new StringBuffer();

    style.append("white-space:pre-wrap;");
    ExcelToHtmlUtils.appendAlign(style, cellStyle.getAlignment());

    if (cellStyle.getFillPattern() == 0) {
        // no fill
    } else if (cellStyle.getFillPattern() == 1) {
        final HSSFColor foregroundColor = cellStyle.getFillForegroundColorColor();
        if (foregroundColor != null) {
            style.append("background-color:" + AbstractExcelUtils.getColor(foregroundColor) + ";");
        }//from   w w  w . j a  va  2s .co m
    } else {
        final HSSFColor backgroundColor = cellStyle.getFillBackgroundColorColor();
        if (backgroundColor != null) {
            style.append("background-color:" + AbstractExcelUtils.getColor(backgroundColor) + ";");
        }
    }

    this.buildStyle_border(workbook, style, "top", cellStyle.getBorderTop(), cellStyle.getTopBorderColor());
    this.buildStyle_border(workbook, style, "right", cellStyle.getBorderRight(),
            cellStyle.getRightBorderColor());
    this.buildStyle_border(workbook, style, "bottom", cellStyle.getBorderBottom(),
            cellStyle.getBottomBorderColor());
    this.buildStyle_border(workbook, style, "left", cellStyle.getBorderLeft(), cellStyle.getLeftBorderColor());

    HSSFFont font = cellStyle.getFont(workbook);
    this.buildStyle_font(workbook, style, font);

    return style.toString();
}

From source file:egovframework.rte.fdl.excel.EgovExcelSXSSFServiceTest.java

License:Apache License

/**
 * [Flow #-6]  ?  :  ? ?(?, ? )? /*from   w  w  w. jav  a 2 s  . co  m*/
 */
@Test
public void testModifyCellAttribute() throws Exception {

    try {
        log.debug("testModifyCellAttribute start....");

        StringBuffer sb = new StringBuffer();
        sb.append(fileLocation).append("/").append("testModifyCellAttribute.xls");

        if (EgovFileUtil.isExistsFile(sb.toString())) {
            EgovFileUtil.delete(new File(sb.toString()));

            log.debug("Delete file...." + sb.toString());
        }

        HSSFWorkbook wbTmp = new HSSFWorkbook();
        wbTmp.createSheet();

        //  ? ?
        excelService.createWorkbook(wbTmp, sb.toString());

        //  ? 
        HSSFWorkbook wb = excelService.loadWorkbook(sb.toString());
        log.debug("testModifyCellAttribute after loadWorkbook....");

        HSSFSheet sheet = wb.createSheet("cell test sheet2");
        //           sheet.setColumnWidth((short) 3, (short) 200);   // column Width

        HSSFCellStyle cs = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setFontHeight((short) 16);
        font.setBoldweight((short) 3);
        font.setFontName("fixedsys");

        cs.setFont(font);
        cs.setAlignment(HSSFCellStyle.ALIGN_RIGHT); // cell 
        cs.setWrapText(true);

        for (int i = 0; i < 100; i++) {
            HSSFRow row = sheet.createRow(i);
            //              row.setHeight((short)300); // row? height 

            for (int j = 0; j < 5; j++) {
                HSSFCell cell = row.createCell(j);
                cell.setCellValue(new HSSFRichTextString("row " + i + ", cell " + j));
                cell.setCellStyle(cs);
            }
        }

        //  ? 
        FileOutputStream out = new FileOutputStream(sb.toString());
        wb.write(out);
        out.close();

        //////////////////////////////////////////////////////////////////////////
        // ?
        HSSFWorkbook wbT = excelService.loadWorkbook(sb.toString());
        HSSFSheet sheetT = wbT.getSheet("cell test sheet2");
        log.debug("getNumCellStyles : " + wbT.getNumCellStyles());

        HSSFCellStyle cs1 = wbT.getCellStyleAt((short) (wbT.getNumCellStyles() - 1));

        HSSFFont fontT = cs1.getFont(wbT);
        log.debug("font getFontHeight : " + fontT.getFontHeight());
        log.debug("font getBoldweight : " + fontT.getBoldweight());
        log.debug("font getFontName : " + fontT.getFontName());
        log.debug("getAlignment : " + cs1.getAlignment());
        log.debug("getWrapText : " + cs1.getWrapText());

        for (int i = 0; i < 100; i++) {
            HSSFRow row1 = sheetT.getRow(i);
            for (int j = 0; j < 5; j++) {
                HSSFCell cell1 = row1.getCell(j);
                log.debug("row " + i + ", cell " + j + " : " + cell1.getRichStringCellValue());
                assertEquals(16, fontT.getFontHeight());
                assertEquals(3, fontT.getBoldweight());
                assertEquals(HSSFCellStyle.ALIGN_RIGHT, cs1.getAlignment());
                assertTrue(cs1.getWrapText());
            }
        }

    } catch (Exception e) {
        log.error(e.toString());
        throw new Exception(e);
    } finally {
        log.debug("testModifyCellAttribute end....");
    }
}

From source file:es.jamisoft.comun.io.excel.ExcelGenerator.java

License:Apache License

/**
 * Este mtodo gestiona el estilo que se proporcionar a las cabeceras.
 *
 * @param wb Objeto Excel.//from www . j av a2 s  . co  m
 */
private void initStylesHeader(HSSFWorkbook wb) {

    // create instance of HSSFCellStyle
    HSSFCellStyle headerStyle = wb.createCellStyle();

    // Create Font Header
    createFontCell(wb, headerStyle, ep.getFillFontColorHeader());
    headerStyle.getFont(wb).setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

    // Create style of header cell
    HSSFPalette palette = wb.getCustomPalette();
    HSSFColor colorMapfreHSSF = palette.findColor((byte) ep.getColorMapfre().getRed(),
            (byte) ep.getColorMapfre().getGreen(), (byte) ep.getColorMapfre().getBlue());

    if (colorMapfreHSSF == null) {
        palette.setColorAtIndex(HSSFColor.LAVENDER.index, (byte) ep.getColorMapfre().getRed(),
                (byte) ep.getColorMapfre().getGreen(), (byte) ep.getColorMapfre().getBlue());
        colorMapfreHSSF = palette.getColor(HSSFColor.LAVENDER.index);
    }

    short fillBGColor = colorMapfreHSSF.getIndex();

    headerStyle.setFillForegroundColor(fillBGColor);
    headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    // create border of header cell
    createBorderCells(headerStyle);

    // set
    ep.setHeaderStyle(headerStyle);
}