How to color a cell using ARGB?

Hi there,

I want to color a cell, but it is weird, some works some not:

/* does not work*/
cell = ws_risk.Cells[“J1”];
cell.PutValue(“Priority”);
Style style32 = cell.GetStyle();
Color color = Color.FromArgb(255, 204, 153);
style32.BackgroundColor = color; // Color.Yellow;
style32.Pattern = BackgroundType.Solid;
cell.SetStyle(style32);

/does not work/
cell = ws_risk.Cells[“K1”];
cell.PutValue(“Risk Owner”);
Style style2 = cell.GetStyle();

style2.BackgroundColor = Color.Yellow;
style2.Pattern = BackgroundType.Solid;
cell.SetStyle(style2);

/this works/
cell = ws_risk.Cells[“L1”];
cell.PutValue(“komank2”);
Style style = cell.GetStyle();
style.BackgroundColor = Color.Yellow;
style.Pattern = BackgroundType.VerticalStripe;
cell.SetStyle(style);

regards

@ibox
When setting the pattern to solid, you need to set the foreground color. When setting the pattern to other types, you can also use the SetPatternColor method to set both the foreground and background colors simultaneously. Please check the following sample code.

Workbook wb = new Workbook();
Worksheet ws_risk = wb.Worksheets[0];
Cell cell = null;
cell = ws_risk.Cells["J1"];
cell.PutValue("Priority");
Style style32 = cell.GetStyle();
style32.Pattern = BackgroundType.Solid;
Color color = Color.FromArgb(255, 204, 153);
style32.ForegroundColor = color; // Color.Yellow;
            
cell.SetStyle(style32);
                        
cell = ws_risk.Cells["K1"];
cell.PutValue("Risk Owner");
Style style2 = cell.GetStyle();
style2.Pattern = BackgroundType.Solid;
style2.ForegroundColor = Color.Yellow;
            
cell.SetStyle(style2);

            
cell = ws_risk.Cells["L1"];
cell.PutValue("komank2");
Style style = cell.GetStyle();
style.Pattern = BackgroundType.VerticalStripe;
style.BackgroundColor = Color.Yellow;            
cell.SetStyle(style);

cell = ws_risk.Cells["J2"];
cell.PutValue("test");
Style j2style = cell.GetStyle();
j2style.SetPatternColor(BackgroundType.VerticalStripe, Color.Red, Color.Green);
cell.SetStyle(j2style);

cell = ws_risk.Cells["J3"];
cell.PutValue("test data");
Style j3style = cell.GetStyle();
j3style.Pattern = BackgroundType.VerticalStripe;
j3style.BackgroundColor = Color.Yellow;
j3style.ForegroundColor = Color.Green;            
cell.SetStyle(j3style);

wb.Save(filePath + "out_net.xlsx");

Regarding how to set the pattern, background color, and foreground color, please refer to the following document.

Hope helps a bit.

thank u Sir,
It works.

regards

@ibox
You are welcome. If you have any questions or confusion, please feel free to contact us at any time.