Search code examples
c#.netexcelcellworksheet

Change the background of Cells with C#


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.


Solution

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