Back To .NET Standard Tutorials

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

  1. Launch Visual Studio Code.
  2. On the View menu, navigate to Integrated Terminal. A Terminal pane should appear at the bottom of the Visual Studio Code window.
  3. 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.
  4. From the File menu, go to Open... and navigate to the folder where you just ran the above command from the Terminal.
  5. Select the "ConsoleApp" folder and click the "Open" button to open the project.

Add SpreadsheetGear Engine for .NET to your project

  1. Go back to the Terminal pane and enter the following command:
    dotnet add package SpreadsheetGear
  2. SpreadsheetGear Engine for .NET is now added to your project.

Add code to create a workbook and calculate a value

  1. Open Program.cs from the Explorer pane.
  2. 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."
    Hit "Yes" for the first message and "Restore" for the second.
  3. 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.");
    }
  4. Save Console.cs by going to File → Save.

Build and run the application

  1. From the Terminal pane, run the following command:
    dotnet run
  2. You should see the following output in the Terminal pane:
    The diameter of the earth is 7926.40954629997 miles.