Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to color entire column in excel using c# in epplus?

I have written a code which colors a single cell which matches the text in cell, but I want to color the entire column which has a matching text in the header row

using System.Drawing;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System.IO;
namespace Project32
{
public class Class1
{
    public static void Main()
    {

        FileInfo newFile = new FileInfo(@"C:\Users\mvmurthy\Downloads\Template.xlsx");
        ExcelPackage pck = new ExcelPackage(newFile);
        var ws = pck.Workbook.Worksheets["ImportTemplate"];
        var start = ws.Dimension.Start;
        var end = ws.Dimension.End;
        for (int col = start.Column; col <= end.Column; col++)
        { // ... Cell by cell...
            if (ws.Cells[1, col].Text == "Tracking Numbers")
            {
                ws.Cells[1, col].Style.Fill.PatternType = ExcelFillStyle.Solid;
                ws.Cells[1, col].Style.Fill.BackgroundColor.SetColor(Color.Red);
            }
        }

        pck.Save();
    }
}

}

like image 382
manvitha Avatar asked Oct 21 '25 22:10

manvitha


1 Answers

You can do this:

excelWorksheet.Column(i).Style.Fill.PatternType = ExcelFillStyle.Solid;
excelWorksheet.Column(i).Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml("#FF00CC"));
like image 50
VDWWD Avatar answered Oct 24 '25 11:10

VDWWD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!