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

可能的使用场景

Aspose.Cells提供了使用AbstractCalculationMonitor类的interrupt()方法来中断或取消工作簿的公式计算的机制。当工作簿的公式计算时间过长并且需要取消其处理时,这将非常有用。

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

以下示例代码实现了AbstractCalculationMonitor类的beforeCalculate()方法。在此方法内,使用行和列索引参数找到单元格名称。如果单元格名称是B8,则通过调用AbstractCalculationMonitor.interrupt()方法中断计算过程。一旦实现了AbstractCalculationMonitor类的具体类,将其实例分配给CalculationOptions.CalculationMonitor属性。最后,通过传递CalculationOptions作为参数调用Workbook.calculateFormula()。请查看给定代码中用到的sample Excel file以及下面给出的控制台输出以供参考。

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Implement calculation monitor class
class clsCalculationMonitor extends AbstractCalculationMonitor
{
public 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.out.println(sheetIndex + "----" + rowIndex + "----" + colIndex + "----" + cellName);
//If cell name is B8, interrupt/cancel the formula calculation
if (cellName.equals("B8") == true)
{
this.interrupt("Interrupt/Cancel the formula calculation");
}//if
}//beforeCalculate
}//clsCalculationMonitor
//---------------------------------------------------------
//---------------------------------------------------------
public void Run() throws Exception
{
//Load the sample Excel file
Workbook wb = new Workbook(srcDir + "sampleCalculationMonitor.xlsx");
//Create calculation options and assign instance of calculation monitor class
CalculationOptions opts = new CalculationOptions();
opts.setCalculationMonitor(new clsCalculationMonitor());
//Calculate formula with calculation options
wb.calculateFormula(opts);
}

控制台输出

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

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

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