计算公式

添加公式及计算结果

Aspose.Cells内置了一个公式计算引擎。它不仅可以重新计算从设计模板导入的公式,还支持计算在运行时添加的公式的结果。

Aspose.Cells支持大多数Microsoft Excel公式或函数(阅读a list of the functions supported by the calculation engine)。这些函数可以通过API或设计工作表来使用。Aspose.Cells支持大量的数学、字符串、布尔、日期/时间、统计、数据库、查找和引用公式。

使用 Formula 属性或 Cell 类的 SetFormula(…) 方法来向单元格添加公式。在应用公式时,始终以等号(=)开头,就像在Microsoft Excel中创建公式时一样,并使用逗号(,)来分隔函数参数。

要计算公式的结果,用户可以调用 Workbook 类的 CalculateFormula 方法,该方法处理Excel文件中嵌入的所有公式。或者,用户可以调用 Worsheet 类的 CalculateFormula 方法,该方法处理工作表中嵌入的所有公式。用户还可以调用 Cell 类的 Calculate 方法,该方法处理一个单元格的公式。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CalculatingFormulas.class) + "formulas/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int sheetIndex = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Adding a value to "A1" cell
worksheet.getCells().get("A1").putValue(1);
// Adding a value to "A2" cell
worksheet.getCells().get("A2").putValue(2);
// Adding a value to "A3" cell
worksheet.getCells().get("A3").putValue(3);
// Adding a SUM formula to "A4" cell
worksheet.getCells().get("A4").setFormula("=SUM(A1:A3)");
// Calculating the results of formulas
workbook.calculateFormula();
// Get the calculated value of the cell
String value = worksheet.getCells().get("A4").getStringValue();
// Saving the Excel file
workbook.save(dataDir + "CalculatingFormulas_out.xls");

重要知识

直接计算公式

Aspose.Cells内置了一个公式计算引擎。除了计算从设计文件导入的公式外,Aspose.Cells还可以直接计算公式的结果。

有时,您需要直接计算公式的结果,而无需将它们添加到工作表中。公式中使用的单元格的值已经存在于工作表中,您只需要根据一些Microsoft Excel公式找到这些值的结果,而不需要将公式添加到工作表中。

您可以使用Aspose.Cells的公式计算引擎API来 Worksheet 到 calculate 不将这些公式添加到工作表中的结果。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(DirectCalculationFormula.class) + "formulas/";
// Create a workbook
Workbook workbook = new Workbook();
// Access first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Put 20 in cell A1
Cell cellA1 = worksheet.getCells().get("A1");
cellA1.putValue(20);
// Put 30 in cell A2
Cell cellA2 = worksheet.getCells().get("A2");
cellA2.putValue(30);
// Calculate the Sum of A1 and A2
Object results = worksheet.calculateFormula("=Sum(A1:A2)");
// Print the output
System.out.println("Value of A1: " + cellA1.getStringValue());
System.out.println("Value of A2: " + cellA2.getStringValue());
System.out.println("Result of Sum(A1:A2): " + results.toString());

以上代码生成以下输出:

Value of A1: 20
Value of A2: 30
Result of Sum(A1:A2): 50.0

重复计算公式

当工作簿中有大量公式,用户需要重复计算它们,并且只需修改其中的一小部分时,启用公式计算链(FormulaSettings.EnableCalculationChain)可能有助于提高性能。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CalculatingFormulasOnce.class) + "formulas/";
// Load the template workbook
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Print the time before formula calculation
System.out.println(DateTime.getNow());
// Set the CreateCalcChain as true
workbook.getSettings().getFormulaSettings().setEnableCalculationChain(true);
// Calculate the workbook formulas
workbook.calculateFormula();
Cells cells = workbook.getWorksheets().get("Sheet1").getCells();
//with original values, the calculated result
System.out.println(cells.get("A11").getValue());
//update one value the formula depends on
cells.get("A5").putValue(15);
// Calculate the workbook formulas again, in fact only A11 needs to be and will be calculated
workbook.calculateFormula();
//check the re-calculated value
System.out.println(cells.get("A11").getValue());
// Print the time after formula calculation
System.out.println(DateTime.getNow());

重要知识

高级主题