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

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

Posted by: admin March 9, 2020 Leave a comment


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

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

How to&Answers:

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


‘#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) 🙂


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


Here is how i do it with Function, simply replacing blue and red byte.
Example with usage in vb.net:

Imports Microsoft.Office.Interop

Public Class Form1

    Dim Ex_Ap As Excel.Application
    Dim Ex_Wb As Excel.Workbook
    Dim Ex_Ws As Excel.Worksheet

    Function Excel_Color_Get(ByVal i As Int32) As Color
        Static c As Color
        c = Color.FromArgb(i)
        Return Color.FromArgb(c.B, c.G, c.R)
    End Function

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Ex_Ap = New Excel.Application
        Ex_Ap.Visible = True
        Ex_Wb = Ex_Ap.Workbooks.Add
        Ex_Ws = Ex_Wb.Worksheets(1)
        Ex_Ws.Cells(7, 6).Value = "???"
        Ex_Ws.Cells(7, 6).interior.color = Color.Pink
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
            TextBox1.BackColor = Excel_Color_Get(Ex_Ws.Cells(7, 6).interior.color)
        Catch ex As Exception
            TextBox1.Text = ex.ToString
        End Try
    End Sub

End Class