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

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

Introduction

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

Prototype

@Override
public void setFillForegroundColor(short fg) 

Source Link

Document

Set the foreground fill color as a indexed color value
Note: Ensure Foreground color is set prior to background color.

Usage

From source file:com.cms.utils.CommonUtils.java

public static XSSFCellStyle styleCell(XSSFWorkbook workbook) {
    XSSFCellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
    cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
    cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
    cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
    cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
    cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
    cellStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
    cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    cellStyle.setWrapText(false);//from   w  w w .  j  a va2 s. c om
    return cellStyle;
}

From source file:com.dfpray.formatter.CardModel.java

/**
 * Exports List of B.C to Excel file, Path should include name and format .xlsx ending
 * @param path/*w w w .  j av a 2 s  .  c om*/
 * @throws IOException 
 */
public void exportToExcel(String path) throws IOException {
    System.out.println("Called ");

    BusinessCard card;
    Cell cell;
    String[] info;
    Double number;
    String cardInfo;
    Row row;

    //Create Blank workbook/sheet
    @SuppressWarnings("resource")
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Business Data");
    String[] tmpArray = { "CompanyName", "ContactFirstName", "ContactLastName", "Title", "Street Address",
            "Suite/PO Box", "City", "State", "ZipCode", "Country", "PhoneNumber", "Extension", "MobilePhone",
            "FaxNumber", "EmailAddress", "Website", "CsiCodes", "CompanyFunction", "MBEAffiliations", "Labor",
            "ServiceArea", "CompanyNotes", "ContactLists", "CF_Alternate Email", "CF_Do Not Use",
            "CF_Supplier/Manuf", "CF_Trade", "CF_Union Value", "CF_Unlicensed States", "CF_Will Not Bid" };

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

    XSSFCellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setFont(headerFont);

    XSSFCellStyle cellStyle2 = workbook.createCellStyle();
    cellStyle2.setFont(headerFont);
    cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
    cellStyle2.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

    //Write Template

    row = sheet.createRow(0);
    for (int k = 0; k < 30; k++) {
        cell = row.createCell(k);
        cell.setCellStyle(cellStyle);

        if (k == 0 || k == 13 || k == 14 || k == 16 || k == 17) {
            cell.setCellStyle(cellStyle2);
        }

        cell.setCellValue(tmpArray[k]);
    }

    //Row = Business
    for (int i = 1; i <= amtCards(); i++) {
        row = sheet.createRow(i);
        card = cards.get(i - 1);
        info = card.infoToArray();

        //Create Column = Data for each Business
        for (int k = 0; k < 30; k++) {
            cardInfo = info[k];
            cell = row.createCell(k);

            if (k == 24)
                continue;

            try {
                number = Double.parseDouble(cardInfo);
                cell.setCellValue(number);
            } catch (NumberFormatException e) {
                cell.setCellValue(cardInfo);
            }

        }
        card.setExported(true);
    }

    //Create file system using specific name
    FileOutputStream out;
    try {
        out = new FileOutputStream(new File(path));
    } catch (FileNotFoundException e) {

        //Reset cards to not exported
        for (BusinessCard cardR : cards) {
            cardR.setExported(false);
        }
        throw new IOException();
    }
    workbook.write(out);
    out.close();

}

From source file:com.envision.envservice.report.template.PraiseReportExcel.java

License:Open Source License

private static XSSFCellStyle buildStyle(XSSFWorkbook workbook, Color color) {
    XSSFCellStyle style = workbook.createCellStyle();
    style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    style.setFillForegroundColor(new XSSFColor(color));

    return style;
}

From source file:com.frameworkset.platform.sanylog.common.POIExcelUtil2007.java

License:Open Source License

private static XSSFCellStyle getHeadCellStyle(XSSFWorkbook wb, XSSFFont font) {//
    XSSFCellStyle headCellStyle = wb.createCellStyle();
    headCellStyle.setFont(font);//  w ww.  j  av  a2  s  .c o m
    headCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    headCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    headCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
    headCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
    headCellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    headCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    headCellStyle.setWrapText(false);
    headCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    headCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    return headCellStyle;
}

From source file:com.frameworkset.platform.util.POIExcelUtil.java

License:Open Source License

public static XSSFCellStyle getHeadCellStyle(XSSFWorkbook wb, XSSFFont font) {
    XSSFCellStyle headCellStyle = wb.createCellStyle();
    headCellStyle.setFont(font);//from   ww w . j av a2  s  .  c om
    headCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    headCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    headCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
    headCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
    headCellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    headCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    headCellStyle.setWrapText(false);
    headCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    headCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    return headCellStyle;
}

From source file:com.github.igor_kudryashov.utils.excel.ExcelWriter.java

License:Apache License

/**
 * Returns a style of cell/*from w w  w .  j ava 2  s.  c  o m*/
 *
 * @param rownum
 *            the number of row for count odd/even rows
 * @param entry
 *            value of cell
 * @param header
 *            <code>true</code> if this row is the header, otherwise
 *            <code>false</code>
 * @return the cell style
 */
private XSSFCellStyle getCellStyle(int rownum, Object entry, boolean header) {
    XSSFCellStyle style;
    String name = entry.getClass().getName();
    if (header) {
        name += "_header";
    } else if ((rownum % 2) == 0) {
        name += "_even";
    }
    if (styles.containsKey(name)) {
        // if we already have a style for this class, return it
        style = styles.get(name);
    } else {
        // create new style
        style = (XSSFCellStyle) workbook.createCellStyle();
        style.setVerticalAlignment(VerticalAlignment.TOP);
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBorderColor(XSSFCellBorder.BorderSide.BOTTOM, colorBorder);
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setBorderColor(XSSFCellBorder.BorderSide.LEFT, colorBorder);
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setBorderColor(XSSFCellBorder.BorderSide.RIGHT, colorBorder);
        // format data
        XSSFDataFormat fmt = (XSSFDataFormat) workbook.createDataFormat();
        short format = 0;
        if (name.contains("Date")) {
            format = fmt.getFormat(BuiltinFormats.getBuiltinFormat(0xe));
            style.setAlignment(CellStyle.ALIGN_LEFT);
        } else if (name.contains("Double")) {
            format = fmt.getFormat(BuiltinFormats.getBuiltinFormat(2));
            style.setAlignment(CellStyle.ALIGN_RIGHT);
        } else if (name.contains("Integer")) {
            format = fmt.getFormat(BuiltinFormats.getBuiltinFormat(1));
            style.setAlignment(CellStyle.ALIGN_RIGHT);
        } else {
            style.setAlignment(CellStyle.ALIGN_LEFT);
            if (!header) {
                style.setWrapText(true);
            }
        }

        if (header) {
            // for header
            style.setFillForegroundColor(colorHeaderBackground);
            style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        } else if (name.contains("_even")) {
            // for even rows
            style.setFillForegroundColor(colorEvenCellBackground);
            style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        }
        style.setDataFormat(format);
        // keep the style for reuse
        styles.put(name, style);
    }
    return style;
}

From source file:com.hauldata.dbpa.file.book.XlsxTargetSheet.java

License:Apache License

/**
 * Translate styling to workbook CellStyle.
 *
 * @param stylesUsed tracks the styles that have been used in the workbook; it will be updated
 * @param fontsUsed tracks the fonts that have been used in the workbook; it may be updated
 * @param colorsUsed tracks the colors that have been used in the workbook; it may be updated
 *///from   w w w .j  av  a  2s  .  co  m
public CellStyle getCellStyle(SXSSFWorkbook book, Map<StylesWithFormatting, XSSFCellStyle> stylesUsed,
        Map<FontStyles, XSSFFont> fontsUsed, Map<Integer, XSSFColor> colorsUsed) {

    XSSFCellStyle cellStyle = stylesUsed.get(this);
    if (cellStyle != null) {
        return cellStyle;
    }

    cellStyle = (XSSFCellStyle) book.createCellStyle();
    cellStyle.cloneStyleFrom(book.getCellStyleAt(formatIndex));

    if (styles.bottomBorder.style != null) {
        cellStyle.setBorderBottom(resolveBorderStyle(styles.bottomBorder));
    }
    if (styles.leftBorder.style != null) {
        cellStyle.setBorderLeft(resolveBorderStyle(styles.leftBorder));
    }
    if (styles.rightBorder.style != null) {
        cellStyle.setBorderRight(resolveBorderStyle(styles.rightBorder));
    }
    if (styles.topBorder.style != null) {
        cellStyle.setBorderTop(resolveBorderStyle(styles.topBorder));
    }

    if (styles.bottomBorder.color != null) {
        cellStyle.setBottomBorderColor(getColor(styles.bottomBorder.color, book, colorsUsed));
    }
    if (styles.leftBorder.color != null) {
        cellStyle.setLeftBorderColor(getColor(styles.leftBorder.color, book, colorsUsed));
    }
    if (styles.rightBorder.color != null) {
        cellStyle.setRightBorderColor(getColor(styles.rightBorder.color, book, colorsUsed));
    }
    if (styles.topBorder.color != null) {
        cellStyle.setTopBorderColor(getColor(styles.topBorder.color, book, colorsUsed));
    }

    if (styles.backgroundColor != null) {
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(getColor(styles.backgroundColor, book, colorsUsed));
    }

    if (styles.textAlign != null) {
        cellStyle.setAlignment(styles.textAlign);
    }

    if (!styles.font.areDefault()) {
        cellStyle.setFont(getFont(styles.font, book, fontsUsed, colorsUsed));
    }

    stylesUsed.put(this, cellStyle);

    return cellStyle;
}

From source file:com.l3.info.magenda.emplois_du_temps.Workbook.java

public void addCellStyle(String nom, Categorie cat) {
    XSSFCellStyle style = (XSSFCellStyle) workbook.createCellStyle();
    XSSFColor border = new XSSFColor(cat.getBorder());
    style.setBorderBottom(BorderStyle.MEDIUM);
    style.setBottomBorderColor(border);//from ww  w .j  a  va2 s . c  om

    style.setBorderLeft(BorderStyle.MEDIUM);
    style.setLeftBorderColor(border);

    style.setBorderRight(BorderStyle.MEDIUM);
    style.setRightBorderColor(border);

    style.setBorderTop(BorderStyle.MEDIUM);
    style.setTopBorderColor(border);

    style.setWrapText(true);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setFillForegroundColor(new XSSFColor(cat.getBackground()));

    XSSFFont font = workbook.createFont();
    font.setColor(new XSSFColor(cat.getForeground()));
    style.setFont(font);

    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

    liste_des_styles.put("Style_" + nom, style);

}

From source file:com.netsteadfast.greenstep.bsc.command.KpiPeriodTrendsExcelCommand.java

License:Apache License

@SuppressWarnings("unchecked")
private void putTables(XSSFWorkbook wb, XSSFSheet sh, Context context) throws Exception {

    XSSFCellStyle cellHeadStyle = wb.createCellStyle();
    cellHeadStyle.setFillForegroundColor(new XSSFColor(SimpleUtils.getColorRGB4POIColor("#f5f5f5")));
    cellHeadStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    cellHeadStyle.setBorderBottom(BorderStyle.THIN);
    cellHeadStyle.setBorderTop(BorderStyle.THIN);
    cellHeadStyle.setBorderRight(BorderStyle.THIN);
    cellHeadStyle.setBorderLeft(BorderStyle.THIN);
    XSSFFont cellHeadFont = wb.createFont();
    cellHeadFont.setBold(true);/*from  www. java2s  . c o  m*/
    cellHeadStyle.setFont(cellHeadFont);

    sh.setColumnWidth(0, 12000);

    int row = 0;

    Row nowRow = sh.createRow(row);
    Cell cell1 = nowRow.createCell(0);
    cell1.setCellStyle(cellHeadStyle);
    cell1.setCellValue("KPI");
    Cell cell2 = nowRow.createCell(1);
    cell2.setCellStyle(cellHeadStyle);
    cell2.setCellValue("Maximum");
    Cell cell3 = nowRow.createCell(2);
    cell3.setCellStyle(cellHeadStyle);
    cell3.setCellValue("Target");
    Cell cell4 = nowRow.createCell(3);
    cell4.setCellStyle(cellHeadStyle);
    cell4.setCellValue("Minimum");
    Cell cell5 = nowRow.createCell(4);
    cell5.setCellStyle(cellHeadStyle);
    cell5.setCellValue("Current score");
    Cell cell6 = nowRow.createCell(5);
    cell6.setCellStyle(cellHeadStyle);
    cell6.setCellValue("Previous score");
    Cell cell7 = nowRow.createCell(6);
    cell7.setCellStyle(cellHeadStyle);
    cell7.setCellValue("Change(%)");

    row++;

    List<PeriodTrendsData<KpiVO>> periodDatas = (List<PeriodTrendsData<KpiVO>>) context.get("periodDatas");
    for (PeriodTrendsData<KpiVO> periodData : periodDatas) {
        nowRow = sh.createRow(row);

        cell1 = nowRow.createCell(0);
        cell1.setCellValue(periodData.getCurrent().getName());
        cell2 = nowRow.createCell(1);
        cell2.setCellValue(periodData.getCurrent().getMax());
        cell3 = nowRow.createCell(2);
        cell3.setCellValue(periodData.getCurrent().getTarget());
        cell4 = nowRow.createCell(3);
        cell4.setCellValue(periodData.getCurrent().getMin());
        cell5 = nowRow.createCell(4);
        cell5.setCellValue(BscReportSupportUtils.parse2(periodData.getCurrent().getScore()));
        cell6 = nowRow.createCell(5);
        cell6.setCellValue(BscReportSupportUtils.parse2(periodData.getPrevious().getScore()));
        cell7 = nowRow.createCell(6);
        cell7.setCellValue(BscReportSupportUtils.parse2(periodData.getChange()));

        row++;
    }

    nowRow = sh.createRow(row);

    cell1 = nowRow.createCell(0);
    cell1.setCellValue("Current period: " + (String) context.get("currentPeriodDateRange")
            + " , Previous period: " + (String) context.get("previousPeriodDateRange"));

}

From source file:com.netsteadfast.greenstep.bsc.command.KpiReportExcelCommand.java

License:Apache License

private int createHead(XSSFWorkbook wb, XSSFSheet sh, int row, VisionVO vision) throws Exception {
    Row headRow = sh.createRow(row);//from w w w  .  j a v a  2 s  .  c om
    headRow.setHeight((short) 700);

    int cell = 0;

    XSSFColor bgColor = new XSSFColor(SimpleUtils.getColorRGB4POIColor(vision.getBgColor()));
    XSSFColor fnColor = new XSSFColor(SimpleUtils.getColorRGB4POIColor(vision.getFontColor()));

    XSSFCellStyle cellHeadStyle = wb.createCellStyle();
    cellHeadStyle.setFillForegroundColor(bgColor);
    cellHeadStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

    XSSFFont cellHeadFont = wb.createFont();
    cellHeadFont.setBold(true);
    cellHeadFont.setColor(fnColor);
    cellHeadStyle.setFont(cellHeadFont);
    cellHeadStyle.setBorderBottom(BorderStyle.THIN);
    cellHeadStyle.setBorderTop(BorderStyle.THIN);
    cellHeadStyle.setBorderRight(BorderStyle.THIN);
    cellHeadStyle.setBorderLeft(BorderStyle.THIN);
    cellHeadStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    cellHeadStyle.setAlignment(HorizontalAlignment.CENTER);
    cellHeadStyle.setWrapText(true);

    int cols = 12;
    for (int i = 0; i < cols; i++) {
        sh.setColumnWidth(i, 4000);
        Cell headCell1 = headRow.createCell(cell++);
        headCell1.setCellValue(
                vision.getTitle() + "\nscore: " + BscReportSupportUtils.parse2(vision.getScore()));
        headCell1.setCellStyle(cellHeadStyle);
    }

    sh.addMergedRegion(new CellRangeAddress(row, row, 0, cols - 1));

    // ------------------------------------------------------------------------
    bgColor = new XSSFColor(SimpleUtils.getColorRGB4POIColor(BscReportPropertyUtils.getBackgroundColor()));
    fnColor = new XSSFColor(SimpleUtils.getColorRGB4POIColor(BscReportPropertyUtils.getFontColor()));

    cellHeadStyle = wb.createCellStyle();
    cellHeadStyle.setFillForegroundColor(bgColor);
    cellHeadStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

    cellHeadFont = wb.createFont();
    cellHeadFont.setBold(true);
    cellHeadFont.setColor(fnColor);
    cellHeadStyle.setFont(cellHeadFont);
    cellHeadStyle.setBorderBottom(BorderStyle.THIN);
    cellHeadStyle.setBorderTop(BorderStyle.THIN);
    cellHeadStyle.setBorderRight(BorderStyle.THIN);
    cellHeadStyle.setBorderLeft(BorderStyle.THIN);
    cellHeadStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    cellHeadStyle.setAlignment(HorizontalAlignment.CENTER);
    cellHeadStyle.setWrapText(true);

    row++;
    headRow = sh.createRow(row);
    cell = 0;
    int titleCols = 4;
    for (int i = 0; i < titleCols; i++) {
        Cell headCell1 = headRow.createCell(cell++);
        headCell1.setCellValue(BscReportPropertyUtils.getPerspectiveTitle());
        headCell1.setCellStyle(cellHeadStyle);
    }
    for (int i = 0; i < titleCols; i++) {
        Cell headCell1 = headRow.createCell(cell++);
        headCell1.setCellValue(BscReportPropertyUtils.getObjectiveTitle());
        headCell1.setCellStyle(cellHeadStyle);
    }
    for (int i = 0; i < titleCols; i++) {
        Cell headCell1 = headRow.createCell(cell++);
        headCell1.setCellValue(BscReportPropertyUtils.getKpiTitle());
        headCell1.setCellStyle(cellHeadStyle);
    }

    sh.addMergedRegion(new CellRangeAddress(row, row, 0, 3));
    sh.addMergedRegion(new CellRangeAddress(row, row, 4, 7));
    sh.addMergedRegion(new CellRangeAddress(row, row, 8, 11));

    // ------------------------------------------------------------------------

    return 2;
}