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

可能的使用场景

Aspose.Cells 提供了一种机制,可以使用 interrupt() 方法中断或取消工作簿的公式计算抽象计算监视器班级。当工作簿的公式计算花费太多时间并且您想取消其处理时,这很有用。

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

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

示例代码

// 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