Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 742
Wassim AZIRAR Avatar asked May 19 '11 13:05

Wassim AZIRAR


People also ask

How do I change the background of my cells?

Select the cell or range of cells you want to format. Click Home > Format Cells dialog launcher, or press Ctrl+Shift+F. On the Fill tab, under Background Color, pick the color you want.

Can you use an IF statement to color a cell?

Apply an If-Then rule to all cells by pressing “Ctrl-A” before assigning your rule. If you copy values from colored cells and paste them into new it new cells, the new cells acquire the color of the copied cell.


1 Answers

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);
like image 81
Lloyd Powell Avatar answered Sep 27 '22 19:09

Lloyd Powell