计算公式的方法
介绍
Aspose.Cells 内嵌公式计算引擎。它不仅可以重新计算从设计器模板导入的公式,还支持计算运行时添加的公式的结果。
添加公式和计算结果
Aspose.Cells 支持 Microsoft Excel 中的大部分公式或函数。它们可以通过 API 或使用设计器电子表格使用。 Aspose.Cells 支持大量数学、字符串、布尔值、日期/时间、统计、查找和参考公式。
使用 Cell.Formula 方法将公式添加到单元格。将公式应用于单元格时,始终以等号 (=) 开头字符串,就像在 Microsoft Excel 中创建公式时所做的那样。使用逗号 (,) 分隔函数参数。
要计算公式的结果,请调用 Workbook.CalculateFormula() 方法,该方法处理嵌入在 Excel 文件中的所有公式。请参阅以下添加公式并计算其结果的示例代码。请检查输出excel文件使用此代码生成。
示例代码
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Output directory path | |
StringPtr outPath = new String("..\\Data\\Output\\"); | |
//Path of output excel file | |
StringPtr outputAddingFormulasAndCalculatingResults = outPath->StringAppend(new String("outputAddingFormulasAndCalculatingResults.xlsx")); | |
//Create workbook | |
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(); | |
//Access first worksheet in the workbook | |
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0); | |
//Adding integer values to cells A1, A2 and A3 | |
ws->GetICells()->GetObjectByIndex(new String("A1"))->PutValue(10); | |
ws->GetICells()->GetObjectByIndex(new String("A2"))->PutValue(20); | |
ws->GetICells()->GetObjectByIndex(new String("A3"))->PutValue(70); | |
//Adding a SUM formula to "A4" cell | |
ws->GetICells()->GetObjectByIndex(new String("A4"))->SetFormula(new String("=SUM(A1:A3)")); | |
//Calculating the results of formulas | |
wb->CalculateFormula(); | |
//Get the calculated value of the cell | |
intrusive_ptr<String> strVal = ws->GetICells()->GetObjectByIndex(new String("A4"))->GetStringValue(); | |
//Print the calculated value on console | |
StringPtr str1 = new String("Calculated Result: "); | |
Console::WriteLine(str1->StringAppend(strVal)); | |
//Saving the workbook | |
wb->Save(outputAddingFormulasAndCalculatingResults); |
公式直接计算
有时,您需要直接计算公式结果而不将它们添加到工作表中。公式中使用的单元格值已存在于工作表中,您只需根据某些 Microsoft Excel 公式查找这些值的结果,而无需在工作表中添加公式。
您可以使用 Worksheet.CalculateFormula(String formula) 方法来计算此类公式的结果,而无需将它们添加到工作表中。
下面的代码产生以下输出。
Value of A1: 20
Value of A2: 30
Result of Sum(A1:A2): 50
示例代码
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Create workbook | |
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(); | |
//Access first worksheet in the workbook | |
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0); | |
//Put 20 in cell A1 | |
intrusive_ptr<ICell> cellA1 = ws->GetICells()->GetObjectByIndex(new String("A1")); | |
cellA1->PutValue(20); | |
//Put 30 in cell A2 | |
intrusive_ptr<ICell> cellA2 = ws->GetICells()->GetObjectByIndex(new String("A2")); | |
cellA2->PutValue(30); | |
//Calculate the Sum of A1 and A2 | |
intrusive_ptr<Aspose::Cells::System::Object> results = ws->CalculateFormula(new String("=Sum(A1:A2)")); | |
//Print the output | |
StringPtr str1 = new String("Value of A1: "); | |
Console::WriteLine(str1->StringAppend(cellA1->GetStringValue())); | |
StringPtr str2 = new String("Value of A2: "); | |
Console::WriteLine(str2->StringAppend(cellA2->GetStringValue())); | |
StringPtr str3 = new String("Result of Sum(A1:A2): "); | |
Console::WriteLine(str3->StringAppend(results->ToString())); |
只计算一次公式
当调用 Workbook.CalculateFormula() 计算工作簿模板中公式的值时,Aspose.Cells 创建一个计算链。当第二次或第三次计算公式时,它会提高性能。
但是,如果模板包含大量公式,第一次计算公式会消耗大量 CPU 处理时间和内存。
Aspose.Cells 允许您关闭创建计算链,这在您只想计算一次公式时很有用。
请使用 false 参数调用 Workbook.GetISettings().SetCreateCalcChain()。您可以使用提供的excel文件测试这段代码。
示例代码
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Source directory path | |
StringPtr dirPath = new String("..\\Data\\Formulas\\"); | |
//Path of input excel file | |
StringPtr sampleCalculatingFormulasOnceOnly = dirPath->StringAppend(new String("sampleCalculatingFormulasOnceOnly.xlsx")); | |
//Create workbook | |
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(sampleCalculatingFormulasOnceOnly); | |
//Set the CreateCalcChain as false | |
wb->GetISettings()->SetCreateCalcChain(false); | |
//Get the time in milliseconds before formula calculation | |
int before_miliseconds = Aspose::Cells::System::DateTime::GetNow()->GetMillisecond(); | |
//Calculate the workbook formulas | |
wb->CalculateFormula(); | |
//Get the time in milliseconds after formula calculation | |
int after_miliseconds = Aspose::Cells::System::DateTime::GetNow()->GetMillisecond(); | |
//Print the difference in milliseconds | |
StringPtr str1 = new String("Workbook Formula Calculation Elapsed Time in Milliseconds: "); | |
Console::WriteLine(str1->StringAppend(Int32Helper::ToString(after_miliseconds - before_miliseconds))); |