EPPlus两色条件日期格式

时间:2019-06-24 17:53:08

标签: epplus

我有一列带有日期的列,我想有条件地为任何早于2周的单元格着色为黄色,而将早于90天的单元格着色为红色。我不知道该怎么做。

2 个答案:

答案 0 :(得分:0)

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

[TestMethod]
public void Conditional_Formatting_Date()
{
    //https://stackoverflow.com/questions/56741642/epplus-two-color-conditional-date-format
    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();
    }
}

哪个在输出中给出:

enter image description here

答案 1 :(得分:-1)

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

 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);
                    }

                }
            }