Change the background of Cells with C# [duplicate]
Asked Answered
M

4

15

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.

Morrison answered 19/5, 2011 at 13:22 Comment(0)
E
39

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);
Eteocles answered 19/5, 2011 at 13:24 Comment(2)
cannot use Interior. There is an error like the excel object doesn't have definition for that keyword.Wadding
Excel interop 14.0 allows you to set colors without ColorTranslator, using Excel.XlRgbColor.rgbSilverOsteitis
W
3

Yes you can color a cell or a entire column or entire row.

The below code will help you out.

xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 2], xlWorkSheet.Cells[2, 4]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);

else

xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 3], xlWorkSheet.Cells[2, 3]).Interior.Color = Excel.XlRgbColor.rgbRed;

Here xlWorksheet is the object excel Worksheet object.

get_Range takes 2 variable one start cell and other is end cell.

so if you specify both the values same then only one cell is colored.

xlWorkSheet.cells[row, column] is used to specify a cell.

System.Drawing.ColorTranslator.ToOle(SystemDrawing.Color.Green) is used to define the color in OLE format.

Excel.XlRgbColor.rgbRed is a excel way of coloring the cells This method gives access to large number of colors which can be found here list of colors

Wadleigh answered 20/3, 2017 at 10:46 Comment(0)
L
1

You can do it like this:

private static readonly int DEL_PERF_FIRST_DATA_ROW = 10;
private static readonly int SUN_ORDERS_COLUMN = 3;
private static readonly int TUE_ORDERS_COLUMN = 5;
private static readonly int THU_ORDERS_COLUMN = 7;
private static readonly int SAT_ORDERS_COLUMN = 9;
private static Color ALTERNATE_WEEKDAY_COLUMNS_COLOR = Color.LightGray;
. . .
int curRow = DEL_PERF_FIRST_DATA_ROW;

(curRow gets incremented each time a row is written to the sheet.)

// Pale Violetize (light gray, actually) Sun, Tues, Thurs, and Saturday columns
var sundayColumnRange = _xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, SUN_ORDERS_COLUMN],_xlSheetDelPerf.Cells[curRow - 1, SUN_ORDERS_COLUMN]];
sundayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;

var tuesdayColumnRange = _xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, TUE_ORDERS_COLUMN],_xlSheetDelPerf.Cells[curRow - 1, TUE_ORDERS_COLUMN]];
tuesdayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;

var thursdayColumnRange = _xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, THU_ORDERS_COLUMN],_xlSheetDelPerf.Cells[curRow - 1, THU_ORDERS_COLUMN]];
thursdayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;

var saturdayColumnRange =_xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, SAT_ORDERS_COLUMN], _xlSheetDelPerf.Cells[curRow - 1, SAT_ORDERS_COLUMN]];
saturdayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;
Lining answered 1/12, 2015 at 22:59 Comment(0)
P
1

For anyone who want to set Hex color to the background can use:

worksheet.get_Range("A1", "F1").Interior.Color = ColorTranslator.FromHtml("#52b69a");
Phlebotomy answered 5/8, 2021 at 6:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.