Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to change the color of a particular cell based on some conditions. I am already exporting whole table from MySQL to Excel, after exporting to Excel I want to change cell color in Excel file. I am using ClosedXML library in C#.NET for exporting to Excel.


Error-
Severity	Code	Description	Project	File	Line	Suppression State
Error	CS1061	'XLColor' does not contain a definition for 'SetColor' and no accessible extension method 'SetColor' accepting a first argument of type 'XLColor' could be found (are you missing a using directive or an assembly reference?)	MailScheduler	


Severity	Code	Description	Project	File	Line	Suppression State
Error	CS0117	'Color' does not contain a definition for 'LightGreen'	MailScheduler	


What I have tried:

private void ExportDataSetToExcel(DataSet ds)
       {
           string[] paths = { Path.GetFullPath(@"..\..\"), "ExcelFiles" };
           string fullPath = Path.Combine(paths);
           string file = fullPath + "\\DataFile.xlsx";

           using (XLWorkbook wb = new XLWorkbook())
           {

               wb.Worksheets.Add(ds.Tables[0]);
               wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
               wb.Style.Font.Bold = true;
               wb.Style.Fill.BackgroundColor.SetColor(Color.LightGreen);
               wb.SaveAs(file);

           }


       }
Posted
Updated 17-Dec-19 20:08pm

1 solution

Based on ClosedXml documentation[^] you have to use one of ClosedXML Predefined Colors[^], instead of standard color.

C#
ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.Red;


Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900