Posted by: joachimvandenbogaert | March 30, 2006

Microsoft.Office.Interop.Excel 00

Colors in C# .net and Excel are defined in different ways. The Interop assemblies only accept doubles as in

using Excel = Microsoft.Office.Interop.Excel;

((Excel.Range)sheet.Cells[1, 1]).EntireRow.Interior.Color = (double)255;

This causes serious trouble when you want to define your colors with Alpha and RGB values.
On this site, there is an explanation of how to do things by shifting bits:

But we don’t want to do that, instead you can use the
System.Drawing.ColorTranslator.ToWin32(Color color) method to translate a Color object to a legacy win32 double denoting a color.

So the complete code to change the color of a row through C# interop is:

((Excel.Range)sheet.Cells[i, 1]).EntireRow.Interior.Color =
System.Drawing.Color.FromArgb(255, 255, 153,0));

The color here is FF9900 (aka pumpkin) which happens to be my favourite color.
Of course you can write a wrapper method to speed up things when reusing your code in the future.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: