SpreadsheetGear Engine for .NET Tutorials
Console App - Spreadsheet Calculations using Visual Studio for Windows
Follow these steps to create a simple Console App with Visual Studio Code for Windows that utilizes SpreadsheetGear Engine for .NET to perform a basic formula calculation. Note that you can target either the full .NET Framework or .NET Core with this tutorial.
Create a new Console Application
- Launch Visual Studio 2015 or later.
- On the File menu, navigate to New → Project...
- Under Project Types / Templates, choose either:
- Visual C# → Windows → Console Application, which will target the full .NET Framework.
- Visual C# → .NET Core → Console App (.NET Core), which will target .NET Core.
- Change the Name and Location as desired.
- Click OK to create the project.
Add SpreadsheetGear Engine for .NET to your project
- On the Project menu, click Manage NuGet Packages. The NuGet Package Manager window appears.
- In the NuGet Package Manager window, click the Browse tab and enter "SpreadsheetGear" in the search field.
- In the list of NuGet packages, select "SpreadsheetGear" and click Install.
- You might be prompted to confirm this change to your project. Click OK.
- You will be prompted to accept SpreadsheetGear's License Agreement. Click I Accept if you wish to proceed.
- Visual Studio should begin the installation process and add a reference to SpreadsheetGear in your Project's References folder.
- Close the NuGet Package Manager UI Tab.
Add code to create a workbook and calculate a value
This tutorial will work with the free version of SpreadsheetGear Engine for .NET (i.e., without activating SpreadsheetGear with your Signed License. Learn more about Signed Licenses here). If you wish to use the free version of SpreadsheetGear Engine for .NET, omit the first line in the source code below.
- If Program.cs is not already open, open it now from the Solution Explorer.
- Replace the contents of the Main(...) method with the following:
static void Main(string[] args) { // If desired, activate the trial or fully licensed version of SpreadsheetGear. SpreadsheetGear.Factory.SetSignedLicense("YOUR SIGNED LICENSE HERE"); // Create a new empty workbook in a new workbook set. SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(); // Get a reference to the first worksheet. SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["Sheet1"]; // Get a reference to the top left cell of Sheet1. SpreadsheetGear.IRange a1 = worksheet.Cells["A1"]; // Set a formula. a1.Formula = "=24901.55 / PI()"; // Output the result of the formula. Console.WriteLine("The diameter of the earth is " + a1.Value + " miles."); }
Build and run the application
- From the Build menu, select Build Solution. The solution should build without errors.
- On the Debug menu, click Start Without Debugging.
- A Command Prompt Window should appear with the following output:
The diameter of the earth is 7926.40954629997 miles.