使用EPPlus进行表达式条件格式设置

20

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

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

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

谢谢!

(抱歉我的英语写得不好)

3个回答

41

我已经独立找到了解决方案。请参考以下示例代码:

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)。
希望这对需要的其他人有所帮助。-Han-

你能提供“包含”文本条件格式化吗? - Annadate Piyush
1
这是一个Excel函数。我看到他们使用了ISNUMBERSEARCH函数。http://office.microsoft.com/zh-cn/excel-help/check-if-a-cell-contains-text-HP003056106.aspx - Han
1
我想直接在Excel上使用条件格式,但是我不清楚你的表达式中的“current”。您使用的表达式为IF(AND(ISBLANK(OFFSET(B3,0,-1)),ISBLANK(B3)),1,0),并且在代码注释中,您说“//如果之前的单元格或当前单元格为空,则填充白色颜色:”。那么“current”单元格的参考在哪里?我只看到了“B3”。 - jotapdiez
1
嗨。_formatRangeAddressB3 开始,所以在公式中,我使用 B3 作为当前单元格。上一个单元格是 OFFSET(B3,0,-1)。评论似乎不正确。我会更正示例语句。 - Han

1

实际上,这并没有真正关注我的问题 :( - Han

0
经过漫长的时间,我发现使用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://stackoverflow.com/a/49022692/8216122 希望能帮助到未来的某个人。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接