Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom color for ICellStyle FillForegroundColor than provided named colors

Tags:

c#

c#-4.0

npoi

We are newly started using NPOI components.

We are having issues to set FillForegroundColor of ICellStyle property.

ICellStyle HeaderCellStyle = xssfworkbook.CreateCellStyle(); 

HeaderCellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.RED.index;

FillForegroundColor expects of type short.

How do we set a different color rather than using colors in HSSFColor.

We need to set to "RGB192:0:0" and how do we do that for ICellStyle property FillForegroundColor

Colud someone help us by some example?

like image 208
SivaRajini Avatar asked Mar 27 '14 12:03

SivaRajini


2 Answers

I found the solution my self. Please refer below code

byte[] rgb = new byte[3] { 192, 0, 0 };
 XSSFCellStyle HeaderCellStyle1 = (XSSFCellStyle)xssfworkbook.CreateCellStyle();
 HeaderCellStyle1.SetFillForegroundColor(new XSSFColor(rgb));
like image 182
SivaRajini Avatar answered Sep 16 '22 21:09

SivaRajini


Sorry, and again.

Color SuperColor = Color.FromArgb(192, 0, 0);
styleH.FillForegroundColor = GetXLColour(hssfworkbook, SuperColor);


private short GetXLColour(HSSFWorkbook workbook, System.Drawing.Color SystemColour)
{
    short s = 0;
    HSSFPalette XlPalette = workbook.GetCustomPalette();
    NPOI.HSSF.Util.HSSFColor XlColour = XlPalette.FindColor(SystemColour.R, SystemColour.G, SystemColour.B);
    if (XlColour == null)
    {
        if (NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE < 255)
        {
            if (NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE < 64)
            {
                NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE = 64;
                NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE += 1;
                XlColour = XlPalette.AddColor(SystemColour.R, SystemColour.G, SystemColour.B);
            }
            else
            {
                XlColour = XlPalette.FindSimilarColor(SystemColour.R, SystemColour.G, SystemColour.B);
            }

            s = XlColour.GetIndex();
        }

    }
    else
        s = XlColour.GetIndex();

    return s;
}  
like image 29
村井誠 Avatar answered Sep 17 '22 21:09

村井誠