如何在epplus中使用c#为excel中的整列上色
本文关键字:excel epplus | 更新日期: 2023-09-27 18:01:11
我已经编写了一个代码,为与单元格中文本匹配的单个单元格着色,但我想为标题行中具有匹配文本的整列着色
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();
}
}
}
您可以这样做:
excelWorksheet.Column(i).Style.Fill.PatternType = ExcelFillStyle.Solid;
excelWorksheet.Column(i).Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml("#FF00CC"));