【问题标题】:ClosedXML when Working With Conditional Formats using formulas - the formula is incorrect使用公式处理条件格式时的 ClosedXML - 公式不正确
【发布时间】:2017-02-01 16:50:36
【问题描述】:

我正在使用带有 ClosedXML 的条件格式,但我遇到了 2 个问题。首先,如果我将条件设置为基于这样的值:

> RangeToAdd.AddConditionalFormat().WhenLessThan(0).Fill.SetBackgroundColor(XLColor.Red).Font.SetFontColor(XLColor.Red);

但是,当我需要将其设置为相对单元格时,它不起作用。这是我尝试过的:

RangeToAdd.AddConditionalFormat().WhenLessThan("\"=RC[-19]\"").Fill.SetBackgroundColor(XLColor.Yellow);
RangeToAdd.AddConditionalFormat().WhenGreaterThan("\"=RC[-19]+RC[-18]+RC[-17]\"").Fill.SetBackgroundColor(XLColor.BabyBlue);

它不工作。它是添加 ="= 然后是不正确的公式。我按照文档中所说的 here 进行了尝试,但也没有转义引号。

另一个问题很小,但我无法弄清楚。如何设置条件为真时停止。

【问题讨论】:

    标签: c# excel closedxml


    【解决方案1】:

    您添加了太多引号:根据文档,这很简单

    WhenLessThan("=RC[-19]") // But Excel can't read it unfortunately 
    

    一种可能的解决方法

    WhenLessThan("=" + RC(RangeToAdd,0,-19))
    

    类似

    WhenGreaterThan("=" + RC(RangeToAdd,0,-19) + "+" + RC(RangeToAdd,0,-18) + "+" + RC(RangeToAdd,0,-17))
    

    使用助手

        static string RC(IXLRange range, int r, int c)
        {
            return range.FirstCell().CellBelow(r).CellRight(c).Address.ToString(XLReferenceStyle.A1);
        }
    

    【讨论】:

    • 我也试过了,还是不行。这会在条件格式中产生不可读的内容错误
    • 当且仅当我知道条件的确切单元格时才有效。因此,如果我输入 WhenLessThan("B2") 那么它可以工作,但是当我使用相对引用时它不会
    • 开启R1C1样式可以在excel中输入=RC[-19]。
    猜你喜欢
    • 2015-12-11
    • 2018-03-08
    • 1970-01-01
    • 2014-12-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    相关资源
    最近更新 更多