Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Excel Cell Colours using RGB Values

I'm trying to set Excel cells using RGB values rather than the way I do it currently as i need to set cells to a specific colour rather than a set standard colour.

This is how i currently do it:

ChartRange.Interior.Color = Microsoft.Office.Interop.Excel.XlRgbColor.rgbRed;

Is it possible to set colour in RGB values in C#?

I am working with Excel sheets that have had the colours set by RGB values in VBA.

like image 343
SBozhko Avatar asked Jul 26 '17 08:07

SBozhko


People also ask

How do I get Excel to change color based on value?

On the Home tab, in the Styles group, click the arrow next to Conditional Formatting, and then click Color Scales. Select a two-color scale. Hover over the color scale icons to see which icon is a two-color scale. The top color represents higher values, and the bottom color represents lower values.

How do I color a cell in Excel based on a number?

Go to Home >> Styles >> Conditional Formatting >> Highlight Cells Rules >> Equal To. In Equal To dialog box put the number and assign the color that you want to it, then click OK. Do the same steps for each number.


2 Answers

You can able to assign the System.Drawing.Color using FromArgb as below:

ChartRange.Interior.Color = System.Drawing.Color.FromArgb(255, 0, 0);
like image 67
Balagurunathan Marimuthu Avatar answered Sep 30 '22 06:09

Balagurunathan Marimuthu


You can convert from a System.Drawing.Color using System.Drawing.ColorTranslator which will let you set using RGB values.

Something like this:

System.Drawing.Color color = System.Drawing.Color.FromArgb(255, 0, 0);
ChartRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(color);
like image 29
musefan Avatar answered Sep 30 '22 05:09

musefan