简体   繁体   中英

How to Change Excel Cell Color Dynamically using C#

Using the below link, I am able to loop through the rows and able to get exact cell, but unable to set its color. Please see the code below.

How do I dynamically set the forecolour of my column data based on the value when exporting excel using EPPLUS?

var costStructureReport = new
            {
                CurrentQuotation = rptCostStructure.GetCostStructureReport()
            };

 var reportEngine = new ReportEngine();
            string fileName = reportEngine.ProcessReport(ReportNames.ProjectDownload_Template, reportname + ".xlsx", costStructureReport);

            var ep = new ExcelPackage(new FileInfo(fileName));
            var sheet1 = ep.Workbook.Worksheets["SPR_ProjectDownload"];
            var row = sheet1.Dimension.End.Row;
            
            for(int i=0;i< costStructureReport.CurrentQuotation.Count;i++)
            {
                if (costStructureReport.CurrentQuotation[i].MaterialCost_ByUser)
                {
                    sheet1.Cells[i+2, 2].Style.Font.Color.SetColor(System.Drawing.Color.Red);
                    sheet1.Cells[i + 2, 12].Style.Font.Color.SetColor(System.Drawing.Color.Red);
                    sheet1.Cells[i + 2, 12].Style.Font.Bold = true;
                }

            }

You can Change the Text Color using

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

And Font Colour using

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM