As far as I can tell (from F# at least), cell can only be colored using predefined NPOI.SS.UserModel.IndexedColors. From the available methods / properties,
val it: ICellStyle =
NPOI.XSSF.UserModel.XSSFCellStyle
{ ...
FillBackgroundColor = -494s;
FillBackgroundColorColor = NPOI.XSSF.UserModel.XSSFColor;
FillBackgroundXSSFColor = NPOI.XSSF.UserModel.XSSFColor;
FillForegroundColor = 64s;
FillForegroundColorColor = null;
FillForegroundXSSFColor = null;
FillPattern = NoFill;
...
only FillBackgroundColor, FillForegroundColor, and FillPattern can be set to an int16 value, which corresponds to the IndexedColors values, and the rest of the patterns are immutable. I can create a custom
new NPOI.XSSF.UserModel.XSSFColor([|255uy; 192uy; 150uy|])
color, but I simply couldn't figure out yet how to assign this either to an existing cell style or to a brand new one. All the articles I could find also only use IndexedColors (e.g., 1, 2, 3, ).
Am I missing something obvious or this is indeed not possible?
The meaning of the terms "foreground" and "background" in this context is also misleading, but that is inherited from Apache POI, and it is well documented online (again, here and here, for example).
edit: Found two other solutions for C# (this and this), that do the obvious by setting FillBackgroundColor to with a custom XSSFColor, but, again, I couldn't figure out how to do this in F#:
> cs;;
val it: ICellStyle =
NPOI.XSSF.UserModel.XSSFCellStyle
{Alignment = General;
BorderBottom = Thin;
BorderDiagonal = None;
BorderDiagonalColor = 8s;
BorderDiagonalLineStyle = None;
BorderLeft = Thin;
BorderRight = Thin;
BorderTop = Thin;
BottomBorderColor = 64s;
BottomBorderXSSFColor = NPOI.XSSF.UserModel.XSSFColor;
DataFormat = 0s;
DiagonalBorderXSSFColor = null;
FillBackgroundColor = -494s;
FillBackgroundColorColor = NPOI.XSSF.UserModel.XSSFColor;
FillBackgroundXSSFColor = NPOI.XSSF.UserModel.XSSFColor;
FillForegroundColor = 0s;
FillForegroundColorColor = NPOI.XSSF.UserModel.XSSFColor;
FillForegroundXSSFColor = NPOI.XSSF.UserModel.XSSFColor;
FillPattern = NoFill;
FontIndex = 3s;
Indention = 0s;
Index = 43s;
IsHidden = false;
IsLocked = false;
LeftBorderColor = 64s;
LeftBorderXSSFColor = NPOI.XSSF.UserModel.XSSFColor;
RightBorderColor = 64s;
RightBorderXSSFColor = NPOI.XSSF.UserModel.XSSFColor;
Rotation = 0s;
ShrinkToFit = false;
TopBorderColor = 64s;
TopBorderXSSFColor = NPOI.XSSF.UserModel.XSSFColor;
VerticalAlignment = Bottom;
WrapText = false;}
> c;;
val it: XSSFColor =
NPOI.XSSF.UserModel.XSSFColor {ARGB = [|255uy; 255uy; 192uy; 150uy|];
ARGBHex = "FFFFC096";
HasAlpha = false;
HasTint = false;
Index = 0s;
Indexed = 0s;
IsAuto = false;
IsIndexed = false;
IsRGB = true;
IsThemed = false;
RGB = [|255uy; 192uy; 150uy|];
RGBWithTint = [|255uy; 192uy; 150uy|];
Theme = 0;
Tint = 0.0;}
> cs.FillBackgroundColor;;
val it: int16 = -494s
> cs.FillBackgroundColor <- c;;
cs.FillBackgroundColor <- c;;
--------------------------^
/Users/toraritte/dev/clones/dotNET/slate-excel-reports/stdin(332,27): error FS0001: This expression was expected to have type
'int16'
but here has type
'XSSFColor'
> cs.FillBackgroundColorColor;;
val it: IColor = NPOI.XSSF.UserModel.XSSFColor {ARGB = null;
ARGBHex = null;
HasAlpha = false;
HasTint = false;
Index = -494s;
Indexed = -494s;
IsAuto = false;
IsIndexed = true;
IsRGB = false;
IsThemed = false;
RGB = null;
RGBWithTint = null;
Theme = 0;
Tint = 0.0;}
> cs.FillBackgroundColorColor <- c;;
cs.FillBackgroundColorColor <- c;;
^^^^^^^^^^^^^^^^^^^^^^^^^^^
/Users/toraritte/dev/clones/dotNET/slate-excel-reports/stdin(334,1): error FS0810: Property 'FillBackgroundColorColor' cannot be set
I failed to notice that a cell's
CellStyleproperty returns the genericICellStyle, which has to be cast down toNPOI.XSSF.UserModel.XSSFCellStylein order to gain access to theFillBackgroundXSSFColorandFillForegroundXSSFColorproperties.Also, here's a summary of which property does what, quoting from this answer:
In my case, I need
NPOI.SS.UserModel.FillPatternandFillForegroundXSSFColorthen. The full code:To call with RGB colors:
To call with hex color string: