Posted by: joachimvandenbogaert | April 3, 2006

Microsoft.Office.Interop.Excel 01

Some more on the colors used in Excel and the color names in the .net environment.

  • Excel uses its own color space of 56 colors. Not all .net colors are mapped on the same Excel Color Name!!! I was puzzled by the fact that I had assigned Color.Orange to a particular cell, and when my code was testing it in a conditional statement, it seemed the Colour had changed into Pumpkin, which is defined as “Orange” in the Excel default palette.
  • Although the colours in Office are defined as 32bit integers, you cannot convert them immediately using an (int) cast.b You need a (double) cast first. So when you want to convert a win32 colour to the .net Colour namespace you need the following statements:SomeObject.Interior.Color =
    System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.Orange)

    Color c =
    ColorTranslator.FromWin32((int)(double)SomeObject.Interior.Color)

Conclusion: if you want to use colors safely when Interopping Excel, you need to make sure that you map the .net system colors onto the Excel palette.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: