Example usage for org.apache.poi.hssf.usermodel HSSFPalette setColorAtIndex

List of usage examples for org.apache.poi.hssf.usermodel HSSFPalette setColorAtIndex

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFPalette setColorAtIndex.

Prototype

public void setColorAtIndex(short index, byte red, byte green, byte blue) 

Source Link

Document

Sets the color at the given offset

Usage

From source file:br.com.tecsinapse.dataio.util.WorkbookUtil.java

License:LGPL

private void replaceColorsPallete(Map<HSSFColor, HSSFColor> colorsReplaceMap, Workbook wb) {
    if (!(wb instanceof HSSFWorkbook)) {
        return;//from  www. j a  va 2s.  c  o  m
    }

    HSSFWorkbook hssfWb = (HSSFWorkbook) wb;
    final HSSFPalette customPalette = hssfWb.getCustomPalette();
    for (Entry<HSSFColor, HSSFColor> e : colorsReplaceMap.entrySet()) {
        short[] rgb = e.getValue().getTriplet();
        customPalette.setColorAtIndex(e.getKey().getIndex(), (byte) rgb[0], (byte) rgb[1], (byte) rgb[2]);
    }
}

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

private static Map<String, CellStyle> createStyles(Workbook workbook) {
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
    CellStyle style;//w  ww . j a v a  2 s. c  o m
    Font titleFont = workbook.createFont();
    titleFont.setFontHeightInPoints((short) 26);
    titleFont.setFontName("Calibri");
    titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = workbook.createCellStyle();
    style.setFont(titleFont);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    styles.put("title", style);

    titleFont = workbook.createFont();
    titleFont.setFontHeightInPoints((short) 18);
    titleFont.setFontName("Calibri");
    style = workbook.createCellStyle();
    style.setFont(titleFont);
    style.setAlignment(CellStyle.ALIGN_JUSTIFY);

    styles.put("pulsioName", style);

    titleFont = workbook.createFont();
    titleFont.setFontHeightInPoints((short) 14);
    titleFont.setFontName("Calibri");
    style = workbook.createCellStyle();
    style.setFont(titleFont);
    style.setAlignment(CellStyle.ALIGN_JUSTIFY);

    styles.put("contacts", style);

    CellStyle footerStyle = workbook.createCellStyle();
    Font footerFont = workbook.createFont();
    footerFont.setFontHeightInPoints((short) 14);
    footerFont.setFontName("Calibri");
    footerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    footerStyle.setFont(footerFont);
    footerStyle.setAlignment(CellStyle.ALIGN_JUSTIFY);

    styles.put("footer", footerStyle);

    titleFont = workbook.createFont();
    titleFont.setFontHeightInPoints((short) 14);
    titleFont.setFontName("Calibri");
    titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    titleFont.setItalic(true);
    style = workbook.createCellStyle();
    style.setFont(titleFont);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setWrapText(true);

    // Override 25% grey to lighter grey
    HSSFWorkbook hssfWorkbook = (HSSFWorkbook) workbook;
    HSSFPalette palette = hssfWorkbook.getCustomPalette();
    palette.setColorAtIndex(HSSFColor.GREY_25_PERCENT.index, (byte) 242, //RGB red (0-255)
            (byte) 242, //RGB green
            (byte) 242 //RGB blue
    );

    style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setBorderLeft(CellStyle.BORDER_MEDIUM);
    style.setBorderTop(CellStyle.BORDER_MEDIUM);
    style.setBorderRight(CellStyle.BORDER_THIN);
    style.setBorderBottom(CellStyle.BORDER_THIN);

    styles.put("tableHeadersLeft", style);

    CellStyle headerRowMiddleCellStyle = workbook.createCellStyle();
    headerRowMiddleCellStyle.cloneStyleFrom(style);
    headerRowMiddleCellStyle.setBorderLeft(CellStyle.BORDER_THIN);

    styles.put("tableHeadersMiddle", headerRowMiddleCellStyle);

    CellStyle headerRowRightCellStyle = workbook.createCellStyle();
    headerRowRightCellStyle.cloneStyleFrom(style);
    headerRowRightCellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);

    styles.put("tableHeadersRight", headerRowRightCellStyle);

    CellStyle footerRowRightCellStyle = workbook.createCellStyle();
    footerRowRightCellStyle.cloneStyleFrom(style);
    footerRowRightCellStyle.setFillPattern(CellStyle.NO_FILL);
    footerRowRightCellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
    footerRowRightCellStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);

    styles.put("tableFooters", footerRowRightCellStyle);

    CellStyle bodyRowLeftCellStyle = workbook.createCellStyle();
    bodyRowLeftCellStyle.cloneStyleFrom(style);
    Font titleBodyFont = workbook.createFont();
    titleBodyFont.setFontHeightInPoints((short) 14);
    titleBodyFont.setFontName("Calibri");
    bodyRowLeftCellStyle.setFont(titleBodyFont);
    bodyRowLeftCellStyle.setBorderTop(CellStyle.BORDER_THIN);
    bodyRowLeftCellStyle.setFillPattern(CellStyle.NO_FILL);

    styles.put("tableBodyLeft", bodyRowLeftCellStyle);

    CellStyle bodyRowMiddleCellStyle = workbook.createCellStyle();
    bodyRowMiddleCellStyle.cloneStyleFrom(bodyRowLeftCellStyle);
    bodyRowMiddleCellStyle.setBorderLeft(CellStyle.BORDER_THIN);

    styles.put("tableBodyMiddle", bodyRowMiddleCellStyle);

    CellStyle bodyRowRightCellStyle = workbook.createCellStyle();
    bodyRowRightCellStyle.cloneStyleFrom(bodyRowMiddleCellStyle);
    bodyRowRightCellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);

    styles.put("tableBodyRight", bodyRowRightCellStyle);

    return styles;
}

From source file:com.ibm.ioes.bulkupload.utilities.ErrorLogServiceImpl.java

public HSSFColor setColor(HSSFWorkbook workbook, byte r, byte g, byte b) {
    HSSFPalette palette = workbook.getCustomPalette();
    HSSFColor hssfColor = null;//from   ww  w.ja va  2  s . c  o  m
    try {
        hssfColor = palette.findColor(r, g, b);
        if (hssfColor == null) {
            palette.setColorAtIndex(HSSFColor.LAVENDER.index, r, g, b);
            hssfColor = palette.getColor(HSSFColor.LAVENDER.index);
        }
    } catch (Exception e) {
        logger.error(e);
        Utility.LOG(true, false, e, "::BULKUPLOAD_ERROR:: Exception occured in setColor method of "
                + this.getClass().getSimpleName());
    }

    return hssfColor;
}

From source file:com.sevenorcas.openstyle.app.service.spreadsheet.SpreadSheet.java

/**
 * Set a color by substituting an used color
 * //from   www  .j a va2  s  . c  om
 * Thanks to http://stackoverflow.com/questions/10528516/poi-setting-background-color-to-a-cell
 * Thanks to http://stackoverflow.com/questions/842817/how-does-java-convert-int-into-byte
 * 
 * @param workbook
 * @param r
 * @param g
 * @param b
 * @return
 */
public HSSFColor setColor(HSSFWorkbook workbook, int r, int g, int b) {
    HSSFPalette palette = workbook.getCustomPalette();
    HSSFColor hssfColor = null;
    try {
        byte rb = (byte) (r);
        byte gb = (byte) (g);
        byte bb = (byte) (b);

        hssfColor = palette.findColor(rb, gb, bb);
        if (hssfColor == null) {
            short s = colorCustomers.getColorIndex(colorCustomers.getNextIndex());
            palette.setColorAtIndex(s, rb, gb, bb);
            hssfColor = palette.getColor(s);
        }
    } catch (Exception e) {
    }

    return hssfColor;
}

From source file:com.yukthi.excel.exporter.InternalWorkbook.java

License:Open Source License

/**
 * Gets the index of specified color in the current workbook
 * //from w w w .  jav a 2s .  c o m
 * @param color
 * @return
 */
public HSSFColor getColor(Color color) {
    Integer index = colorToIndex.get(color);
    HSSFPalette palette = workbook.getCustomPalette();

    // if color was already added get color based on found index
    if (index != null) {
        return palette.getColor(index);
    }

    //add color to the workbook and return the same
    HSSFColor hssfColor = null;

    try {
        palette.setColorAtIndex(nextColorIndex, (byte) color.getRed(), (byte) color.getGreen(),
                (byte) color.getBlue());
        hssfColor = palette.getColor(nextColorIndex);

        nextColorIndex++;
    } catch (Exception ex) {
        throw new IllegalStateException("An error occurred while adding color to workbook", ex);
    }

    return hssfColor;
}

From source file:das.pf.io.IOExcel.java

License:Open Source License

private HSSFCellStyle getHSSFCellStyleValue(Workbook worbook, TypeValues typeValue, TypeUnits typeUnit) {
    HSSFCellStyle style = (HSSFCellStyle) worbook.createCellStyle();
    HSSFFont font = (HSSFFont) worbook.createFont();
    HSSFPalette palette = ((HSSFWorkbook) worbook).getCustomPalette();

    font.setColor(HSSFColor.WHITE.index);

    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    switch (typeValue) {
    case VALUES:// ww  w.j  ava 2s  . c om
        switch (typeUnit) {
        case MTH:
            style.setFillForegroundColor(HSSFColor.DARK_BLUE.index);

            break;

        case QRT:
            style.setFillForegroundColor(HSSFColor.DARK_BLUE.index);
            palette.setColorAtIndex(HSSFColor.DARK_BLUE.index, (byte) 0, (byte) 0, (byte) 102);

            break;

        case YTD:
            style.setFillForegroundColor(HSSFColor.BLUE.index);

            break;

        case MAT:
            style.setFillForegroundColor(HSSFColor.AQUA.index);

            break;
        }

        break;

    case UNITS:
        switch (typeUnit) {
        case MTH:
            style.setFillForegroundColor(HSSFColor.RED.index);

            break;

        case QRT:
            style.setFillForegroundColor(HSSFColor.DARK_RED.index);

            break;

        case YTD:
            style.setFillForegroundColor(HSSFColor.ORANGE.index);

            break;

        case MAT:
            style.setFillForegroundColor(HSSFColor.YELLOW.index);

            break;
        }

        break;

    case U_E:
        switch (typeUnit) {
        case MTH:
            style.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);

            break;

        case QRT:
            style.setFillForegroundColor(HSSFColor.DARK_GREEN.index);

            break;

        case YTD:
            style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);

            break;

        case MAT:
            style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);

            break;
        }

        break;
    }

    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    style.setFont(font);

    return style;
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.elasticExcel.export.template.ExcelStylesCreator.java

License:Open Source License

private static void setCustomVioletForegroundColor(Workbook wb, CellStyle style) {
    if (wb instanceof HSSFWorkbook) {
        HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette();
        palette.setColorAtIndex(HSSFColor.VIOLET.index, (byte) 169, (byte) 33, (byte) 142);
        style.setFillForegroundColor(IndexedColors.VIOLET.getIndex());
    } else {/*from  w ww .ja  v a 2  s  .com*/
        XSSFColor xssfColor = new XSSFColor(new java.awt.Color(169, 33, 142));
        ((XSSFCellStyle) style).setFillForegroundColor(xssfColor);
    }
}

From source file:edu.vt.vbi.patric.common.ExcelHelper.java

License:Apache License

/**
 * This method creates a map of Cellstyle objects for page building. Note: this method used for HSSF pages
 * /*from w  ww  .  ja  va2 s  .  c om*/
 * @return hashmap of styles
 */
private Map<String, CellStyle> createStyles() {

    // create custom colors
    HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette();

    // This replaces various shades of grey with custom colors
    palette.setColorAtIndex(HSSFColor.GREY_25_PERCENT.index, (byte) 0, // RGB red (0-255)
            (byte) 52, // RGB green
            (byte) 94 // RGB blue
    );
    palette.setColorAtIndex(HSSFColor.GREY_40_PERCENT.index, (byte) 230, (byte) 240, (byte) 248);
    palette.setColorAtIndex(HSSFColor.GREY_50_PERCENT.index, (byte) 255, (byte) 193, (byte) 193);

    // Create header style

    CellStyle style = createBorderStyle();

    Font headerFont = wb.createFont();
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    headerFont.setColor(IndexedColors.WHITE.getIndex());

    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setWrapText(true);
    style.setFont(headerFont);
    styles.put("header", style);

    // Create alternating-color body styles

    Font bodyFont = wb.createFont();
    bodyFont.setFontHeightInPoints((short) 8);

    style = createBorderStyle();
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFont(bodyFont);
    style.setWrapText(true);
    styles.put("bg1", style);

    style = createBorderStyle();
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setWrapText(true);
    style.setFont(bodyFont);
    styles.put("bg2", style);

    // create style for empty cell
    style = createBorderStyle();
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFont(bodyFont);
    style.setWrapText(true);
    styles.put("empty", style);

    return styles;
}

From source file:es.jamisoft.comun.io.excel.ExcelGenerator.java

License:Apache License

/**
 * Este mtodo gestiona el estilo que se proporcionar a las cabeceras.
 *
 * @param wb Objeto Excel./*from w  ww.jav  a 2 s . c o m*/
 */
private void initStylesHeader(HSSFWorkbook wb) {

    // create instance of HSSFCellStyle
    HSSFCellStyle headerStyle = wb.createCellStyle();

    // Create Font Header
    createFontCell(wb, headerStyle, ep.getFillFontColorHeader());
    headerStyle.getFont(wb).setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

    // Create style of header cell
    HSSFPalette palette = wb.getCustomPalette();
    HSSFColor colorMapfreHSSF = palette.findColor((byte) ep.getColorMapfre().getRed(),
            (byte) ep.getColorMapfre().getGreen(), (byte) ep.getColorMapfre().getBlue());

    if (colorMapfreHSSF == null) {
        palette.setColorAtIndex(HSSFColor.LAVENDER.index, (byte) ep.getColorMapfre().getRed(),
                (byte) ep.getColorMapfre().getGreen(), (byte) ep.getColorMapfre().getBlue());
        colorMapfreHSSF = palette.getColor(HSSFColor.LAVENDER.index);
    }

    short fillBGColor = colorMapfreHSSF.getIndex();

    headerStyle.setFillForegroundColor(fillBGColor);
    headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    // create border of header cell
    createBorderCells(headerStyle);

    // set
    ep.setHeaderStyle(headerStyle);
}

From source file:fr.amapj.service.engine.generator.excel.ExcelGeneratorTool.java

License:Open Source License

private void beGray(CellStyle style) {
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);

    if (wb instanceof HSSFWorkbook) {
        HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette();
        palette.setColorAtIndex(HSSFColor.LAVENDER.index, (byte) 0xF0, (byte) 0xF0, (byte) 0xF0);
        style.setFillForegroundColor(IndexedColors.LAVENDER.getIndex());
    } else {//from w w  w .j av  a  2 s .c o m
        ((XSSFCellStyle) style).setFillForegroundColor(new XSSFColor(new java.awt.Color(0xF0, 0xF0, 0xF0)));
    }
}