How to convert Excel.Range.Interior.Color to System.Drawing.Color in C#?

It's much easier to let ColorTranslator do the work for you:

System.Drawing.Color col = System.Drawing.ColorTranslator.FromOle((int) r.Interior.Color);

And when you are writing an Excel project inside Visual Studio with the automatically generated proxy objects (instead of the regular Interop project) you need to cast r.Interior.Color to a double, and then back to an int:

System.Drawing.Color col = System.Drawing.ColorTranslator.FromOle((int)((double) r.Interior.Color));

The value returned by Excel.Range.Interior.Color is a long integer value of a color.

Examples:

'#000000 is equal to 0

'#FFFFFF is equal to 16777215

You need to convert the decimal value into Hexadecimal. From there, it is easy to convert to RGB. (Group into 2 octets, and convert back to decimal) :)

Tags:

C#

Excel