【问题标题】:EPPlus Conditional FormattingEPPlus 条件格式
【发布时间】:2018-10-10 10:18:38
【问题描述】:

我试过关注这个: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$1;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;

提前致谢

【问题讨论】:

  • 首先尝试将条件格式添加到 4 个单元格,A2、A3、A4、A5,而不是在一个范围内进行。当我尝试在某个范围内进行操作时遇到了一些问题。

标签: c# .net epplus


【解决方案1】:

对于初学者,公式中缺少=。而且我不知道SEARCH($A$1;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;

【讨论】:

    【解决方案2】:

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

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

    来自 EPPlus 文档

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

    Epplus Formula Calculation

    【讨论】:

    • 啊,谢谢指出:只支持逗号。我的 Excel 的默认分隔符是分号。
    • 啊,直到现在我才知道这是可能的。不确定你是否愿意,但看起来你可以通过语言设置来改变它(假设你在 Windows 上):lockone.wordpress.com/2015/08/06/…
    猜你喜欢
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    相关资源
    最近更新 更多