Custom color for ICellStyle FillForegroundColor than provided named colors

旧城冷巷雨未停 提交于 2021-01-26 12:38:50

问题


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?


回答1:


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));



回答2:


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;
}  



回答3:


change font color

example..

ICellStyle style0 = hssfworkbook.CreateCellStyle();

IFont font0 = hssfworkbook.CreateFont();
font0.Color = NPOI.HSSF.Util.HSSFColor.RED.index;
style0.SetFont(font0);

for (int i = 0; i < cell.Length; i++)
{
    cell[i].CellStyle = style0;
}

--

I'm Japanese, can't English. sorry.




回答4:


For people coming here in 2016, you can find lots of useful colours here:

nameStyle.Color = NPOI.HSSF.Util.HSSFColor.BlueGrey.Index;

Whereby BlueGrey is the colour you are looking for.




回答5:


Here is a VB equivalent answer, I had to convert this, so maybe it will save someone some work if they find it:

'my workbook is from a template that already contains some header data
Dim fs As New FileStream(HttpContext.Current.Server.MapPath("..\_ExcelTemplates\filename.xlsx"), FileMode.Open, FileAccess.Read)            
Dim workbook As IWorkbook
workbook = New XSSFWorkbook(fs) 
Dim sheet_stats As ISheet = workbook.GetSheet("Stats")

'style
Dim rgb_grandTotalRow_fg() As Byte = New Byte() {197, 217, 241} 'lightish blue
Dim style_grandTotalRow As XSSFCellStyle = workbook.CreateCellStyle()
style_grandTotalRow.SetFillForegroundColor(New XSSFColor(rgb_grandTotalRow_fg)) 'XSSFCellStyle only for other use (FillForegroundColor = IndexedColors.LightTurquoise.Index)
style_grandTotalRow.FillPattern = FillPattern.SolidForeground

'apply the style to a cell
Dim rowHdr As IRow = sheet_stats.GetRow(2)
Dim cell As ICell

cell = Row.CreateCell(1) 'create cell at col index 1
cell.CellStyle = style_grandTotalRow
cell.SetCellValue("TEST")


来源:https://stackoverflow.com/questions/22687901/custom-color-for-icellstyle-fillforegroundcolor-than-provided-named-colors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!