简体   繁体   English

如何在epplus中使用C#为Excel着色整列?

[英]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();
    }
}

} }

You can do this: 你可以这样做:

excelWorksheet.Column(i).Style.Fill.PatternType = ExcelFillStyle.Solid;
excelWorksheet.Column(i).Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml("#FF00CC"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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