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
What Advantages of Extension Methods Have You Found
Excel to Datatable Using Epplus - Excel Locked for Editing
Select Parsed Int, If String Was Parseable to Int
How to Restrict/Control the Navigation Routes the User Can Visit Based on Login Status/Role
Creating Wcf Channelfactory<T>
Using Case/Switch and Gettype to Determine the Object
Export Datatable to Excel File
Mobile Device Detection in ASP.NET
What Is Point of Ssl If Fiddler 2 Can Decrypt All Calls Over Https
How to Sum Up an Array of Integers in C#
How to Add Claims in ASP.NET Identity
Resizing an Image in ASP.NET Without Losing the Image Quality
How to Run Visual Studio Without Plugin and All Third Party Feature
Using Itextsharp to Extract and Update Links in an Existing PDF
Listbox Items Return String, When Datatemplate Is Button