EPPlus 两种颜色条件日期格式

EPPlus two color conditional date format

我有一个包含日期的列,我想有条件地将任何超过 2 周的单元格着色为黄色,并将任何超过 90 天的单元格着色为红色。我不知道该怎么做。

我假设您的记录中有日期列的列号和行数。此外,以下循环假设第一行是您的列 header 并且记录从第二行开始。相应地更改循环计数器的初始化和分配。

 int rowsCount; //get your no of rows
            int dateColNumber; //Assign column number in excel file of your date column
            string cellValue;
            DateTime dateValue;
            DateTime today = DateTime.Now;
            double daysCount;

            for(int i=1;i<rowsCount;i++)
            {
                cellValue = ws.Cells[i + 1, dateColNumber].Text.ToString(); //First row is header start from second
                if(DateTime.TryParse(cellValue,out dateValue))
                {
                    daysCount = (today - dateValue).Days;
                    if(daysCount>90)
                    {
                        ws.Cells[i + 1,dateColNumber].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                        ws.Cells[i + 1,dateColNumber].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Red);
                    }
                    else if(daysCount>14)
                    {
                        ws.Cells[i + 1, dateColNumber].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                        ws.Cells[i + 1, dateColNumber].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Yellow);
                    }

                }
            }

应该可以像添加任何其他条件一样添加条件。可以在excel中使用TODAY()函数,减去:

[TestMethod]
public void Conditional_Formatting_Date()
{
    //
    var file = new FileInfo(@"c:\temp\Conditional_Formatting_Date.xlsx");
    if (file.Exists)
        file.Delete();

    //Throw in some data
    var dataTable = new DataTable("tblData");
    dataTable.Columns.AddRange(new[] {
        new DataColumn("Col1", typeof(DateTime)),
        new DataColumn("Col3", typeof(string))
    });

    var rnd = new Random();
    for (var i = 0; i < 100; i++)
    {
        var row = dataTable.NewRow();
        row[0] = DateTime.Now.AddDays(-rnd.Next(1, 100));
        row[1] = $"=TODAY() - A{i +1}";
        dataTable.Rows.Add(row);
    }

    //Create a test file    
    using (var package = new ExcelPackage(file))
    {
        //Make the stylesheet
        var ws = package.Workbook.Worksheets.Add("table");
        var range = ws.Cells[1, 1].LoadFromDataTable(dataTable, false);
        ws.Column(1).Style.Numberformat.Format = "mm-dd-yy";
        ws.Column(1).AutoFit();

        //Add the calc check
        var count = 0;
        foreach (DataRow row in dataTable.Rows)
            ws.Cells[++count, 2].Formula = row[1].ToString();

        //Add the conditions - order matters
        var rangeA = range.Offset(0, 0, count, 1);

        var condition90 = ws.ConditionalFormatting.AddExpression(rangeA);
        condition90.Style.Font.Color.Color = Color.White;
        condition90.Style.Fill.PatternType = ExcelFillStyle.Solid;
        condition90.Style.Fill.BackgroundColor.Color = Color.Red;
        condition90.Formula = "TODAY() - A1> 90";
        condition90.StopIfTrue = true;

        var condition14 = ws.ConditionalFormatting.AddExpression(rangeA);
        condition14.Style.Font.Color.Color = Color.Black;
        condition14.Style.Fill.PatternType = ExcelFillStyle.Solid;
        condition14.Style.Fill.BackgroundColor.Color = Color.Yellow;
        condition14.Formula = "TODAY() - A1> 14";

        package.Save();
    }
}

这在输出中给出了这个: