Calculating Formulas

Skip to end of metadata
Go to start of metadata
Formula calculation engine is embedded in Aspose.Cells. It can not only re-calculate the formula imported from the designer file but also supports to calculate the results of formulas added at runtime.

Adding Formulas & Calculating Results

Aspose.Cells supports most of the formulas or functions that are the part of Microsoft Excel. Developers can use these formulas using API or Designer Spreadsheets. Aspose.Excel supports a huge set of Mathematical, String, Boolean, Date/Time, Statistical, Database, Lookup and Reference formulas.
Developers can use Formula property of the Cell class to add a formula to a cell. When applying a formula to a cell, always begin the string with an equal sign (=) as you do when creating a formula in Microsoft Excel and use a comma (,) to delimit function parameters.
To calclulate the results of the formulas, call CalculateFormula method of the Excel class that processes all formulas embedded in an Excel file. For the list of functions supported by CalculateFormula method, Click Here .

Currently, Aspose.Cells supports the following operators: +, -, *, /, <, <=, =, >=, >, <>, &, %, ^.


Example:

[C#]
//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("D:\\test.xls");
 


[VB.NET]
'Instantiating a Workbook object
Dim Workbook As Workbook = New Workbook()

'Adding a new worksheet to the Excel object
Dim sheetIndex As Integer = Workbook.Worksheets.Add()

'Obtaining the reference of the newly added worksheet by passing its sheet index
Dim worksheet As 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
Dim value As String = worksheet.Cells("A4").Value.ToString()

'Saving the Excel file
Workbook.Save("D:\test.xls", FileFormatType.Default)
 

Important to Know

Formula property of Cell class works differently from the CalculateFormula method of Excel class. Formula property simply adds the formula to a cell but doesn't calculate the formula results at runtime, which is done by CalculateFormula method.
Provides a list of all functions or formulas supported by Formula Calculation Engine.
10/21/2007 8:15:29 PM - -222.190.3.131

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.