【问题标题】:EPPlus how to check for circular references?EPPlus 如何检查循环引用?
【发布时间】:2013-01-08 11:26:26
【问题描述】:

我正在使用 EPPlus 库来构建一个 excel 文件,并且想知道是否有一种方法可以使用该库来检查公式中的循环引用?

我知道Microsoft Interop 有这个功能 Range CircularReference { get; },但我尽量避免使用它。

【问题讨论】:

    标签: excel c#-4.0 epplus


    【解决方案1】:

    如果存在循环引用并且您不允许在计算选项中使用它,则在尝试计算时会抛出异常。

    顺便说一句,默认情况下循环引用会抛出异常。允许他们将属性更改为 true。

    您还可以计算整个工作表/工作簿。

    检查单元格是否包含循环引用的示例代码:

    //Assign Formula
    var cell = worksheet.Cells["A2"]; //Example Cell
    cell.Formula = "YourFormula";
    
    //Initiate calculation option
    var calculateOptions = new ExcelCalculationOption();
    calculateOptions.AllowCirculareReferences = false;
    
    bool isFormulaCircularReference = false;
    try
    {
        cell.Calculate(calculateOptions);
    }
    catch (CircularReferenceException ex)
    {
        //If there is a circular reference this exception will be thrown
        isFormulaCircularReference = true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2014-02-26
      • 2023-04-08
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多