以编程方式确定 C# 中给定工作表的条件格式公式的结果,Vsto

Determine the result of a Conditional Formatting formula programmatically for a given worksheet in C#, Vsto

有没有办法通过 OpenXML 或 Interop.Excel 方法或解析器 运行 条件格式化公式,以检查给定工作表的条件是否满足。 前任。如果条件格式公式为“=$B$3>25%”,对于给定的工作表,有没有办法检查此条件的结果是真还是假。

我认为为此您可以使用 Application.Evaluate 函数并将测试单元格的格式条件公式传递给它。是您要找的吗?


简单示例:

using MSExcel = Microsoft.Office.Interop.Excel;

public static bool EvaluateConditionalFormattingFormula(MSExcel.Range cell)
{
    MSExcel.FormatConditions formatConditions = cell.FormatConditions;

    if (formatConditions == null || formatConditions.Count == 0)
        return false;

    MSExcel.FormatCondition formatCondition = formatConditions[1];
    bool result = cell.Application.Evaluate(formatCondition.Formula1);

    return result;
}