计算公式
Contents
[
Hide
]
添加公式和计算结果
Aspose.Cells 内嵌公式计算引擎。它不仅可以重新计算从设计器模板导入的公式,还支持计算运行时添加的公式的结果。
Aspose.Cells 支持 Microsoft Excel 中的大部分公式或函数(阅读计算引擎支持的函数列表).这些功能可以通过 API 或设计器电子表格使用。 Aspose.Cells 支持大量数学、字符串、布尔值、日期/时间、统计、数据库、查找和参考公式。
使用公式财产或设置公式(…)的方法Cell类将公式添加到单元格。应用公式时,始终以等号 (=) 开头字符串,就像在 Microsoft Excel 中创建公式时所做的那样,并使用逗号 (,) 分隔函数参数。
要计算公式的结果,用户可以调用计算公式的方法工作簿处理嵌入在 Excel 文件中的所有公式的类。或者,用户可以调用计算公式的方法工作表处理工作表中嵌入的所有公式的类。或者,用户也可以调用计算的方法Cell处理一个 Cell 的公式的类:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
重要须知
这公式财产和设置公式(…)的方法Cell班级工作不同于计算的方法工作簿, 工作表和Cell类。这公式财产和设置公式(…)方法只是将公式添加到单元格,但不在运行时计算结果。要获得公式的结果,请致电计算方法。
公式直接计算
Aspose.Cells 内嵌公式计算引擎。除了从设计器文件导入计算公式,Aspose.Cells 还可以直接计算公式结果。
有时,您需要直接计算公式结果而不将它们添加到工作表中。公式中使用的单元格值已存在于工作表中,您只需根据某些 Microsoft Excel 公式查找这些值的结果,而无需在工作表中添加公式。
您可以使用 Aspose.Cells' 公式计算引擎 APIs工作表到计算这些公式的结果而不将它们添加到工作表中:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
反复计算公式
当工作簿中的公式很多,用户需要重复计算而只修改其中的一小部分时,启用公式计算链可能对性能有帮助:公式设置.启用计算链.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
重要须知
默认情况下,计算链是禁用的。因为创建链也需要额外的时间,第一次计算公式(工作簿.CalculateFormula(…)) 与不使用链式计算公式相比,可能会消耗更多的 CPU 处理时间和内存。如果用户不需要重复计算公式,默认行为(直接计算公式而不创建计算链)应该是更好的方法。
推进主题
- 添加 Cells 到 Microsoft Excel 公式监视窗口
- 使用 Aspose.Cells 计算 IFNA 函数
- 数据表数组公式计算
- Excel 2016 MINIFS和MAXIFS函数的计算
- 减少Cell.Calculate方法的计算时间
- 检测循环引用
- 自定义函数直接计算,无需写在工作表中
- 实施自定义计算引擎以扩展 Aspose.Cells 的默认计算引擎
- 中断或取消工作簿的公式计算
- 使用 AbstractCalculationEngine 返回值范围
- 使用 ICustomFunction 返回值范围
- 设置工作簿的公式计算方式
- 在 Aspose.Cells 中使用 FormulaText 函数
- 使用 ICustomFunction 功能