计算公式

添加公式及计算结果

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

Aspose.Cells支持大部分Microsoft Excel公式或函数(阅读 计算引擎支持的函数列表)。这些函数可以通过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-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int sheetIndex = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[sheetIndex];
// Adding a value to "A1" cell
worksheet.Cells["A1"].PutValue(1);
// Adding a value to "A2" cell
worksheet.Cells["A2"].PutValue(2);
// Adding a value to "A3" cell
worksheet.Cells["A3"].PutValue(3);
// Adding a SUM formula to "A4" cell
worksheet.Cells["A4"].Formula = "=SUM(A1:A3)";
// Calculating the results of formulas
workbook.CalculateFormula();
// Get the calculated value of the cell
string value = worksheet.Cells["A4"].Value.ToString();
// Saving the Excel file
workbook.Save(dataDir + "output.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-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Create a workbook
Workbook workbook = new Workbook();
// Access first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Put 20 in cell A1
Cell cellA1 = worksheet.Cells["A1"];
cellA1.PutValue(20);
// Put 30 in cell A2
Cell cellA2 = worksheet.Cells["A2"];
cellA2.PutValue(30);
// Calculate the Sum of A1 and A2
var results = worksheet.CalculateFormula("=Sum(A1:A2)");
// Print the output
System.Console.WriteLine("Value of A1: " + cellA1.StringValue);
System.Console.WriteLine("Value of A2: " + cellA2.StringValue);
System.Console.WriteLine("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-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Load the template workbook
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Print the time before formula calculation
Console.WriteLine(DateTime.Now);
// Set the CreateCalcChain as tue
workbook.Settings.FormulaSettings.EnableCalculationChain = true;
// Calculate the workbook formulas
workbook.CalculateFormula();
// Print the time after formula calculation
Console.WriteLine(DateTime.Now);
//change the value of one cell
workbook.Worksheets[0].Cells["A1"].PutValue("newvalue");
//re-calculate those formulas which depend on cell A1
workbook.CalculateFormula();

重要知识

高级主题