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

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

Introduction

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

Prototype

void setVerticalAlignment(VerticalAlignment align);

Source Link

Document

set the type of vertical alignment for the cell

Usage

From source file:com.globalsight.everest.webapp.pagehandler.administration.reports.SlaXlsReportHelper.java

License:Apache License

private CellStyle getContentRightStyle(Workbook p_workbook) throws Exception {
    if (contentRightStyle == null) {
        CellStyle style = p_workbook.createCellStyle();
        style.setWrapText(true);//from  w ww . j a  v a 2 s  .  c  o m
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        Font font = p_workbook.createFont();
        font.setFontName("Arial");
        font.setFontHeightInPoints((short) 10);
        style.setFont(font);

        contentRightStyle = style;
    }

    return contentRightStyle;
}

From source file:com.griffinslogistics.document.excel.BookLabelGenerator.java

private static Map<String, CellStyle> createStyles(XSSFWorkbook workbook) {
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
    CellStyle smallStyle = workbook.createCellStyle();
    Font font = workbook.createFont();
    font.setFontHeightInPoints((short) 10);
    smallStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
    smallStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
    smallStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
    smallStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
    smallStyle.setFont(font);//from   www . java 2  s  .co m
    smallStyle.setAlignment(CellStyle.ALIGN_CENTER);
    smallStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    styles.put(STYLE_SMALL_FONT, smallStyle);

    CellStyle labelStyle = workbook.createCellStyle();
    Font labelFont = workbook.createFont();
    labelFont.setFontHeightInPoints((short) 20);
    labelStyle.setBorderBottom(CellStyle.BORDER_THIN);
    labelStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
    labelStyle.setBorderRight(CellStyle.BORDER_THIN);
    labelStyle.setBorderTop(CellStyle.BORDER_THIN);
    labelStyle.setFont(labelFont);
    labelStyle.setAlignment(CellStyle.ALIGN_CENTER);
    labelStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    labelStyle.setWrapText(true);
    styles.put(STYLE_LABEL, labelStyle);

    CellStyle contentStyle = workbook.createCellStyle();
    Font contentFont = workbook.createFont();
    contentFont.setFontHeightInPoints((short) 25);
    contentFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    contentStyle.setBorderBottom(CellStyle.BORDER_THIN);
    contentStyle.setBorderLeft(CellStyle.BORDER_THIN);
    contentStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
    contentStyle.setBorderTop(CellStyle.BORDER_THIN);
    contentStyle.setFont(contentFont);
    contentStyle.setAlignment(CellStyle.ALIGN_CENTER);
    contentStyle.setWrapText(true);
    contentStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    styles.put(STYLE_CONTENT, contentStyle);

    return styles;
}

From source file:com.griffinslogistics.document.excel.CMRGenerator.java

private static Map<String, CellStyle> createStyles(Workbook workbook) {
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();

    CellStyle style = workbook.createCellStyle();
    Font font = workbook.createFont();
    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    styles.put(DEFAULT_STYLE, style);//from w  w w  .jav  a  2 s .c  o  m

    CellStyle labelStyle = workbook.createCellStyle();
    font = workbook.createFont();
    font.setFontHeightInPoints((short) 10);
    font.setColor(HSSFColor.DARK_RED.index);
    labelStyle.setFont(font);
    labelStyle.setWrapText(true);
    styles.put(LABEL_STYLE, labelStyle);

    CellStyle labelTopStyle = workbook.createCellStyle();
    labelTopStyle.cloneStyleFrom(labelStyle);
    labelTopStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
    labelTopStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
    labelTopStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
    labelTopStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    styles.put(LABEL_TOP_STYLE, labelTopStyle);

    CellStyle labelMiddleStyle = workbook.createCellStyle();
    labelMiddleStyle.cloneStyleFrom(labelStyle);
    labelMiddleStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
    labelMiddleStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
    labelMiddleStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    styles.put(LABEL_MIDDLE_STYLE, labelMiddleStyle);

    CellStyle labelLeftStyle = workbook.createCellStyle();
    labelLeftStyle.cloneStyleFrom(labelStyle);
    labelLeftStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
    labelLeftStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    styles.put(LABEL_LEFT_STYLE, labelLeftStyle);

    CellStyle labelRightStyle = workbook.createCellStyle();
    labelRightStyle.cloneStyleFrom(labelStyle);
    labelRightStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
    labelRightStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    styles.put(LABEL_RIGHT_STYLE, labelRightStyle);

    CellStyle labelBottomStyle = workbook.createCellStyle();
    labelBottomStyle.cloneStyleFrom(labelStyle);
    labelBottomStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
    labelBottomStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
    labelBottomStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
    styles.put(LABEL_BOTTOM_STYLE, labelBottomStyle);

    CellStyle labelWholeStyle = workbook.createCellStyle();
    labelWholeStyle.cloneStyleFrom(labelStyle);
    labelWholeStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
    labelWholeStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
    labelWholeStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
    labelWholeStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
    labelWholeStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    styles.put(LABEL_WHOLE_STYLE, labelWholeStyle);

    Font contentFont = workbook.createFont();
    contentFont.setFontHeightInPoints((short) 12);
    contentFont.setBoldweight(Font.BOLDWEIGHT_BOLD);

    CellStyle contentTopStyle = workbook.createCellStyle();
    contentTopStyle.cloneStyleFrom(labelTopStyle);
    contentTopStyle.setFont(contentFont);
    styles.put(CONTENT_TOP_STYLE, contentTopStyle);

    CellStyle contentMiddleStyle = workbook.createCellStyle();
    contentMiddleStyle.cloneStyleFrom(labelMiddleStyle);
    contentMiddleStyle.setFont(contentFont);
    styles.put(CONTENT_MIDDLE_STYLE, contentMiddleStyle);

    CellStyle contentMiddleAllignRightStyle = workbook.createCellStyle();
    contentMiddleAllignRightStyle.cloneStyleFrom(contentMiddleStyle);
    contentMiddleAllignRightStyle.setBorderRight(CellStyle.BORDER_NONE);
    contentMiddleAllignRightStyle.setFont(contentFont);
    contentMiddleAllignRightStyle.setAlignment(CellStyle.ALIGN_RIGHT);
    styles.put(CONTENT_MIDDLE_ALLIGN_RIGHT_STYLE, contentMiddleAllignRightStyle);

    CellStyle contentBottomAllignCenterStyle = workbook.createCellStyle();
    contentBottomAllignCenterStyle.cloneStyleFrom(contentMiddleStyle);
    contentBottomAllignCenterStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
    contentBottomAllignCenterStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
    contentBottomAllignCenterStyle.setFont(contentFont);
    contentBottomAllignCenterStyle.setAlignment(CellStyle.ALIGN_CENTER);
    styles.put(CONTENT_BOTTOM_ALLIGN_CENTER_STYLE, contentBottomAllignCenterStyle);

    CellStyle contentMiddleNoBordersAllignRightStyle = workbook.createCellStyle();
    contentMiddleNoBordersAllignRightStyle.cloneStyleFrom(contentMiddleAllignRightStyle);
    contentMiddleNoBordersAllignRightStyle.setBorderLeft(CellStyle.BORDER_NONE);
    contentMiddleNoBordersAllignRightStyle.setAlignment(CellStyle.ALIGN_CENTER);
    contentMiddleNoBordersAllignRightStyle.setFont(contentFont);
    styles.put(CONTENT_MIDDLE_NO_BORDERS_STYLE, contentMiddleNoBordersAllignRightStyle);

    CellStyle contentMiddleAllignCenterStyle = workbook.createCellStyle();
    contentMiddleAllignCenterStyle.cloneStyleFrom(contentMiddleStyle);
    contentMiddleAllignCenterStyle.setAlignment(CellStyle.ALIGN_CENTER);
    contentMiddleAllignCenterStyle.setFont(contentFont);
    styles.put(CONTENT_MIDDLE_ALLIGN_CENTER_STYLE, contentMiddleAllignCenterStyle);

    CellStyle contentRightStyle = workbook.createCellStyle();
    contentRightStyle.cloneStyleFrom(labelRightStyle);
    contentRightStyle.setFont(contentFont);
    styles.put(CONTENT_RIGHT_STYLE, contentRightStyle);

    CellStyle contentBottomStyle = workbook.createCellStyle();
    contentBottomStyle.cloneStyleFrom(labelBottomStyle);
    contentBottomStyle.setFont(contentFont);
    styles.put(CONTENT_BOTTOM_STYLE, contentBottomStyle);

    CellStyle contentWholeStyle = workbook.createCellStyle();
    contentWholeStyle.cloneStyleFrom(labelWholeStyle);
    contentWholeStyle.setFont(contentFont);
    styles.put(CONTENT_WHOLE_STYLE, contentWholeStyle);

    return styles;
}

From source file:com.griffinslogistics.excel.BookLabelGenerator.java

private static Map<String, CellStyle> createStyles(XSSFWorkbook workbook) {
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
    CellStyle smallStyle = workbook.createCellStyle();
    Font font = workbook.createFont();
    font.setFontHeightInPoints((short) 10);
    smallStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
    smallStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
    smallStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
    smallStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
    smallStyle.setFont(font);//w w  w.j  a  v a 2 s  .  c om
    smallStyle.setAlignment(CellStyle.ALIGN_CENTER);
    smallStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    styles.put(STYLE_SMALL_FONT, smallStyle);

    CellStyle labelStyle = workbook.createCellStyle();
    Font labelFont = workbook.createFont();
    labelFont.setFontHeightInPoints((short) 13);
    labelStyle.setBorderBottom(CellStyle.BORDER_THIN);
    labelStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
    labelStyle.setBorderRight(CellStyle.BORDER_THIN);
    labelStyle.setBorderTop(CellStyle.BORDER_THIN);
    labelStyle.setFont(labelFont);
    labelStyle.setAlignment(CellStyle.ALIGN_CENTER);
    labelStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    styles.put(STYLE_LABEL, labelStyle);

    CellStyle contentStyle = workbook.createCellStyle();
    Font contentFont = workbook.createFont();
    contentFont.setFontHeightInPoints((short) 15);
    contentFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    contentStyle.setBorderBottom(CellStyle.BORDER_THIN);
    contentStyle.setBorderLeft(CellStyle.BORDER_THIN);
    contentStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
    contentStyle.setBorderTop(CellStyle.BORDER_THIN);
    contentStyle.setFont(contentFont);
    contentStyle.setAlignment(CellStyle.ALIGN_CENTER);
    contentStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    styles.put(STYLE_CONTENT, contentStyle);

    return styles;
}

From source file:com.guardias.excel.CalendarToExcel.java

License:Apache License

/**
 * cell styles used for formatting calendar sheets
 *///from www .j  av a  2s.c o m
private static Map<String, CellStyle> createStyles(Workbook wb) {
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();

    short borderColor = IndexedColors.GREY_50_PERCENT.getIndex();

    CellStyle style;
    Font titleFont = wb.createFont();
    titleFont.setFontHeightInPoints((short) 48);
    titleFont.setColor(IndexedColors.DARK_BLUE.getIndex());
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setFont(titleFont);
    styles.put("title", style);

    Font monthFont = wb.createFont();
    monthFont.setFontHeightInPoints((short) 12);
    monthFont.setColor(IndexedColors.WHITE.getIndex());
    monthFont.setBold(true);
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setFont(monthFont);
    styles.put("month", style);

    Font dayFont = wb.createFont();
    dayFont.setFontHeightInPoints((short) 14);
    dayFont.setBold(true);
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.LEFT);
    style.setVerticalAlignment(VerticalAlignment.TOP);
    style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderLeft(BorderStyle.THIN);
    style.setLeftBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    style.setFont(dayFont);
    styles.put("weekend_left", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.TOP);
    style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    styles.put("weekend_right", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.LEFT);
    style.setVerticalAlignment(VerticalAlignment.TOP);
    style.setBorderLeft(BorderStyle.THIN);
    style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setLeftBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    style.setFont(dayFont);
    styles.put("workday_left", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.TOP);
    style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    styles.put("workday_right", style);

    style = wb.createCellStyle();
    style.setBorderLeft(BorderStyle.THIN);
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    styles.put("grey_left", style);

    style = wb.createCellStyle();
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(borderColor);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(borderColor);
    styles.put("grey_right", style);

    return styles;
}

From source file:com.haulmont.cuba.gui.export.ExcelExporter.java

License:Apache License

public void exportTable(Table<Entity> table, List<Table.Column> columns, Boolean exportExpanded,
        ExportDisplay display, List<String> filterDescription, String fileName, ExportMode exportMode) {

    if (display == null) {
        throw new IllegalArgumentException("ExportDisplay is null");
    }/*from  ww  w.  j  a v a  2 s . c  o m*/

    createWorkbookWithSheet();
    createFonts();
    createFormats();

    int r = 0;
    if (filterDescription != null) {
        for (r = 0; r < filterDescription.size(); r++) {
            String line = filterDescription.get(r);
            HSSFRow row = sheet.createRow(r);
            if (r == 0) {
                HSSFRichTextString richTextFilterName = new HSSFRichTextString(line);
                richTextFilterName.applyFont(boldFont);
                row.createCell(0).setCellValue(richTextFilterName);
            } else {
                row.createCell(0).setCellValue(line);
            }
        }
        r++;
    }
    HSSFRow row = sheet.createRow(r);
    createAutoColumnSizers(columns.size());

    float maxHeight = sheet.getDefaultRowHeightInPoints();

    CellStyle headerCellStyle = wb.createCellStyle();
    headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    for (Table.Column column : columns) {
        String caption = column.getCaption();

        int countOfReturnSymbols = StringUtils.countMatches(caption, "\n");
        if (countOfReturnSymbols > 0) {
            maxHeight = Math.max(maxHeight, (countOfReturnSymbols + 1) * sheet.getDefaultRowHeightInPoints());
            headerCellStyle.setWrapText(true);
        }
    }
    row.setHeightInPoints(maxHeight);

    for (int c = 0; c < columns.size(); c++) {
        Table.Column column = columns.get(c);
        String caption = column.getCaption();

        HSSFCell cell = row.createCell(c);
        HSSFRichTextString richTextString = new HSSFRichTextString(caption);
        richTextString.applyFont(boldFont);
        cell.setCellValue(richTextString);

        ExcelAutoColumnSizer sizer = new ExcelAutoColumnSizer();
        sizer.notifyCellValue(caption, boldFont);
        sizers[c] = sizer;

        cell.setCellStyle(headerCellStyle);
    }

    CollectionDatasource datasource = table.getDatasource();
    if (exportMode == ExportMode.SELECTED_ROWS && table.getSelected().size() > 0) {
        Set<Entity> selected = table.getSelected();
        List<Entity> ordered = ((Collection<Entity>) datasource.getItems()).stream().filter(selected::contains)
                .collect(Collectors.toList());
        for (Entity item : ordered) {
            createRow(table, columns, 0, ++r, item.getId());
        }
    } else {
        if (table instanceof TreeTable) {
            TreeTable treeTable = (TreeTable) table;
            HierarchicalDatasource ds = treeTable.getDatasource();
            if (table.isAggregatable()) {
                r = createAggregatableRow(table, columns, ++r, 1, datasource);
            }
            for (Object itemId : ds.getRootItemIds()) {
                r = createHierarhicalRow(treeTable, columns, exportExpanded, r, itemId);
            }
        } else if (table instanceof GroupTable && datasource instanceof GroupDatasource
                && ((GroupDatasource) datasource).hasGroups()) {
            GroupDatasource ds = (GroupDatasource) datasource;
            if (table.isAggregatable()) {
                r = createAggregatableRow(table, columns, ++r, 1, datasource);
            }
            for (Object item : ds.rootGroups()) {
                r = createGroupRow((GroupTable) table, columns, ++r, (GroupInfo) item, 0);
            }
        } else {
            if (table.isAggregatable()) {
                r = createAggregatableRow(table, columns, ++r, 1, datasource);
            }
            for (Object itemId : datasource.getItemIds()) {
                createRow(table, columns, 0, ++r, itemId);
            }
        }
    }

    for (int c = 0; c < columns.size(); c++) {
        sheet.setColumnWidth(c, sizers[c].getWidth() * COL_WIDTH_MAGIC);
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        wb.write(out);
    } catch (IOException e) {
        throw new RuntimeException("Unable to write document", e);
    }
    if (fileName == null) {
        fileName = messages.getTools().getEntityCaption(datasource.getMetaClass());
    }

    display.show(new ByteArrayDataProvider(out.toByteArray()), fileName + ".xls", ExportFormat.XLS);
}

From source file:com.haulmont.cuba.gui.export.ExcelExporter.java

License:Apache License

public void exportDataGrid(DataGrid<Entity> dataGrid, List<DataGrid.Column> columns, ExportDisplay display,
        List<String> filterDescription, String fileName, ExportMode exportMode) {
    if (display == null) {
        throw new IllegalArgumentException("ExportDisplay is null");
    }//from  w  ww  .  j av a 2s  .c o  m

    createWorkbookWithSheet();
    createFonts();
    createFormats();

    int r = 0;
    if (filterDescription != null) {
        for (r = 0; r < filterDescription.size(); r++) {
            String line = filterDescription.get(r);
            HSSFRow row = sheet.createRow(r);
            if (r == 0) {
                HSSFRichTextString richTextFilterName = new HSSFRichTextString(line);
                richTextFilterName.applyFont(boldFont);
                row.createCell(0).setCellValue(richTextFilterName);
            } else {
                row.createCell(0).setCellValue(line);
            }
        }
        r++;
    }
    HSSFRow row = sheet.createRow(r);
    createAutoColumnSizers(columns.size());

    float maxHeight = sheet.getDefaultRowHeightInPoints();

    CellStyle headerCellStyle = wb.createCellStyle();
    headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    for (DataGrid.Column column : columns) {
        String caption = column.getCaption();

        int countOfReturnSymbols = StringUtils.countMatches(caption, "\n");
        if (countOfReturnSymbols > 0) {
            maxHeight = Math.max(maxHeight, (countOfReturnSymbols + 1) * sheet.getDefaultRowHeightInPoints());
            headerCellStyle.setWrapText(true);
        }
    }
    row.setHeightInPoints(maxHeight);

    for (int c = 0; c < columns.size(); c++) {
        DataGrid.Column column = columns.get(c);
        String caption = column.getCaption();

        HSSFCell cell = row.createCell(c);
        HSSFRichTextString richTextString = new HSSFRichTextString(caption);
        richTextString.applyFont(boldFont);
        cell.setCellValue(richTextString);

        ExcelAutoColumnSizer sizer = new ExcelAutoColumnSizer();
        sizer.notifyCellValue(caption, boldFont);
        sizers[c] = sizer;

        cell.setCellStyle(headerCellStyle);
    }

    CollectionDatasource datasource = dataGrid.getDatasource();
    if (exportMode == ExportMode.SELECTED_ROWS && dataGrid.getSelected().size() > 0) {
        Set<Entity> selected = dataGrid.getSelected();
        List<Entity> ordered = ((Collection<Entity>) datasource.getItems()).stream().filter(selected::contains)
                .collect(Collectors.toList());
        for (Entity item : ordered) {
            createDataGridRow(dataGrid, columns, 0, ++r, item.getId());
        }
    } else {
        for (Object itemId : datasource.getItemIds()) {
            createDataGridRow(dataGrid, columns, 0, ++r, itemId);
        }
    }

    for (int c = 0; c < columns.size(); c++) {
        sheet.setColumnWidth(c, sizers[c].getWidth() * COL_WIDTH_MAGIC);
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        wb.write(out);
    } catch (IOException e) {
        throw new RuntimeException("Unable to write document", e);
    }
    if (fileName == null) {
        fileName = messages.getTools().getEntityCaption(datasource.getMetaClass());
    }

    display.show(new ByteArrayDataProvider(out.toByteArray()), fileName + ".xls", ExportFormat.XLS);
}

From source file:com.heimaide.server.common.utils.excel.ExportExcel.java

License:Open Source License

/**
 * ?//w ww .  ja v a  2s  .c om
 * 
 * @param wb
 *            
 * @return ?
 */
private Map<String, CellStyle> createStyles(Workbook wb) {
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();

    CellStyle style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    Font titleFont = wb.createFont();
    titleFont.setFontName("Arial");
    titleFont.setFontHeightInPoints((short) 16);
    titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style.setFont(titleFont);
    styles.put("title", style);
    style.setBorderTop(CellStyle.BORDER_THIN);
    style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setBorderLeft(CellStyle.BORDER_THIN);
    style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setBorderBottom(CellStyle.BORDER_THIN);
    style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_RIGHT);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    Font titleFont1 = wb.createFont();
    titleFont1.setFontName("Arial");
    titleFont1.setFontHeightInPoints((short) 10);
    titleFont1.setBoldweight(Font.BOLDWEIGHT_BOLD);
    titleFont1.setColor(Font.COLOR_RED);
    style.setFont(titleFont1);
    styles.put("title1", style);
    style.setBorderRight(CellStyle.BORDER_THIN);
    style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setBorderTop(CellStyle.BORDER_THIN);
    style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setBorderBottom(CellStyle.BORDER_THIN);
    style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());

    style = wb.createCellStyle();
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setBorderRight(CellStyle.BORDER_THIN);
    style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setBorderLeft(CellStyle.BORDER_THIN);
    style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setBorderTop(CellStyle.BORDER_THIN);
    style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setBorderBottom(CellStyle.BORDER_THIN);
    style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    Font dataFont = wb.createFont();
    dataFont.setFontName("Arial");
    dataFont.setFontHeightInPoints((short) 10);
    style.setFont(dataFont);
    styles.put("data", style);

    style = wb.createCellStyle();
    style.cloneStyleFrom(styles.get("data"));
    style.setAlignment(CellStyle.ALIGN_LEFT);
    styles.put("data1", style);

    style = wb.createCellStyle();
    style.cloneStyleFrom(styles.get("data"));
    style.setAlignment(CellStyle.ALIGN_CENTER);
    styles.put("data2", style);

    style = wb.createCellStyle();
    style.cloneStyleFrom(styles.get("data"));
    style.setAlignment(CellStyle.ALIGN_RIGHT);
    styles.put("data3", style);

    style = wb.createCellStyle();
    style.cloneStyleFrom(styles.get("data"));
    // style.setWrapText(true);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    Font headerFont = wb.createFont();
    headerFont.setFontName("Arial");
    headerFont.setFontHeightInPoints((short) 10);
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    headerFont.setColor(IndexedColors.WHITE.getIndex());
    style.setFont(headerFont);
    styles.put("header", style);

    return styles;
}

From source file:com.helger.poi.excel.style.ExcelStyle.java

License:Apache License

public void fillCellStyle(@Nonnull final Workbook aWB, @Nonnull final CellStyle aCS,
        @Nonnull final CreationHelper aCreationHelper) {
    if (m_eAlign != null)
        aCS.setAlignment(m_eAlign.getValue());
    if (m_eVAlign != null)
        aCS.setVerticalAlignment(m_eVAlign.getValue());
    aCS.setWrapText(m_bWrapText);/*  w w  w . j av a2s. c o  m*/
    if (m_sDataFormat != null)
        aCS.setDataFormat(aCreationHelper.createDataFormat().getFormat(m_sDataFormat));
    if (m_eFillBackgroundColor != null)
        aCS.setFillBackgroundColor(m_eFillBackgroundColor.getIndex());
    if (m_eFillForegroundColor != null)
        aCS.setFillForegroundColor(m_eFillForegroundColor.getIndex());
    if (m_eFillPattern != null)
        aCS.setFillPattern(m_eFillPattern.getValue());
    if (m_eBorderTop != null)
        aCS.setBorderTop(m_eBorderTop.getValue());
    if (m_eBorderRight != null)
        aCS.setBorderRight(m_eBorderRight.getValue());
    if (m_eBorderBottom != null)
        aCS.setBorderBottom(m_eBorderBottom.getValue());
    if (m_eBorderLeft != null)
        aCS.setBorderLeft(m_eBorderLeft.getValue());
    if (m_nFontIndex >= 0)
        aCS.setFont(aWB.getFontAt(m_nFontIndex));
}

From source file:com.ideaspymes.proyecttemplate.stock.web.ProductoConsultaBean.java

@Override
public Workbook getWorkBook() {
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("My Sample Excel");
    List<CatalogoProductos> lista = (List<CatalogoProductos>) getDetalles();

    sheet.setDefaultRowHeight((short) (sheet.getDefaultRowHeight() * new Short("6")));

    org.apache.poi.ss.usermodel.Font fontTitulo = wb.createFont();
    fontTitulo.setFontHeightInPoints((short) 12);
    fontTitulo.setBoldweight(org.apache.poi.ss.usermodel.Font.BOLDWEIGHT_BOLD);

    org.apache.poi.ss.usermodel.Font fontTituloPricipal = wb.createFont();
    fontTituloPricipal.setFontHeightInPoints((short) 22);
    fontTituloPricipal.setBoldweight(org.apache.poi.ss.usermodel.Font.BOLDWEIGHT_BOLD);

    DataFormat format = wb.createDataFormat();

    CellStyle styleTituloPrincipal = wb.createCellStyle();
    styleTituloPrincipal.setFont(fontTituloPricipal);
    styleTituloPrincipal.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    styleTituloPrincipal.setAlignment(CellStyle.ALIGN_CENTER);

    CellStyle styleTitulo = wb.createCellStyle();
    styleTitulo.setFont(fontTitulo);//w  ww .j av  a2 s . c o m
    styleTitulo.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    styleTitulo.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
    styleTitulo.setFillPattern(CellStyle.SOLID_FOREGROUND);
    styleTitulo.setWrapText(true);

    CellStyle styleNumero = wb.createCellStyle();
    styleNumero.setDataFormat(format.getFormat("#,##0"));
    styleNumero.setWrapText(true);
    styleNumero.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    styleNumero.setAlignment(CellStyle.ALIGN_CENTER);

    CellStyle styleFecha = wb.createCellStyle();
    styleFecha.setDataFormat(format.getFormat("dd/MM/yyyy"));
    styleFecha.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    styleFecha.setAlignment(CellStyle.ALIGN_CENTER);

    CellStyle style = wb.createCellStyle();
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setWrapText(true);

    CellStyle styleCenter = wb.createCellStyle();
    styleCenter.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    styleCenter.setAlignment(CellStyle.ALIGN_CENTER);
    styleCenter.setWrapText(true);

    Row rowTitle = sheet.createRow(0);
    Cell cellTitle = rowTitle.createCell(1);
    cellTitle.setCellStyle(styleTituloPrincipal);

    sheet.addMergedRegion(new CellRangeAddress(0, //first row (0-based)
            1, //last row  (0-based)
            1, //first column (0-based)
            11 //last column  (0-based)
    ));

    cellTitle.setCellValue("Listado de Activos");

    int i = 2;

    Row row0 = sheet.createRow(i);
    row0.setHeight((short) 500);

    Cell cell1 = row0.createCell(1);
    cell1.setCellValue("Foto");
    cell1.setCellStyle(styleTitulo);

    Cell cellFecha = row0.createCell(3);
    cellFecha.setCellValue("Fecha Ingreso");
    cellFecha.setCellStyle(styleTitulo);

    Cell cellFechaCarga = row0.createCell(4);
    cellFechaCarga.setCellValue("Fecha Carga");
    cellFechaCarga.setCellStyle(styleTitulo);

    Cell cell3 = row0.createCell(5);
    cell3.setCellValue("Nombre");
    cell3.setCellStyle(styleTitulo);

    Cell cell4 = row0.createCell(6);
    cell4.setCellValue("Cdigo");
    cell4.setCellStyle(styleTitulo);

    Cell cell5 = row0.createCell(7);
    cell5.setCellValue("Descripcin");
    cell5.setCellStyle(styleTitulo);

    Cell cell6 = row0.createCell(8);
    cell6.setCellValue("Es Regalo?");
    cell6.setCellStyle(styleTitulo);

    Cell cell7 = row0.createCell(9);
    cell7.setCellValue("Familia");
    cell7.setCellStyle(styleTitulo);

    Cell cell8 = row0.createCell(10);
    cell8.setCellValue("Ubicaciones");
    cell8.setCellStyle(styleTitulo);

    Cell cell9 = row0.createCell(11);
    cell9.setCellValue("Stock");
    cell9.setCellStyle(styleTitulo);

    for (CatalogoProductos cp : lista) {

        int indexFila = i + 1;
        if (cp.getImagen() != null) {
            int pictureIdx = wb.addPicture(cp.getImagen(), Workbook.PICTURE_TYPE_PNG);
            CreationHelper helper = wb.getCreationHelper();

            //Creates the top-level drawing patriarch.
            Drawing drawing = sheet.createDrawingPatriarch();

            //Create an anchor that is attached to the worksheet
            ClientAnchor anchor = helper.createClientAnchor();
            //set top-left corner for the image
            anchor.setCol1(1);
            anchor.setRow1(indexFila);

            //Creates a picture
            Picture pict = drawing.createPicture(anchor, pictureIdx);
            //Reset the image to the original size
            pict.resize(0.4);
        }
        Row row1 = sheet.createRow(indexFila);
        row1.setHeightInPoints(80f);

        Cell cellColFecha = row1.createCell(3);

        if (cp.getFecha() != null) {
            cellColFecha.setCellValue(cp.getFecha());
            cellColFecha.setCellStyle(styleFecha);

        } else {
            cellColFecha.setCellValue("");
            cellColFecha.setCellStyle(styleFecha);
        }

        Cell cellColFechaCarga = row1.createCell(4);

        if (cp.getFechaCarga() != null) {
            cellColFechaCarga.setCellValue(cp.getFechaCarga());
            cellColFechaCarga.setCellStyle(styleFecha);

        } else {
            cellColFechaCarga.setCellValue("");
            cellColFechaCarga.setCellStyle(styleFecha);
        }

        Cell cellCol1 = row1.createCell(5);
        cellCol1.setCellValue(cp.getProducto());
        cellCol1.setCellStyle(style);

        Cell cellCol2 = row1.createCell(6);
        cellCol2.setCellValue(cp.getCodigo());
        cellCol2.setCellStyle(styleNumero);

        Cell cellCol3 = row1.createCell(7);
        cellCol3.setCellValue(cp.getDescripcion());
        cellCol3.setCellStyle(style);

        Cell cellCol4 = row1.createCell(8);
        cellCol4.setCellValue(cp.isEsRegalo() ? "SI" : "NO");
        cellCol4.setCellStyle(styleCenter);

        Cell cellCol5 = row1.createCell(9);
        cellCol5.setCellValue(cp.getFamilia());
        cellCol5.setCellStyle(style);

        Cell cellCol6 = row1.createCell(10);
        cellCol6.setCellValue(cp.getUbicaciones());
        cellCol6.setCellStyle(style);

        Cell cellCol7 = row1.createCell(11);
        cellCol7.setCellValue(cp.getStock());
        cellCol7.setCellStyle(styleNumero);

        i++;

    }

    sheet.setColumnWidth(1, 4000);
    sheet.setColumnWidth(2, 0);
    sheet.setColumnWidth(3, 4000);
    sheet.setColumnWidth(4, 4000);
    sheet.setColumnWidth(5, 10000);
    sheet.setColumnWidth(6, 3000);
    sheet.setColumnWidth(7, 10000);
    sheet.setColumnWidth(8, 3500);
    sheet.setColumnWidth(9, 6000);
    sheet.setColumnWidth(10, 10000);
    sheet.setColumnWidth(11, 2000);

    return wb;
}