Cell Color Changing in Excel Using C#

Cell color changing in Excel using C#

For text:

[RangeObject].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);

For cell background

[RangeObject].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);

How to change the background color of an Excel sheet cell in C#

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!

excel cell coloring

Try something like that

ws.Cells[row, clmn].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)

How can I change color of text in a cell of MS Excel?

Try:

workSheet.Cells[1, (i + 1)].Characters[start_pos, len].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);

where start_pos and len is the part of the string where to apply color.

Your use case example:

    Application excelApp = new Application();
excelApp.Workbooks.Add();
// single worksheet
_Worksheet workSheet = excelApp.ActiveSheet;

string Green = "Green";
string Red = "Red";
for (int start = 0; start < 10; start++)
{
Range ColorMeMine = workSheet.Cells[1, (start + 1)];
ColorMeMine.Value = string.Format("{0} {1}", Green, Red);
ColorMeMine.Characters[0, Green.Length].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
ColorMeMine.Characters[Green.Length + 1, Green.Length + 1 + Red.Length].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}

Change the background of Cells with C#

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

Change Excel Range Background Color

I solved already the Problem. I used the System.Drawing Library but in

Section1.Interior.Color = Colors.LightBlue;

"Colors" there was used another Library that shouldn´t. So i just addes the correct Library before Colors.



Related Topics



Leave a reply



Submit