EPPlus如何检查循环引用?

时间:2013-01-08 11:26:27

标签: excel c#-4.0 epplus

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

我知道Microsoft Interop具有 Range CircularReference {get; } ,但我试图避免使用它。

1 个答案:

答案 0 :(得分:0)

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

BTW,默认情况下循环引用会抛出异常。允许他们将属性更改为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;
}