Custom color for ICellStyle FillForegroundColor than the provided named colors

We recently started using NPOI components.

We are having problems setting the FillForegroundColor property of the ICellStyle property.

ICellStyle HeaderCellStyle = xssfworkbook.CreateCellStyle(); HeaderCellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.RED.index; 

FillForegroundColor expects a short type.

How to set a different color, rather than using colors in HSSFColor.

We need to set "RGB192:0:0" and how to do it for the ICellStyle FillForegroundColor property

Can someone help us in some example?

+7
c # npoi
source share
5 answers

I found the solution myself. See below code

 byte[] rgb = new byte[3] { 192, 0, 0 }; XSSFCellStyle HeaderCellStyle1 = (XSSFCellStyle)xssfworkbook.CreateCellStyle(); HeaderCellStyle1.SetFillForegroundColor(new XSSFColor(rgb)); 
+9
source share

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; } 
+2
source share

For people coming here in 2016, here you can find many useful colors:

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

As a result, BlueGrey is the color you are looking for.

+1
source share

Here is the equivalent VB answer, I had to convert this, so maybe it will save someone if it finds 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") 
+1
source share

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 am Japanese, I can not English. sorry.

0
source share

All Articles