Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

60%
+1 −0
Q&A How to set the cell color to a user defined value using NPOI from F#?

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.UserM...

1 answer  ·  posted 4mo ago by toraritte‭  ·  last activity 4mo ago by toraritte‭

Question f# excel NPOI
#1: Initial revision by user avatar toraritte‭ · 2024-01-11T16:04:10Z (4 months ago)
How to set the cell color to a user defined value using NPOI from F#?
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](https://www.baeldung.com/apache-poi-background-color), [2](https://stackoverflow.com/a/58475635/1498178), [3](https://stackoverflow.com/questions/37052352/how-to-change-cell-color-with-npoi), ).

Am I missing something obvious or this is indeed not possible?

<sup>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](https://www.baeldung.com/apache-poi-background-color) and [here](https://stackoverflow.com/a/58475635/1498178), for example).</sup>

---

**edit**: Found two other solutions for C# ([this](https://stackoverflow.com/questions/55514395/is-there-any-way-to-add-color-to-cell-from-hex-value-like-72fe9c-in-my-excel) and [this](https://stackoverflow.com/questions/65245153/npoi-set-cell-color-to-a-custom-color)), 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
```