How can I change the background color of an Excel sheet cell with C#?
Asked
Active
Viewed 1,626 times
0

tonix
- 6,671
- 13
- 75
- 136

WernerBeinhart
- 28
- 4
-
1dup? https://stackoverflow.com/questions/2452417/cell-color-changing-in-excel-using-c-sharp – Mikev Mar 12 '19 at 16:26
-
Well, reference `System.Drawing` into your project?!? https://i.stack.imgur.com/fNRPb.png – Rand Random Mar 12 '19 at 16:28
1 Answers
3
I would do it that way:
xlWorksheet.Cells[1, 1].Interior.Color = Excel.XlRgbColor.rgbRed;
But don't forget to declare the right using:
using Excel = Microsoft.Office.Interop.Excel;
And I declared the following variables
Excel.Application xlApplication;
Excel.Workbook xlWoorkbook;
Excel.Worksheet xlWorksheet;
xlApplication = new Excel.Application();
xlWoorkbook = xlApplication.Workbooks.Add();
xlWorksheet = xlWoorkbook.Worksheets[1];
Hopefully that works for you too!