计算公式

添加公式和计算结果

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

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

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

要计算公式的结果,用户可以调用计算公式的方法工作簿处理嵌入在 Excel 文件中的所有公式的类。或者,用户可以调用计算公式的方法工作表处理工作表中嵌入的所有公式的类。或者,用户也可以调用计算的方法Cell处理一个 Cell 的公式的类:

// 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' 公式计算引擎 APIs工作表计算这些公式的结果而不将它们添加到工作表中:

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

反复计算公式

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

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

重要须知

推进主题