0

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

            }
  • Does this answer your question? [Cell color changing in Excel using C#](https://stackoverflow.com/questions/2452417/cell-color-changing-in-excel-using-c-sharp) – user1672994 Mar 30 '21 at 07:31

1 Answers1

0

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

Asad
  • 617
  • 2
  • 8
  • 23
  • Hi, ExcelRange doesn't have property Interior, I have modified my code below, it has color property, but as it is read only I am not able to assign the color to it."Rng.Style.Font.Color" Even the Bold is not applying. Please see the code below `if (costStructureReport.CurrentQuotation[i].MaterialCost_ByUser) { using (ExcelRange Rng = sheet1.Cells[i + 2, 12]) { Rng.Style.Font.Bold = true; Rng.Style.Font.Italic = true; } ` – user981849 Mar 30 '21 at 10:33