计算公式

添加公式和计算结果

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

Aspose.Cells 支持 Microsoft Excel 中的大部分公式或函数(阅读计算引擎支持的函数列表).这些功能可以通过 API 或设计器电子表格使用。 Aspose.Cells 支持大量数学、字符串、布尔值、日期/时间、统计、数据库、查找和参考公式。

使用公式财产或设置公式(…) 的方法Cell类将公式添加到单元格。应用公式时,始终以等号 (=) 开头字符串,就像在 Microsoft Excel 中创建公式时所做的那样,并使用逗号 (,) 分隔函数参数。

要计算公式的结果,用户可以调用[计算公式](https://reference.aspose.com/cells/java/com.aspose.cells/workbook#calculateFormula(com.aspose.cells.CalculationOptions)的方法工作簿处理嵌入在 Excel 文件中的所有公式的类。或者,用户可以调用[计算公式](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#calculateFormula(com.aspose.cells.CalculationOptions,%20boolean)的方法工作表处理工作表中嵌入的所有公式的类。或者,用户也可以调用[计算](https://reference.aspose.com/cells/java/com.aspose.cells/cell#calculate(com.aspose.cells.CalculationOptions)的方法Cell处理一个 Cell 的公式的类:

// 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' 公式计算引擎 APIs工作表到[计算](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#calculateFormula(java.lang.String,%20com.aspose.cells.CalculationOptions)此类公式的结果而不将它们添加到工作表中:

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

反复计算公式

当工作簿中的公式很多,用户需要重复计算而只修改其中的一小部分时,启用公式计算链可能对性能有帮助:公式设置.启用计算链.

// 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());

重要须知

推进主题