SpreadsheetGear Engine for .NET Tutorials
Console App - Spreadsheet Calculations using Visual Studio Code for Mac / Linux / Windows
Follow these steps to create a simple Console App with Visual Studio Code for Mac / Linux / Windows that utilizes SpreadsheetGear Engine for .NET to perform a basic formula calculation.
Prerequisites:
- This tutorial assumes you already have both Visual Studio Code and the .NET Core SDK installed on your Mac. If not, please use the following links to install these now:
Create a new Console Application
- Launch Visual Studio Code.
- On the View menu, navigate to Integrated Terminal. A Terminal pane should appear at the bottom of the Visual Studio Code window.
- Enter the following command to create a new project from the "Console Application" template, giving the Project a name of "ConsoleApp":
dotnet new console -o ConsoleApp
You should see a couple messages indicating the generation time and success of the command. - From the File menu, go to Open... and navigate to the folder where you just ran the above command from the Terminal.
- Select the "ConsoleApp" folder and click the "Open" button to open the project.
Add SpreadsheetGear Engine for .NET to your project
- Go back to the Terminal pane and enter the following command:
dotnet add package SpreadsheetGear
- SpreadsheetGear Engine for .NET is now added to your project.
Add code to create a workbook and calculate a value
- Open Program.cs from the Explorer pane.
- You should see the following prompts appear at the top of the window:
- "Required assets to build and debug are missing from 'ConsoleApp'. Add them?"
- "There are unresolved dependencies from 'ConsoleApp.csproj'. Please execute the restore command to continue."
- Replace the contents of the Main(...) method with the following:
static void Main(string[] args) { // 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."); }
- Save Console.cs by going to File → Save.
Build and run the application
- From the Terminal pane, run the following command:
dotnet run
- You should see the following output in the Terminal pane:
The diameter of the earth is 7926.40954629997 miles.