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

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

Introduction

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

Prototype

void setAlignment(HorizontalAlignment align);

Source Link

Document

set the type of horizontal alignment for the cell

Usage

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/*from  w ww.  j a  v a2  s .  com*/
 * 
 * @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.openelis.bean.WorksheetExcelHelperBean.java

License:Open Source License

private void createStyles(HSSFWorkbook wb) {
    CellStyle dateTimeEditStyle, dateTimeNoEditStyle, headerStyle, rowEditStyle, rowNoEditStyle;
    CreationHelper helper;/*  w w w  .  jav a2s .co m*/
    Font font;

    helper = wb.getCreationHelper();
    styles = new HashMap<String, CellStyle>();

    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);
    headerStyle.setLocked(true);
    styles.put("header", headerStyle);

    rowEditStyle = wb.createCellStyle();
    rowEditStyle.setAlignment(CellStyle.ALIGN_LEFT);
    rowEditStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    rowEditStyle.setLocked(false);
    styles.put("row_edit", rowEditStyle);

    rowNoEditStyle = wb.createCellStyle();
    rowNoEditStyle.setAlignment(CellStyle.ALIGN_LEFT);
    rowNoEditStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    rowNoEditStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
    rowNoEditStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    rowNoEditStyle.setLocked(true);
    styles.put("row_no_edit", rowNoEditStyle);

    dateTimeEditStyle = wb.createCellStyle();
    dateTimeEditStyle.setDataFormat(helper.createDataFormat().getFormat("yyyy-MM-dd hh:mm"));
    dateTimeEditStyle.setAlignment(CellStyle.ALIGN_LEFT);
    dateTimeEditStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    dateTimeEditStyle.setLocked(false);
    styles.put("datetime_edit", dateTimeEditStyle);

    dateTimeNoEditStyle = wb.createCellStyle();
    dateTimeNoEditStyle.setDataFormat(helper.createDataFormat().getFormat("yyyy-MM-dd hh:mm"));
    dateTimeNoEditStyle.setAlignment(CellStyle.ALIGN_LEFT);
    dateTimeNoEditStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    dateTimeNoEditStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
    dateTimeNoEditStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    dateTimeNoEditStyle.setLocked(true);
    styles.put("datetime_no_edit", dateTimeNoEditStyle);
}

From source file:org.phenotips.export.internal.Styler.java

License:Open Source License

public void style(DataCell dataCell, Cell cell, Workbook wBook) {
    Set<StyleOption> styles = dataCell.getStyles();
    CellStyle cellStyle = wBook.createCellStyle();
    /* For \n to work properly set to true */
    cellStyle.setWrapText(true);/*from   w  ww .  j av  a  2 s. c o m*/
    if (this.defaultFont == null) {
        this.defaultFont = createDefaultFont(wBook);
    }
    cellStyle.setFont(this.defaultFont);
    if (styles == null) {
        if (this.styleCache.containsKey(Collections.<StyleOption>emptySet())) {
            cell.setCellStyle(this.styleCache.get(Collections.<StyleOption>emptySet()));
            return;
        }
        cell.setCellStyle(cellStyle);
        this.styleCache.put(Collections.<StyleOption>emptySet(), cellStyle);
        return;
    }

    if (this.styleCache.containsKey(styles)) {
        cell.setCellStyle(this.styleCache.get(styles));
        return;
    }

    /* Priority can be coded in by placing the if statement lower, for higher priority */
    /** Font styles */
    Font headerFont = null;
    if (styles.contains(StyleOption.HEADER)) {
        headerFont = wBook.createFont();
        headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        cellStyle.setFont(headerFont);
        cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        cell.setCellStyle(cellStyle);
    }
    if (styles.contains(StyleOption.LARGE_HEADER)) {
        if (headerFont == null) {
            headerFont = wBook.createFont();
            headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        }
        headerFont.setFontHeightInPoints((short) 12);
        cellStyle.setFont(headerFont);
        cell.setCellStyle(cellStyle);
    }
    if (styles.contains(StyleOption.YES)) {
        Font font = createDefaultFont(wBook);
        font.setColor(HSSFColor.GREEN.index);
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);
    }
    if (styles.contains(StyleOption.NO)) {
        Font font = createDefaultFont(wBook);
        font.setColor(HSSFColor.DARK_RED.index);
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);
    }

    /** Border styles */
    if (styles.contains(StyleOption.HEADER_BOTTOM)) {
        cellStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
        cell.setCellStyle(cellStyle);
    }
    if (styles.contains(StyleOption.SECTION_BORDER_LEFT)) {
        cellStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
        cell.setCellStyle(cellStyle);
    }
    if (styles.contains(StyleOption.SECTION_BORDER_RIGHT)) {
        cellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
        cell.setCellStyle(cellStyle);
    }
    if (styles.contains(StyleOption.PATIENT_BORDER)) {
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        cell.setCellStyle(cellStyle);
    }
    if (styles.contains(StyleOption.FEATURE_SEPARATOR)) {
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);
        cellStyle.setTopBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
        cell.setCellStyle(cellStyle);
    }
    if (styles.contains(StyleOption.YES_NO_SEPARATOR)) {
        cellStyle.setBorderTop(CellStyle.BORDER_DASHED);
        cellStyle.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        cell.setCellStyle(cellStyle);
    }

    /* Keep this as the last statement. */
    this.styleCache.put(styles, cellStyle);
}

From source file:org.primefaces.extensions.showcase.util.ExcelCustomExporter.java

License:Apache License

protected CellStyle addColumnAlignments(UIComponent component, CellStyle style) {
    if (component instanceof HtmlOutputText) {
        HtmlOutputText output = (HtmlOutputText) component;
        if (output.getStyle() != null && output.getStyle().contains("left")) {
            style.setAlignment(HorizontalAlignment.LEFT);
        }/*from  ww w.j av  a2  s  . c  o m*/
        if (output.getStyle() != null && output.getStyle().contains("right")) {
            style.setAlignment(HorizontalAlignment.RIGHT);
        }
        if (output.getStyle() != null && output.getStyle().contains("center")) {
            style.setAlignment(HorizontalAlignment.CENTER);
        }
    }
    return style;
}

From source file:org.projectforge.excel.CellFormat.java

License:Open Source License

/**
 * Please note: the data format should be set by the ContentProvider (for re-usage).
 * @param cellStyle/* w  w w  .  j  a  va2  s .co  m*/
 */
public void copyToCellStyle(final CellStyle cellStyle) {
    if (alignment != null) {
        cellStyle.setAlignment(alignment);
    }
    if (font != null) {
        cellStyle.setFont(font);
    }
    if (fillForegroundColor != null) {
        cellStyle.setFillForegroundColor(fillForegroundColor);
    }
    if (wrapText != null) {
        cellStyle.setWrapText(wrapText);
    }
}

From source file:org.sakaiproject.signup.tool.downloadEvents.WorksheetStyleClass.java

License:Educational Community License

public static Map<String, CellStyle> createStyles(Workbook wb) {
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
    CellStyle style;
    Font titleFont = wb.createFont();
    titleFont.setFontHeightInPoints((short) 18);
    titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    titleFont.setColor(IndexedColors.DARK_BLUE.getIndex());
    style = wb.createCellStyle();/*from  ww  w .  ja va  2s . co  m*/
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFont(titleFont);
    styles.put("title", style);

    Font commentTitleFont = wb.createFont();
    commentTitleFont.setFontHeightInPoints((short) 12);
    commentTitleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    commentTitleFont.setColor(IndexedColors.DARK_BLUE.getIndex());
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFont(commentTitleFont);
    styles.put("commentTitle", style);

    Font itemFont = wb.createFont();
    itemFont.setFontHeightInPoints((short) 10);
    itemFont.setFontName("Trebuchet MS");
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setFont(itemFont);
    styles.put("item_left", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFont(itemFont);
    style.setWrapText(true);
    styles.put("item_left_wrap", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    style.setFont(itemFont);
    style.setWrapText(true);
    styles.put("item_left_wrap_top", style);

    itemFont.setFontHeightInPoints((short) 10);
    itemFont.setFontName("Trebuchet MS");
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFont(itemFont);
    styles.put("tabItem_fields", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_RIGHT);
    style.setFont(itemFont);
    styles.put("item_right", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFont(itemFont);
    style.setWrapText(true);
    styles.put("attendee_layout", style);

    Font itemBoldFont = wb.createFont();
    itemBoldFont.setFontHeightInPoints((short) 10);
    itemBoldFont.setFontName("Trebuchet MS");
    itemBoldFont.setColor(IndexedColors.DARK_BLUE.getIndex());
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    itemBoldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style.setFont(itemBoldFont);
    styles.put("item_leftBold", style);

    Font tableFont = wb.createFont();
    tableFont.setFontHeightInPoints((short) 12);
    tableFont.setColor(IndexedColors.WHITE.getIndex());
    tableFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFont(tableFont);
    styles.put("tabColNames", style);

    tableFont.setFontHeightInPoints((short) 10);
    tableFont.setColor(IndexedColors.WHITE.getIndex());
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFont(tableFont);
    style.setWrapText(true);
    styles.put("header", style);

    Font linkFont = wb.createFont();
    linkFont.setFontHeightInPoints((short) 10);
    linkFont.setColor(IndexedColors.BLUE.getIndex());
    linkFont.setUnderline(Font.U_SINGLE);
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFont(linkFont);
    styles.put("hyperLink", style);

    style = wb.createCellStyle();
    style.setBorderTop(CellStyle.BORDER_THICK);
    style.setTopBorderColor(IndexedColors.DARK_BLUE.getIndex());
    styles.put("tab_endline", style);

    return styles;
}

From source file:org.sigmah.server.endpoint.export.sigmah.spreadsheet.ExcelUtils.java

License:Open Source License

public CellStyle getTopicStyle(HSSFWorkbook wb) {
    CellStyle style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFont(getBoldFont(wb, (short) 14));
    return style;
}

From source file:org.sigmah.server.endpoint.export.sigmah.spreadsheet.ExcelUtils.java

License:Open Source License

public CellStyle getHeaderStyle(HSSFWorkbook wb) {
    CellStyle style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

    HSSFPalette palette = wb.getCustomPalette();
    palette.setColorAtIndex(HSSFColor.GREY_25_PERCENT.index, ExportConstants.GRAY_10_RGB[0],
            ExportConstants.GRAY_10_RGB[1], ExportConstants.GRAY_10_RGB[2]);
    style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFont(getBoldFont(wb, (short) 10));
    style.setWrapText(true);//from   w  w w.  ja va2s.co m
    return style;
}

From source file:org.spdx.spdxspreadsheet.AbstractSheet.java

License:Apache License

public static CellStyle createHeaderStyle(Workbook wb) {
    CellStyle headerStyle = wb.createCellStyle();
    headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    Font headerFont = wb.createFont();
    headerFont.setFontName("Arial");
    headerFont.setFontHeight(FONT_SIZE);
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    headerStyle.setFont(headerFont);//from  w w w  .j a  v  a 2 s  .  c  o  m
    headerStyle.setAlignment(CellStyle.ALIGN_CENTER);
    return headerStyle;
}

From source file:org.spdx.spdxspreadsheet.AbstractSheet.java

License:Apache License

public static CellStyle createLeftWrapStyle(Workbook wb) {
    CellStyle wrapStyle = wb.createCellStyle();
    wrapStyle.setWrapText(true);/*from  w w w  . j  a va 2  s.  co m*/
    wrapStyle.setAlignment(CellStyle.ALIGN_LEFT);
    return wrapStyle;
}