【问题标题】:Conditional Formatting by Expression using EPPlus使用 EPPlus 通过表达式进行条件格式化
【发布时间】:2025-11-24 18:45:01
【问题描述】:

我正在尝试使用 EPPlus 的条件格式化功能来格式化某些范围。我阅读了很多文档,但没有任何地方提到条件格式表达式。

我很困惑。不知道如何使用该功能。以下是我的一些问题:

  1. 我们可以使用多个范围来放入参数 ExcelAddress (如 "H1:H17,L1:L17,"AA1:AA17")
  2. 将公式放入公式属性中是否类似于 Interop Excel? (就像我们使用“A1”来表示当前单元格 用于在 interop excel 中进行格式化)
  3. 能否给我一个使用条件格式表达式的小演示代码段。

谢谢!

(对不起,我写的英语不好)

【问题讨论】:

    标签: c# excel excel-2007 conditional-formatting epplus


    【解决方案1】:

    我自己找到了解决方案。请举个例子代码:

    ExcelAddress _formatRangeAddress = new ExcelAddress("B3:B10,D3:D10,F3:F10,H3:H10:J3:J10");
    // fill WHITE color if previous cell or current cell is BLANK:
    // B3 is the current cell because the range _formatRangeAddress starts from B3.
    // OFFSET(B3,0,-1) returns the previous cell's value. It's excel function.
    string _statement = "IF(OR(ISBLANK(OFFSET(B3,0,-1)),ISBLANK(B3)),1,0)";
    var _cond4 = sheet.ConditionalFormatting.AddExpression(_formatRangeAddress);
    _cond4.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    _cond4.Style.Fill.BackgroundColor.Color = Color.White;
    _cond4.Formula = _statement;
    
    // fill GREEN color if value of the current cell is greater than 
    //    or equals to value of the previous cell
    _statement = "IF(OFFSET(B3,0,-1)-B3<=0,1,0)";
    var _cond1 = sheet.ConditionalFormatting.AddExpression(_formatRangeAddress);
    _cond1.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    _cond1.Style.Fill.BackgroundColor.Color = Color.Green;
    _cond1.Formula = _statement;
    
    // fill RED color if value of the current cell is less than 
    //    value of the previous cell
    _statement = "IF(OFFSET(B3,0,-1)-B3>0,1,0)";
    var _cond3 = sheet.ConditionalFormatting.AddExpression(_formatRangeAddress);
    _cond3.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    _cond3.Style.Fill.BackgroundColor.Color = Color.Red;
    _cond3.Formula = _statement;
    

    在上面的例子中,

    • _formatRangeAddress 是适用于 表达式的条件格式。此中的第一个单元格 范围将在条件公式中使用。 (B3)。
    • _statement 是 用于计算条件的公式,这个字符串没有 以等号 (=) 开头(与 MS Excel 的不同点), 用于表达的单元格是第一个单元格 _formatRangeAddress。 (B3)。

    希望这对其他需要的人有所帮助。 -韩-

    【讨论】:

    • 你能提供“包含”文本条件格式吗
    • 是Excel函数。我看到他们使用ISNUMBERSEARCH 函数。 office.microsoft.com/en-001/excel-help/…
    • 我想直接在 Excel 上使用条件格式,但我不清楚您的表达式中的 current。您用作表达式IF(AND(ISBLANK(OFFSET(B3,0,-1)),ISBLANK(B3)),1,0),并且在代码注释中您说// fill WHITE color if one of previous cell or current cell is BLANK:current 单元格的引用在哪里?我只看到B3
    • 嗨。 _formatRangeAddressB3 开始,所以在公式中,我使用B3,它是当前单元格。上一个单元格是OFFSET(B3,0,-1)。评论似乎不对。我将更正示例语句。
    【解决方案2】:

    EPPlus 3.1 beta 版支持条件格式。

    在此处查看源代码:http://epplus.codeplex.com/discussions/348196/

    【讨论】:

    • 其实这并没有真正关注我的问题:(
    【解决方案3】:

    经过许多个月后,我找到了使用 LINQ 和 EPPlus 更灵活、更快速的方法。您需要做的就是:在列表中添加额外的属性以保存 Excel 行号,然后使用 LINQ 检索单元格地址。在这种情况下,它看起来像这样:

    string sRng = string.Join(",", YourModel.Where(f => f.YourField == null)
        .Select(a => "H" + a.iRow + ",L" + a.iRow + ",AA" + a.iRow)); // this address could be many pages and it works
    
    if (sRng.Length > 0) {
        ws.Cells[sRng].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Green); 
    }
    

    这是全文:

    https://www.codeproject.com/Tips/1231992/Conditional-Formatting-in-Excel-with-LINQ-and-EPPl

    另请参见此处的另一个示例:https://*.com/a/49022692/8216122 希望这对将来的人有所帮助。

    【讨论】: