中断或取消工作簿的公式计算

可能的使用场景

Aspose.Cells 提供中断或取消工作簿公式计算的机制AbstractCalculationMonitor.Interrupt()方法。当工作簿的公式计算花费太多时间并且您想取消其处理时,这很有用。

中断或取消工作簿的公式计算

下面的示例代码实现了计算前()的方法抽象计算监视器班级。在此方法内部,它使用行和列索引参数查找单元格名称。如果单元格名称是 B8,它会通过调用AbstractCalculationMonitor.Interrupt()方法。曾经,具体类抽象计算监视器类被实现,它的实例被分配给计算选项.计算监视器财产。最后,工作簿.CalculateFormula()通过传递调用计算选项作为参数。请参阅示例 Excel 文件代码内部使用以及下面给出的代码的控制台输出供参考。

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
class InterruptOrCancelFormulaCalculationOfWorkbook
{
//Implement calculation monitor class
class clsCalculationMonitor : AbstractCalculationMonitor
{
public override void BeforeCalculate(int sheetIndex, int rowIndex, int colIndex)
{
//Find the cell name
string cellName = CellsHelper.CellIndexToName(rowIndex, colIndex);
//Print the sheet, row and column index as well as cell name
System.Diagnostics.Debug.WriteLine(sheetIndex + "----" + rowIndex + "----" + colIndex + "----" + cellName);
//If cell name is B8, interrupt/cancel the formula calculation
if (cellName == "B8")
{
this.Interrupt("Interrupt/Cancel the formula calculation");
}//if
}//BeforeCalculate
}//clsCalculationMonitor
public static void Run()
{
//Load the sample Excel file
Workbook wb = new Workbook("sampleCalculationMonitor.xlsx");
//Create calculation options and assign instance of calculation monitor class
CalculationOptions opts = new CalculationOptions();
opts.CalculationMonitor = new clsCalculationMonitor();
//Calculate formula with calculation options
wb.CalculateFormula(opts);
}
}

控制台输出

 0----1----3----D2

0----4----6----G5

0----7----1----B8