Excel Formula Calculation in C# and VB.NET

The following example shows how to use the GemBox.Spreadsheet calculation engine to calculate Excel cell formula values in C# and VB.NET.

The example demonstrates how to set cell formulas and calculate their values by calling one of the methods: ExcelCell.Calculate, ExcelWorksheet.Calculate or ExcelFile.Calculate. The calculation result is stored as ExcelCell.Value.

Notice that below you have Run options where you can choose output file format.

Screenshot

Excel Formula Calculation Screenshot

See the full code below, use RUN EXAMPLE to execute.


1using System;
2using GemBox.Spreadsheet;
3
4class Sample
5{
6    [STAThread]
7    static void Main(string[] args)
8    {
9        // If using Professional version, put your serial key below.
10        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
11
12        ExcelFile ef = new ExcelFile();
13        ExcelWorksheet ws = ef.Worksheets.Add("Formula Calculation");
14
15        // Some formatting.
16        ExcelRow row = ws.Rows[0];
17        row.Style.Font.Weight = ExcelFont.BoldWeight;
18
19        ExcelColumn col = ws.Columns[0];
20        col.SetWidth(250, LengthUnit.Pixel);
21        col.Style.HorizontalAlignment = HorizontalAlignmentStyle.Left;
22        col = ws.Columns[1];
23        col.SetWidth(250, LengthUnit.Pixel);
24        col.Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
25
26        // Use first row for column headers.
27        ws.Cells["A1"].Value = "Formula";
28        ws.Cells["B1"].Value = "Calculated value";
29
30        // Enter some Excel formulas as text in first column.
31        ws.Cells["A2"].Value = "=1 + 1";
32        ws.Cells["A3"].Value = "=3 * (2 - 8)";
33        ws.Cells["A4"].Value = "=3 + ABS(B3)";
34        ws.Cells["A5"].Value = "=B4 > 15";
35        ws.Cells["A6"].Value = "=IF(B5, \"Hello world\", \"World hello\")";
36        ws.Cells["A7"].Value = "=B6 & \" example\"";
37        ws.Cells["A8"].Value = "=CODE(RIGHT(B7))";
38        ws.Cells["A9"].Value = "=POWER(B8, 3) * 0.45%";
39        ws.Cells["A10"].Value = "=SIGN(B9)";
40        ws.Cells["A11"].Value = "=SUM(B2:B10)";
41
42        // Set text from first column as second row cell's formula.
43        int rowIndex = 1;
44        while (ws.Cells[rowIndex, 0].ValueType != CellValueType.Null)
45            ws.Cells[rowIndex, 1].Formula = ws.Cells[rowIndex++, 0].StringValue;
46
47        // GemBox.Spreadsheet supports single Excel cell calculation, ...
48        ws.Cells["B2"].Calculate();
49
50        // ... Excel worksheet calculation,
51        ws.Calculate();
52
53        // ... and whole Excel file calculation.
54        ws.Parent.Calculate();
55
56        ef.Save("Formula Calculation.xls");
57    }
58}
1Imports System
2Imports GemBox.Spreadsheet
3
4Module Samples
5
6    Sub Main()
7
8        ' If using Professional version, put your serial key below.
9        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
10
11        Dim ef As ExcelFile = New ExcelFile
12        Dim ws As ExcelWorksheet = ef.Worksheets.Add("Formula Calculation")
13
14        ' Some formatting.
15        Dim row As ExcelRow = ws.Rows(0)
16        row.Style.Font.Weight = ExcelFont.BoldWeight
17
18        Dim col As ExcelColumn = ws.Columns(0)
19        col.SetWidth(250, LengthUnit.Pixel)
20        col.Style.HorizontalAlignment = HorizontalAlignmentStyle.Left
21        col = ws.Columns(1)
22        col.SetWidth(250, LengthUnit.Pixel)
23        col.Style.HorizontalAlignment = HorizontalAlignmentStyle.Right
24
25        ' Use first row for column headers.
26        ws.Cells("A1").Value = "Formula"
27        ws.Cells("B1").Value = "Calculated value"
28
29        ' Enter some Excel formulas as text in first column.
30        ws.Cells("A2").Value = "=1 + 1"
31        ws.Cells("A3").Value = "=3 * (2 - 8)"
32        ws.Cells("A4").Value = "=3 + ABS(B3)"
33        ws.Cells("A5").Value = "=B4 > 15"
34        ws.Cells("A6").Value = "=IF(B5, ""Hello world"", ""World hello"")"
35        ws.Cells("A7").Value = "=B6 & "" example"""
36        ws.Cells("A8").Value = "=CODE(RIGHT(B7))"
37        ws.Cells("A9").Value = "=POWER(B8, 3) * 0.45%"
38        ws.Cells("A10").Value = "=SIGN(B9)"
39        ws.Cells("A11").Value = "=SUM(B2:B10)"
40
41        ' Set text from first column as second row cell's formula.
42        Dim rowIndex As Integer = 0
43        While ws.Cells(rowIndex, 0).ValueType <> CellValueType.Null
44            ws.Cells(rowIndex, 1).Formula = ws.Cells(rowIndex, 0).StringValue
45            rowIndex += 1
46        End While
47
48        ' GemBox.Spreadsheet supports single Excel cell calculation, ...
49        ws.Cells("B1").Calculate()
50
51        ' ... Excel worksheet calculation,
52        ws.Calculate()
53
54        ' ... and whole Excel file calculation.
55        ws.Parent.Calculate()
56        ef.Save("Formula Calculation.xls")
57
58    End Sub
59
60End Module

Check next sample or find out more about GemBox.Spreadsheet and GemBox Software.