Change the background of Cells with C#

Wassim AZIRAR picture Wassim AZIRAR · May 19, 2011 · Viewed 36.3k times · Source

I'm developing an program using C# to manipulate an Excel document, and I'm using

    Microsoft.Office.Interop.Excel._Worksheet worksheet;

When I insert something to a x,y cell I use :

worksheet.Cells[x, y] = "something";

Now I want to know if it's possible to change the backColor of the Cells[x,y] from C#.

Thank you.

Answer

Lloyd Powell picture Lloyd Powell · May 19, 2011

Try

worksheet.Cells[x, y].Interior.Color

You won't be able to use the Colors in .Net directly, they will require a translation.

It is recommended to use the following (obviously change from silver) :

worksheet.Cells[x, y].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Silver);