Example usage for org.apache.poi.ss.usermodel CellStyle setFont

List of usage examples for org.apache.poi.ss.usermodel CellStyle setFont

Introduction

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

Prototype

void setFont(Font font);

Source Link

Document

set the font for this style

Usage

From source file:org.devgateway.eudevfin.sheetexp.poi.PoiObjectCreator.java

License:Open Source License

private CellStyle createBodyDateCellStyle() {
    final CellStyle style = this.workbook.createCellStyle();
    final Font font = this.workbook.createFont();
    style.setFont(font);

    final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, this.locale);
    final String pattern = ((SimpleDateFormat) df).toPattern();
    final HSSFDataFormat hssfDataFormat = this.workbook.createDataFormat();

    style.setDataFormat(hssfDataFormat.getFormat(pattern));

    return style;
}

From source file:org.devgateway.eudevfin.sheetexp.poi.PoiObjectCreator.java

License:Open Source License

private CellStyle createBodyStringCellStyle() {
    final CellStyle style = this.workbook.createCellStyle();
    final Font font = this.workbook.createFont();
    style.setFont(font);
    style.setWrapText(true);//  w  ww.  j  ava2 s. com
    return style;
}

From source file:org.devgateway.eudevfin.sheetexp.poi.PoiObjectCreator.java

License:Open Source License

private CellStyle createHeaderCellStyle() {
    final CellStyle style = this.workbook.createCellStyle();
    final Font font = this.workbook.createFont();
    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style.setFont(font);
    return style;
}

From source file:org.displaytag.render.XssfTableWriter.java

License:Artistic License

/**
 * @see org.displaytag.render.TableWriterTemplate#writeCaption(org.displaytag.model.TableModel)
 *//*from   w  w  w .j a v  a2  s.  c  om*/
protected void writeCaption(TableModel model) throws Exception {
    XSSFFont captionFont = wb.createFont();
    captionFont.setFontHeightInPoints((short) 14);
    captionFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
    captionFont.setBold(true);
    captionFont.setItalic(false);

    CellStyle captionstyle = this.wb.createCellStyle();
    captionstyle.setAlignment(CellStyle.ALIGN_CENTER);
    captionstyle.setFont(captionFont);

    this.colNum = 0;
    this.currentRow = this.sheet.createRow(this.rowNum++);
    this.currentCell = this.currentRow.createCell(this.colNum++);
    this.currentCell.setCellStyle(captionstyle);
    String caption = model.getCaption();
    this.currentCell.setCellValue(new XSSFRichTextString(caption));
    this.rowSpanTable(model);
}

From source file:org.eclipse.nebula.widgets.nattable.extension.poi.PoiExcelExporter.java

License:Open Source License

private CellStyle getExcelCellStyle(Color fg, Color bg, FontData fontData, String dataFormat, int hAlign,
        int vAlign, boolean vertical) {

    CellStyle xlCellStyle = xlCellStyles
            .get(new ExcelCellStyleAttributes(fg, bg, fontData, dataFormat, hAlign, vAlign, vertical));

    if (xlCellStyle == null) {
        xlCellStyle = xlWorkbook.createCellStyle();

        if (applyBackgroundColor) {
            // Note: xl fill foreground = background
            setFillForegroundColor(xlCellStyle, bg);
            xlCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        }/*  w  ww . jav  a 2 s.c  o  m*/

        Font xlFont = xlWorkbook.createFont();
        setFontColor(xlFont, fg);
        xlFont.setFontName(fontData.getName());
        xlFont.setFontHeightInPoints((short) fontData.getHeight());
        xlCellStyle.setFont(xlFont);

        if (vertical)
            xlCellStyle.setRotation((short) 90);

        switch (hAlign) {
        case SWT.CENTER:
            xlCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
            break;
        case SWT.LEFT:
            xlCellStyle.setAlignment(CellStyle.ALIGN_LEFT);
            break;
        case SWT.RIGHT:
            xlCellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
            break;
        }
        switch (vAlign) {
        case SWT.TOP:
            xlCellStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
            break;
        case SWT.CENTER:
            xlCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
            break;
        case SWT.BOTTOM:
            xlCellStyle.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);
            break;
        }

        if (dataFormat != null) {
            CreationHelper createHelper = xlWorkbook.getCreationHelper();
            xlCellStyle.setDataFormat(createHelper.createDataFormat().getFormat(dataFormat));
        }

        xlCellStyles.put(new ExcelCellStyleAttributes(fg, bg, fontData, dataFormat, hAlign, vAlign, vertical),
                xlCellStyle);
    }
    return xlCellStyle;
}

From source file:org.eclipse.scada.ae.ui.views.export.excel.impl.ExportEventsImpl.java

License:Open Source License

private void makeHeader(final List<Field> columns, final HSSFSheet sheet) {
    final Font font = sheet.getWorkbook().createFont();
    font.setFontName("Arial");
    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    font.setColor(HSSFColor.WHITE.index);

    final CellStyle style = sheet.getWorkbook().createCellStyle();
    style.setFont(font);
    style.setFillForegroundColor(HSSFColor.BLACK.index);
    style.setFillPattern(PatternFormatting.SOLID_FOREGROUND);

    final HSSFRow row = sheet.createRow(0);

    for (int i = 0; i < columns.size(); i++) {
        final Field field = columns.get(i);

        final HSSFCell cell = row.createCell(i);
        cell.setCellValue(field.getHeader());
        cell.setCellStyle(style);// w  ww  . j  ava2  s  .c  om
    }
}

From source file:org.eclipse.sw360.exporter.ExcelExporter.java

License:Open Source License

/**
 * Create header style, same has cell style but with bold font
 *///from w ww.  j ava  2s  . com
private static CellStyle createHeaderStyle(Workbook workbook) {
    CellStyle headerCellStyle = createCellStyle(workbook);
    Font font = workbook.createFont();
    font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
    headerCellStyle.setFont(font);
    return headerCellStyle;
}

From source file:org.exist.xquery.corenlp.Tokenize.java

License:Open Source License

private void createXSLXSpreadsheet(List<List<CoreLabel>> sentences, List<CoreLabel> tokens) {
    Workbook workbook = null;//from   w ww . j  a  va2 s  .c  om
    if (outputFormat == OutDocType.XSLX) {
        workbook = new SXSSFWorkbook();
    } else {
        workbook = new HSSFWorkbook();
    }
    CreationHelper creationHelper = workbook.getCreationHelper();
    org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet();

    Font boldFont = workbook.createFont();
    boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);

    // Header
    CellStyle headerStyle = workbook.createCellStyle();
    headerStyle.setFont(boldFont);
    int lineIndex = 0;
    for (List<CoreLabel> sentence : sentences) {
        for (CoreLabel token : sentence) {
            String value = token.get(CoreAnnotations.OriginalTextAnnotation.class);
            Row row = sheet.createRow(lineIndex);
            row.createCell(0).setCellValue(creationHelper.createRichTextString(value));
            row.createCell(1).setCellValue(creationHelper.createRichTextString(backgroundSymbol));
            lineIndex++;
        }
        Row row = sheet.createRow(lineIndex);
        row.createCell(0).setCellValue(creationHelper.createRichTextString(""));
        row.createCell(1).setCellValue(creationHelper.createRichTextString(""));
        lineIndex++;
    }

    try (OutputStream os = Files.newOutputStream(tempOutFile)) {
        workbook.write(os);
    } catch (FileNotFoundException fe) {
        LOG.error(fe);
    } catch (IOException ioe) {
        LOG.error(ioe);
    } finally {
        if (workbook != null) {
            if (workbook instanceof SXSSFWorkbook) {
                ((SXSSFWorkbook) workbook).dispose();
            } else {
                workbook = null;
            }
        }
    }
}

From source file:org.forzaframework.util.ExcelUtils.java

License:Apache License

static public void modelToExcelSheet(Workbook wb, String sheetName, List<Map<String, Object>> headers,
        List<Map<String, Object>> data, List<Map<String, Object>> footers, Integer freezePane,
        Boolean defaultFormat, Boolean createNewSheet, Integer indexSheet, Integer startInRow,
        Boolean printHeader, Boolean autoSizeColumns) {
    Sheet sheet = getSheet(wb, sheetName, createNewSheet, indexSheet);
    CellStyle headerCellStyle = getDefaultHeaderCellStyle(wb, defaultFormat);
    CellStyle titlesCellStyle = null;
    if (defaultFormat != null && defaultFormat) {
        titlesCellStyle = wb.createCellStyle();
        //Creamos el tipo de fuente
        Font titleFont = wb.createFont();
        //            headerFont.setFontName(HSSFFont.FONT_ARIAL);
        titleFont.setBold(Boolean.TRUE);
        titleFont.setColor(Font.COLOR_NORMAL);
        titleFont.setFontHeightInPoints((short) 8);
        titlesCellStyle.setFont(titleFont);
    }/*from   w  ww .  j  av a 2  s.  c  om*/

    Integer col = 0;
    Integer row = 0;
    if (startInRow != null) {
        row = startInRow;
    }
    Map<Integer, Integer> columnWidthMap = new HashMap<Integer, Integer>();
    //Indice de la fila donde empieza los encabezados de titulo de cada columna
    Integer principalHeaderIndex = headers.size() - 1;
    if (printHeader != null && printHeader) {
        //Armamos el encabezado
        for (Map<String, Object> header : headers) {
            for (Map.Entry<String, Object> entry : header.entrySet()) {
                Cell cell = getCell(sheet, row, col);
                if (defaultFormat != null && defaultFormat) {
                    if (principalHeaderIndex.equals(row)) {
                        //Colocamos el formato de la celda
                        cell.setCellStyle(headerCellStyle);
                    } else {
                        cell.setCellStyle(titlesCellStyle);
                    }
                }
                setValue(cell, entry.getValue());
                //Especificamos el ancho que tendra la columna
                if (autoSizeColumns != null && autoSizeColumns) {
                    columnWidthMap.put(col, entry.getValue().toString().length());
                }
                col++;
            }
            row++;
            col = 0;
        }
        //Ponemos la altura del encabezado
        setRowHeight(sheet, row - 1, (short) 420);
    }

    CellStyle detailCellStyle = getDefaultDetailCellStyle(wb, defaultFormat);

    Map<String, Object> principalHeader = headers.get(principalHeaderIndex);
    // datos
    for (Map<String, Object> map : data) {
        for (Map.Entry<String, Object> entry : principalHeader.entrySet()) {
            Object value = map.get(entry.getKey());
            buildCellAndCalculateColumnWidth(sheet, value, col, row, detailCellStyle, columnWidthMap,
                    autoSizeColumns);
            col++;
        }
        col = 0;
        row++;
    }
    CellStyle totalCellStyle = null;
    if (defaultFormat != null && defaultFormat) {
        //Armamos el formato los totales
        totalCellStyle = wb.createCellStyle();
        Font totalFont = wb.createFont();
        totalFont.setBold(Boolean.TRUE);
        totalFont.setColor(Font.COLOR_NORMAL);
        totalFont.setFontHeightInPoints((short) 8);
        totalCellStyle.setFont(totalFont);
    }

    if (footers != null) {
        for (Map<String, Object> footer : footers) {
            for (Map.Entry<String, Object> entry : principalHeader.entrySet()) {
                Cell cell = getCell(sheet, row, col++);
                if (totalCellStyle != null) {
                    //Colocamos el formato de la celda
                    cell.setCellStyle(totalCellStyle);
                }

                Object object = footer.get(entry.getKey());
                if (object != null) {
                    setValue(cell, object);
                } else {
                    setText(cell, "");
                }
            }
        }
    }

    if (autoSizeColumns != null && autoSizeColumns) {
        setColumnsWidth(sheet, columnWidthMap, principalHeader.size());
    }

    if (freezePane != null && freezePane > 0) {
        //Colocamos la columna estatica y las filas del encabezado estaticas
        sheet.createFreezePane(freezePane, headers.size());
    }
}

From source file:org.forzaframework.util.ExcelUtils.java

License:Apache License

public static CellStyle getDefaultDetailCellStyle(Workbook wb, Boolean defaultFormat) {
    CellStyle detailCellStyle = null;
    if (defaultFormat != null && defaultFormat) {
        //Armamos el formato para los datos del detalle
        detailCellStyle = wb.createCellStyle();
        Font detailFont = wb.createFont();
        detailFont.setFontHeightInPoints((short) 8);
        detailCellStyle.setFont(detailFont);
    }/*from   ww w  .  ja v  a 2  s . c om*/
    return detailCellStyle;
}