EPPlus 条件格式

EPPlus Conditional Formatting

我试着关注这个:Conditional Formatting by Expression using EPPlus

但在我的例子中,excel 文件 已损坏 并且给我选择在删除规则的情况下恢复。

我想实现这个(简化): screenshot

这是我的代码(A 列):

ExcelWorksheet ew = ep.Workbook.Worksheets.Add("Sheet1");

var cells = new ExcelAddress("A2:A5");
string formula = "ISNUMBER(SEARCH($A;C2))";
var condition = ew.ConditionalFormatting.AddExpression(cells);
condition.Formula = formula;
condition.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
condition.Style.Fill.BackgroundColor.Color = System.Drawing.Color.Yellow;

提前致谢

对于初学者来说,公式中缺少 =。我不知道 SEARCH($A;C2) 的目的是什么,但下面的代码有效。

//the range of cells to be searched
var cells = new ExcelAddress("A1:Z10");

//the excel formula, note that it uses the top left cell of the range
//so if the range was C5:d10, it would be =ISNUMBER(C5)
var formula = "=ISNUMBER(A1)";

var condition = worksheet.ConditionalFormatting.AddExpression(cells);
condition.Formula = formula;
condition.Style.Fill.PatternType = ExcelFillStyle.Solid;
condition.Style.Fill.BackgroundColor.Color = Color.Yellow;

您收到损坏错误的原因是公式中的分号。 分号不是此公式中的有效运算符。

回应 VDWWD - 我认为等号不是问题,如果在公式中使用等号,我会收到损坏错误。

来自 EPPlus 文档

  • 不要使用本地化的函数名称。仅支持英文名称(如 SUM、IF、VLOOKUP 等)。
  • 不要使用分号作为函数参数之间的分隔符。仅支持逗号。
  • 不要在公式中添加前导 = 号。 "=SUM(A1:A2)" 是错误的,"SUM(A1:A2)" 是正确的。

Epplus Formula Calculation