tags:

views:

1801

answers:

2

I have an excel sheet with some cells having some background color. I need this color in html code and hence I want to convert Excel.Range.Interior.Color to RGB format or System.Drawing.Color.

After doing that i would be using System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color) to get color to be used in html tags.

I tried doing following:

Excel.Range r = (Excel.Range)m_objRange[2, 2];
System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(r.Interior.Color);
               MessageBox.Show(""+converter.ConvertTo(r.Interior.Color,typeof(System.Drawing.Color)));

But i get an error that i cannot convert System.Double to System.Drawing.Color

+1  A: 

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) :)

Jon
It worked. Thanks :)
No problem, you should accept my answer ;)
Jon
+2  A: 

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

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