Search code examples
c#epplus

How to color a row based on the cell vales in Epplus?


I am using Epplus in C#. I generated the excel file and I have columns count with some values. I want to color code the rows that have the value of count as 1. Can anyone help me?

Thanks


Solution

  • To set the background color of a cell simply use the following code:

    ws.Cells[row, col].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    ws.Cells[row, col].Style.Fill.BackgroundColor.SetColor(Color.Red);
    

    Where ws is the ExcelWorksheet object.

    In order to set anything relating to the Fill property you must specify the PatternType that is going to be used to actually fill the cell

    You have 2 options when it comes to checking the value. If you are writing to the Excel file at the time of color formatting then you can simply check the value before writing and color code the cells as you process the values. However, if you are NOT writing at the time of color formatting then you will have to read the value already present in the cell and then color it accordingly.