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.netxilia.impexp.impl.PoiUtils.java

License:Open Source License

public static CellStyle netxiliaStyle2Poi(Styles nxStyle, Workbook workbook, CellStyle poiStyle) {
    if (nxStyle == null) {
        return poiStyle;
    }//w  w w  .  ja  va2  s .c  o  m
    poiStyle.setWrapText(nxStyle.contains(DefaultStyle.nowrap.getStyle()));

    // font
    short bold = nxStyle.contains(DefaultStyle.bold.getStyle()) ? Font.BOLDWEIGHT_BOLD : Font.BOLDWEIGHT_NORMAL;
    byte underline = nxStyle.contains(DefaultStyle.underline.getStyle()) ? Font.U_SINGLE : Font.U_NONE;
    boolean italic = nxStyle.contains(DefaultStyle.italic.getStyle());
    boolean strikeout = nxStyle.contains(DefaultStyle.strikeout.getStyle());
    Font defaultFont = workbook.getFontAt(poiStyle.getFontIndex());
    Font font = workbook.findFont(bold, defaultFont.getColor(), defaultFont.getFontHeight(),
            defaultFont.getFontName(), italic, strikeout, defaultFont.getTypeOffset(), underline);
    if (font == null) {
        font = workbook.createFont();
        font.setBoldweight(bold);
        font.setItalic(italic);
        font.setUnderline(underline);
        font.setStrikeout(strikeout);
    }
    poiStyle.setFont(font);

    // borders
    if (nxStyle.contains(DefaultStyle.borderLeft.getStyle())) {
        poiStyle.setBorderLeft(CellStyle.BORDER_THIN);
    }
    if (nxStyle.contains(DefaultStyle.borderRight.getStyle())) {
        poiStyle.setBorderRight(CellStyle.BORDER_THIN);
    }
    if (nxStyle.contains(DefaultStyle.borderTop.getStyle())) {
        poiStyle.setBorderTop(CellStyle.BORDER_THIN);
    }
    if (nxStyle.contains(DefaultStyle.borderBottom.getStyle())) {
        poiStyle.setBorderBottom(CellStyle.BORDER_THIN);
    }

    // align
    if (nxStyle.contains(DefaultStyle.alignLeft.getStyle())) {
        poiStyle.setAlignment(CellStyle.ALIGN_LEFT);
    } else if (nxStyle.contains(DefaultStyle.alignRight.getStyle())) {
        poiStyle.setAlignment(CellStyle.ALIGN_RIGHT);
    } else if (nxStyle.contains(DefaultStyle.alignCenter.getStyle())) {
        poiStyle.setAlignment(CellStyle.ALIGN_CENTER);
    } else if (nxStyle.contains(DefaultStyle.alignJustify.getStyle())) {
        poiStyle.setAlignment(CellStyle.ALIGN_JUSTIFY);
    }

    return poiStyle;
}

From source file:org.olat.core.gui.components.form.flexible.impl.elements.table.XlsFlexiTableExporter.java

License:Apache License

public static CellStyle getHeaderCellStyle(final Workbook wb) {
    CellStyle cellStyle = wb.createCellStyle();
    Font boldFont = wb.createFont();
    boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    cellStyle.setFont(boldFont);
    return cellStyle;
}

From source file:org.olat.core.gui.components.table.DefaultXlsTableExporter.java

License:Apache License

private CellStyle getHeaderCellStyle(final Workbook wb) {
    CellStyle cellStyle = wb.createCellStyle();
    Font boldFont = wb.createFont();
    boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    cellStyle.setFont(boldFont);
    return cellStyle;
}

From source file:org.openelis.bean.DataViewBean.java

License:Open Source License

private CellStyle createStyle(HSSFWorkbook wb) {
    CellStyle headerStyle;
    Font font;/*  w  w  w. j  av  a2 s  . c  om*/

    font = wb.createFont();
    font.setColor(IndexedColors.WHITE.getIndex());
    headerStyle = wb.createCellStyle();
    headerStyle.setAlignment(CellStyle.ALIGN_LEFT);
    headerStyle.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    headerStyle.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex());
    headerStyle.setFont(font);

    return headerStyle;
}

From source file:org.openelis.bean.DataViewReportBean.java

License:Open Source License

/**
 * Creates the header row in "sheet" from "headers"; sets a style on the
 * header row to distinguish it from the other rows; updates "maxChars" to
 * account for the header labels because the header row is added after the
 * other rows have been added/*w  w w. j a  v a 2 s .  co m*/
 * 
 * @param sheet
 *        the sheet that contains all rows in "wb"
 * @param wb
 *        the workbook that gets converted to an Excel file
 * @param headers
 *        the list of labels to be shown in the header row
 * @param maxChars
 *        the list containing the maximum number of characters in each
 *        column of "sheet"
 */
private void setHeaderCells(Sheet sheet, XSSFWorkbook wb, ArrayList<String> headers,
        ArrayList<Integer> maxChars) {
    Cell cell;
    Row row;
    Font font;
    CellStyle style;

    /*
     * create the style to distinguish the header row from the other rows in
     * the output
     */
    font = wb.createFont();
    font.setColor(IndexedColors.WHITE.getIndex());
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex());
    style.setFont(font);

    row = sheet.createRow(0);
    for (int i = 0; i < headers.size(); i++) {
        cell = row.createCell(i);
        cell.setCellStyle(style);
        setCellValue(cell, headers.get(i), null);
        setMaxChars(cell.getColumnIndex(), headers.get(i), maxChars, null);
    }
}

From source file:org.openmrs.module.mksreports.renderer.PatientHistoryExcelTemplateRenderer.java

License:Open Source License

/** 
 * @see ReportRenderer#render(ReportData, String, OutputStream)
 *//*from   www.  j a  v  a 2  s . c om*/
public void render(ReportData reportData, String argument, OutputStream out)
        throws IOException, RenderingException {

    try {
        log.debug("Attempting to render report with ExcelTemplateRenderer");
        ReportDesign design = getDesign(argument);
        Workbook wb = getExcelTemplate(design);

        if (wb == null) {
            XlsReportRenderer xlsRenderer = new XlsReportRenderer();
            xlsRenderer.render(reportData, argument, out);
        } else {
            //This should be changed to get the dataset name form a parameter 
            DataSet ds = reportData.getDataSets().get("patient");
            ArrayList<String> names = new ArrayList<String>();

            for (DataSetColumn dataSetRow : ds.getMetaData().getColumns()) {
                names.add(dataSetRow.getName());
            }
            Sheet s = wb.getSheetAt(0);

            //Trying to creat a row that has the replacement values pre-populated
            Row h = s.createRow(8);
            CellStyle style = wb.createCellStyle();
            Font font = wb.createFont();
            font.setFontName(HSSFFont.FONT_ARIAL);
            font.setBold(true);
            style.setFont(font);

            for (String name : names) {
                Cell c = h.createCell(names.indexOf(name));
                String value = name.toUpperCase().replace("_", " ");
                c.setCellValue(value);
                c.setCellStyle(style);
            }

            Row r = s.getRow(9);

            for (String name : names) {
                Cell c = r.createCell(names.indexOf(name));
                String value = "#patient." + name + "#";
                c.setCellValue(value);
            }

            Map<String, String> repeatSections = getRepeatingSections(design);

            // Put together base set of replacements.  Any dataSet with only one row is included.
            Map<String, Object> replacements = getBaseReplacementData(reportData, design);

            // Iterate across all of the sheets in the workbook, and configure all those that need to be added/cloned
            List<SheetToAdd> sheetsToAdd = new ArrayList<SheetToAdd>();

            Set<String> usedSheetNames = new HashSet<String>();
            int numberOfSheets = wb.getNumberOfSheets();

            for (int sheetNum = 0; sheetNum < numberOfSheets; sheetNum++) {

                Sheet currentSheet = wb.getSheetAt(sheetNum);
                String originalSheetName = wb.getSheetName(sheetNum);

                String dataSetName = getRepeatingSheetProperty(sheetNum, repeatSections);
                if (dataSetName != null) {

                    DataSet repeatingSheetDataSet = getDataSet(reportData, dataSetName, replacements);
                    int dataSetRowNum = 0;
                    for (Iterator<DataSetRow> rowIterator = repeatingSheetDataSet.iterator(); rowIterator
                            .hasNext();) {
                        DataSetRow dataSetRow = rowIterator.next();
                        dataSetRowNum++;
                        Map<String, Object> newReplacements = getReplacementData(replacements, reportData,
                                design, dataSetName, dataSetRow, dataSetRowNum);
                        Sheet newSheet = (dataSetRowNum == 1 ? currentSheet : wb.cloneSheet(sheetNum));
                        sheetsToAdd.add(new SheetToAdd(newSheet, sheetNum, originalSheetName, newReplacements));
                    }
                } else {
                    sheetsToAdd.add(new SheetToAdd(currentSheet, sheetNum, originalSheetName, replacements));
                }
            }

            // Then iterate across all of these and add them in
            for (int i = 0; i < sheetsToAdd.size(); i++) {
                addSheet(wb, sheetsToAdd.get(i), usedSheetNames, reportData, design, repeatSections);
            }

            wb.write(out);
        }
    } catch (Exception e) {
        throw new RenderingException("Unable to render results due to: " + e, e);
    }
}

From source file:org.openmrs.module.rwandareports.renderer.PatientHistoryExcelTemplateRenderer.java

License:Open Source License

/** 
 * @see ReportRenderer#render(ReportData, String, OutputStream)
 */// w  w  w  . j av  a  2 s . c  o  m
public void render(ReportData reportData, String argument, OutputStream out)
        throws IOException, RenderingException {

    try {
        log.debug("Attempting to render report with ExcelTemplateRenderer");
        ReportDesign design = getDesign(argument);
        Workbook wb = getExcelTemplate(design);

        if (wb == null) {
            XlsReportRenderer xlsRenderer = new XlsReportRenderer();
            xlsRenderer.render(reportData, argument, out);
        } else {
            //This should be changed to get the dataset name form a parameter 
            DataSet ds = reportData.getDataSets().get("patient");
            ArrayList<String> names = new ArrayList<String>();

            for (DataSetColumn dataSetRow : ds.getMetaData().getColumns()) {
                names.add(dataSetRow.getName());
            }
            Sheet s = wb.getSheetAt(0);

            Row h = s.createRow(8);
            CellStyle style = wb.createCellStyle();
            Font font = wb.createFont();
            font.setFontName(HSSFFont.FONT_ARIAL);
            font.setBold(true);
            style.setFont(font);

            for (String name : names) {
                Cell c = h.createCell(names.indexOf(name));
                String value = name.toUpperCase().replace("_", " ");
                c.setCellValue(value);
                c.setCellStyle(style);
            }

            //Trying to creat a row that has the replacement values pre-populated
            Row r = s.getRow(9);

            for (String name : names) {
                Cell c = r.createCell(names.indexOf(name));
                String value = "#patient." + name + "#";
                c.setCellValue(value);

            }

            ExcelUtil.formatRow(r);
            Map<String, String> repeatSections = getRepeatingSections(design);

            // Put together base set of replacements.  Any dataSet with only one row is included.
            Map<String, Object> replacements = getBaseReplacementData(reportData, design);

            // Iterate across all of the sheets in the workbook, and configure all those that need to be added/cloned
            List<SheetToAdd> sheetsToAdd = new ArrayList<SheetToAdd>();

            Set<String> usedSheetNames = new HashSet<String>();
            int numberOfSheets = wb.getNumberOfSheets();

            for (int sheetNum = 0; sheetNum < numberOfSheets; sheetNum++) {

                Sheet currentSheet = wb.getSheetAt(sheetNum);
                String originalSheetName = wb.getSheetName(sheetNum);

                String dataSetName = getRepeatingSheetProperty(sheetNum, repeatSections);
                if (dataSetName != null) {

                    DataSet repeatingSheetDataSet = getDataSet(reportData, dataSetName, replacements);
                    int dataSetRowNum = 0;
                    for (Iterator<DataSetRow> rowIterator = repeatingSheetDataSet.iterator(); rowIterator
                            .hasNext();) {
                        DataSetRow dataSetRow = rowIterator.next();
                        dataSetRowNum++;
                        Map<String, Object> newReplacements = getReplacementData(replacements, reportData,
                                design, dataSetName, dataSetRow, dataSetRowNum);
                        Sheet newSheet = (dataSetRowNum == 1 ? currentSheet : wb.cloneSheet(sheetNum));
                        sheetsToAdd.add(new SheetToAdd(newSheet, sheetNum, originalSheetName, newReplacements));
                    }
                } else {
                    sheetsToAdd.add(new SheetToAdd(currentSheet, sheetNum, originalSheetName, replacements));
                }
            }

            // Then iterate across all of these and add them in
            for (int i = 0; i < sheetsToAdd.size(); i++) {
                addSheet(wb, sheetsToAdd.get(i), usedSheetNames, reportData, design, repeatSections);
            }

            wb.write(out);
        }
    } catch (Exception e) {
        throw new RenderingException("Unable to render results due to: " + e, e);
    }
}

From source file:org.openpythia.plugin.worststatements.DeltaSnapshotWriter.java

License:Apache License

private CellStyle createHyperlinkStyle(Workbook workbook) {
    // cell style for hyperlinks
    // by default hyperlinks are blue and underlined
    CellStyle hyperlinkStyle = workbook.createCellStyle();
    hyperlinkStyle.cloneStyleFrom(statementsSheet.getRow(INDEX_ROW_TEMPLATE_DELTA_SQL_STATEMENT)
            .getCell(INDEX_COLUMN_SQL_TEXT).getCellStyle());

    Font hyperlinkFont = workbook.createFont();
    hyperlinkFont.setUnderline(Font.U_SINGLE);
    hyperlinkFont.setColor(IndexedColors.BLUE.getIndex());

    hyperlinkStyle.setFont(hyperlinkFont);

    return hyperlinkStyle;
}

From source file:org.openscada.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(HSSFPatternFormatting.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);/*ww w.  j a v a 2 s  .c  o m*/
    }
}

From source file:org.patientview.radar.service.impl.ExcelDocumentDataBuilder.java

License:Open Source License

public byte[] build(DocumentData documentData) {

    Workbook workbook = new HSSFWorkbook();
    CreationHelper createHelper = workbook.getCreationHelper();
    Sheet sheet = workbook.createSheet("data");

    // add the headers/columns
    Row headerRow = sheet.createRow((short) 0);
    sheet.autoSizeColumn(0);//  ww  w .java 2 s.c  om

    CellStyle headerStyle = workbook.createCellStyle();

    headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    headerStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
    headerStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
    headerStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
    headerStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());

    headerStyle.setLeftBorderColor(CellStyle.BORDER_THIN);
    headerStyle.setRightBorderColor(CellStyle.BORDER_THIN);
    headerStyle.setTopBorderColor(CellStyle.BORDER_THIN);
    headerStyle.setBottomBorderColor(CellStyle.BORDER_THIN);

    Font headerFont = workbook.createFont();
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    headerStyle.setFont(headerFont);

    List<String> headers = documentData.getHeaders();
    int headerColumnIndex = 0;
    for (String header : headers) {
        sheet.autoSizeColumn(headerColumnIndex);
        Cell cell = headerRow.createCell(headerColumnIndex);
        cell.setCellStyle(headerStyle);
        cell.setCellValue(header);
        headerColumnIndex++;
    }

    // add the row data
    int columnIndex = 0;
    int rowIndex = 1;
    for (List<String> row : documentData.getRows()) {
        Row spreadSheetRow = sheet.createRow((short) rowIndex++);
        for (String data : row) {
            spreadSheetRow.createCell(columnIndex++).setCellValue(data);
        }
        columnIndex = 0;
    }

    // set the column width to fit the contents - this must be done after the data is added
    headerColumnIndex = 0;
    for (String header : headers) {
        sheet.autoSizeColumn(headerColumnIndex);
        headerColumnIndex++;
    }
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        workbook.write(outputStream);
        outputStream.close();
        outputStream.flush();
    } catch (IOException e) {
        LOGGER.error("Unable to write workbook to output stream " + e.getMessage(), e);
    }

    return outputStream.toByteArray();
}