Example usage for org.apache.poi.xssf.usermodel XSSFCellStyle setFillPattern

List of usage examples for org.apache.poi.xssf.usermodel XSSFCellStyle setFillPattern

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFCellStyle setFillPattern.

Prototype

@Override
public void setFillPattern(FillPatternType pattern) 

Source Link

Document

This element is used to specify cell fill information for pattern and solid color cell fills.

Usage

From source file:tr.org.liderahenk.liderconsole.core.utils.SWTResourceManager.java

License:Open Source License

private static XSSFWorkbook createWorkbookFromTable(TableViewer tableViewer, String sheetName) {

    // Create workbook & sheet
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet(sheetName == null ? "Sheet1" : sheetName);

    // Shade the background of the header row
    XSSFCellStyle headerStyle = wb.createCellStyle();
    headerStyle.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    headerStyle.setBorderTop(CellStyle.BORDER_THIN);
    headerStyle.setBorderBottom(CellStyle.BORDER_THIN);
    headerStyle.setBorderLeft(CellStyle.BORDER_THIN);
    headerStyle.setBorderRight(CellStyle.BORDER_THIN);
    headerStyle.setAlignment(HorizontalAlignment.CENTER);

    // Add header row
    Table table = tableViewer.getTable();
    TableColumn[] columns = table.getColumns();
    int rowIndex = 0;
    int cellIndex = 0;
    XSSFRow header = sheet.createRow((short) rowIndex++);
    for (TableColumn column : columns) {
        XSSFCell cell = header.createCell(cellIndex++);
        cell.setCellValue(column.getText());
        cell.setCellStyle(headerStyle);//w  ww .ja  v a 2 s .  co m
    }

    // Add data rows
    TableItem[] items = tableViewer.getTable().getItems();
    for (TableItem item : items) {
        // create a new row
        XSSFRow row = sheet.createRow((short) rowIndex++);
        cellIndex = 0;

        for (int i = 0; i < columns.length; i++) {
            // Create a new cell
            XSSFCell cell = row.createCell(cellIndex++);
            String text = item.getText(i);

            // Set the horizontal alignment (default to RIGHT)
            XSSFCellStyle cellStyle = wb.createCellStyle();
            if (LiderCoreUtils.isInteger(text)) {
                cellStyle.setAlignment(HorizontalAlignment.RIGHT);
            } else if (LiderCoreUtils.isValidDate(text,
                    ConfigProvider.getInstance().get(LiderConstants.CONFIG.DATE_FORMAT))) {
                cellStyle.setAlignment(HorizontalAlignment.CENTER);
            } else {
                cellStyle.setAlignment(HorizontalAlignment.LEFT);
            }
            cell.setCellStyle(cellStyle);

            // Set the cell's value
            cell.setCellValue(text);
        }
    }

    // Auto-fit the columns
    for (int i = 0; i < columns.length; i++) {
        sheet.autoSizeColumn((short) i);
    }

    return wb;
}

From source file:uk.co.certait.htmlexporter.writer.excel.ExcelStyleGenerator.java

License:Apache License

protected void applyBackground(Style style, XSSFCellStyle cellStyle) {
    if (style.isBackgroundSet()) {
        cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(new XSSFColor(style.getProperty(CssColorProperty.BACKGROUND)));
    }// ww w  .  ja  v  a2 s  . c o m
}

From source file:uk.co.spudsoft.birt.emitters.excel.StyleManagerXUtils.java

License:Open Source License

@Override
public void addBackgroundColourToStyle(Workbook workbook, CellStyle style, String colour) {
    if (colour == null) {
        return;//w w  w.ja va2 s  .  co  m
    }
    if (IStyle.TRANSPARENT_VALUE.equals(colour)) {
        return;
    }
    if (style instanceof XSSFCellStyle) {
        XSSFCellStyle cellStyle = (XSSFCellStyle) style;
        XSSFColor xColour = getXColour(colour);
        if (xColour != null) {
            cellStyle.setFillForegroundColor(xColour);
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        }
    }
}

From source file:uk.gov.ofwat.fountain.api.report.PoiReportBuilder.java

License:Open Source License

private void makeHeaderCell(String content, short colour) {
    Cell cell = row.createCell(cellIdx++);
    cell.setCellType(Cell.CELL_TYPE_STRING);
    RichTextString rts = creationHelper.createRichTextString(content);
    cell.setCellValue(rts);/*from  w ww . jav a2  s. c  o m*/
    XSSFCellStyle style = workBook.createCellStyle();
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFillForegroundColor(colour);
    style.setFont(font);
    cell.setCellStyle(style);
}

From source file:uk.gov.ofwat.fountain.api.report.POIReportWriter.java

License:Open Source License

private XSSFCellStyle getInputDataNumericStyle(DataFormat format, DataDto dataDto) {
    short formatIndex = cellFormat(format, dataDto);
    if (inputDataNumericStyleMap.containsKey(formatIndex)) {
        return inputDataNumericStyleMap.get(formatIndex);
    }/*  w w w. ja v  a  2  s  .  c om*/
    XSSFCellStyle style = workBook.createCellStyle();
    style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    style.setFillForegroundColor(lightYellow);
    style.setDataFormat(formatIndex);
    inputDataNumericStyleMap.put(formatIndex, style);
    return style;
}

From source file:uk.gov.ofwat.fountain.api.report.POIReportWriter.java

License:Open Source License

private XSSFCellStyle getCalcDataNumericStyle(DataFormat format, DataDto dataDto) {
    short formatIndex = cellFormat(format, dataDto);
    if (calcDataNumericStyleMap.containsKey(formatIndex)) {
        return calcDataNumericStyleMap.get(formatIndex);
    }//from  w w w . ja  va 2  s . c  o  m
    XSSFCellStyle style = workBook.createCellStyle();
    style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    style.setFillForegroundColor(lightBlue);
    style.setDataFormat(formatIndex);
    calcDataNumericStyleMap.put(formatIndex, style);
    return style;
}

From source file:uk.gov.ofwat.fountain.api.table.POITableRenderer.java

License:Open Source License

private XSSFCellStyle getInputDataNumericStyle(DataFormat format, DataDto dataDto) {
    short formatIndex = cellFormat(format, dataDto);
    if (inputDataNumericStyleMap.containsKey(formatIndex)) {
        return inputDataNumericStyleMap.get(formatIndex);
    }/*from   www  . j  a  v a2  s  .co  m*/
    XSSFCellStyle style = workBook.createCellStyle();
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFillForegroundColor(lightYellow);
    style.setDataFormat(formatIndex);
    inputDataNumericStyleMap.put(formatIndex, style);
    return style;
}

From source file:uk.gov.ofwat.fountain.api.table.POITableRenderer.java

License:Open Source License

private XSSFCellStyle getCopyCellDataNumericStyle(DataFormat format, DataDto dataDto) {
    short formatIndex = cellFormat(format, dataDto);
    if (copyCellDataNumericStyleMap.containsKey(formatIndex)) {
        return copyCellDataNumericStyleMap.get(formatIndex);
    }/*from  w  ww.j av a  2  s  .co m*/
    XSSFCellStyle style = workBook.createCellStyle();
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFillForegroundColor(pink);
    style.setDataFormat(formatIndex);
    copyCellDataNumericStyleMap.put(formatIndex, style);
    return style;
}

From source file:uk.gov.ofwat.fountain.api.table.POITableRenderer.java

License:Open Source License

private XSSFCellStyle getCalcDataNumericStyle(DataFormat format, DataDto dataDto) {
    short formatIndex = cellFormat(format, dataDto);
    if (calcDataNumericStyleMap.containsKey(formatIndex)) {
        return calcDataNumericStyleMap.get(formatIndex);
    }//from  w ww. j  av  a 2  s . c  o m
    XSSFCellStyle style = workBook.createCellStyle();
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFillForegroundColor(lightBlue);
    style.setDataFormat(formatIndex);
    calcDataNumericStyleMap.put(formatIndex, style);
    return style;
}

From source file:vd7_cellstyle.CellStyle.java

public static void main(String[] args) throws FileNotFoundException, IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet spreadsheet = workbook.createSheet("cellstyle");
    XSSFRow row = spreadsheet.createRow((short) 1); //row 2 in excel

    //SET HEIGHT OF ROW 2 (in excel)
    row.setHeight((short) 800);
    XSSFCell cell = (XSSFCell) row.createCell((short) 1);

    cell.setCellValue("test of merging");
    //MEARGING CELLS 
    //this statement for merging cells
    spreadsheet.addMergedRegion(new CellRangeAddress(1, //first row (0-based)
            1, //last row (0-based)
            1, //first column (0-based)
            4 //last column (0-based)
    ));/*from  w ww  .  ja  va2s.com*/

    //CELL Alignment
    row = spreadsheet.createRow(5); //row 6 (in excel)
    cell = (XSSFCell) row.createCell(0);
    row.setHeight((short) 800);
    // Top Left alignment 
    XSSFCellStyle style1 = workbook.createCellStyle();
    spreadsheet.setColumnWidth(0, 8000);
    style1.setAlignment(XSSFCellStyle.ALIGN_LEFT);
    style1.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP);
    cell.setCellValue("Top Left");
    //apply the style
    cell.setCellStyle(style1);

    row = spreadsheet.createRow(6);//row 7 in excel
    cell = (XSSFCell) row.createCell(1);
    row.setHeight((short) 800);
    // Center Align Cell Contents 
    XSSFCellStyle style2 = workbook.createCellStyle();
    style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
    style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
    cell.setCellValue("Center Aligned");
    //apply the style
    cell.setCellStyle(style2);

    row = spreadsheet.createRow(7); //row 8 in excel
    cell = (XSSFCell) row.createCell(2);
    row.setHeight((short) 800);
    // Bottom Right alignment 
    XSSFCellStyle style3 = workbook.createCellStyle();
    style3.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
    style3.setVerticalAlignment(XSSFCellStyle.VERTICAL_BOTTOM);
    cell.setCellValue("Bottom Right");
    //apply the style
    cell.setCellStyle(style3);

    row = spreadsheet.createRow(8);//row 9 in excel
    cell = (XSSFCell) row.createCell(3);
    // Justified Alignment (cn ?u trong )
    XSSFCellStyle style4 = workbook.createCellStyle();
    style4.setAlignment(XSSFCellStyle.ALIGN_JUSTIFY);
    style4.setVerticalAlignment(XSSFCellStyle.VERTICAL_JUSTIFY);
    cell.setCellValue("Contents are Justified in Alignment");
    cell.setCellStyle(style4);

    //CELL BORDER
    row = spreadsheet.createRow((short) 9); //row 10 in excel
    row.setHeight((short) 800);
    cell = (XSSFCell) row.createCell((short) 0);
    cell.setCellValue("BORDER");
    XSSFCellStyle style5 = workbook.createCellStyle();
    //set bottom border which is thick line
    style5.setBorderBottom(XSSFCellStyle.BORDER_THICK);
    //set color of bottom border
    style5.setBottomBorderColor(IndexedColors.BLUE.getIndex());
    style5.setBorderLeft(XSSFCellStyle.BORDER_DOUBLE);
    style5.setLeftBorderColor(IndexedColors.GREEN.getIndex());
    style5.setBorderRight(XSSFCellStyle.BORDER_HAIR);
    style5.setRightBorderColor(IndexedColors.RED.getIndex());
    style5.setBorderTop(XSSFCellStyle.BIG_SPOTS);
    style5.setTopBorderColor(IndexedColors.CORAL.getIndex());
    cell.setCellStyle(style5);

    //Fill Colors
    //background color
    row = spreadsheet.createRow((short) 10);
    cell = (XSSFCell) row.createCell((short) 1);
    XSSFCellStyle style6 = workbook.createCellStyle();
    style6.setFillBackgroundColor(HSSFColor.LEMON_CHIFFON.index);
    style6.setFillPattern(XSSFCellStyle.LESS_DOTS);
    spreadsheet.setColumnWidth(1, 8000);
    cell.setCellValue("FILL BACKGROUNG/FILL PATTERN");
    cell.setCellStyle(style6);

    FileOutputStream out = new FileOutputStream(new File("src\\vd7_cellstyle\\cellstyle.xlsx"));
    workbook.write(out);
    out.close();
    System.out.println("cellstyle.xlsx written successfully");
}