Back To Tutorials

SpreadsheetGear for .NET Standard Tutorials

Console App - Spreadsheet Calculations using .NET Core CLI

Follow these steps to create a simple Console App with the .NET Core Command Line Interface (CLI) that utilizes SpreadsheetGear for .NET Standard to perform a basic formula calculation.

Prerequisites:

Create a new Console Application

  1. Open up a Command Prompt and navigate the folder where you want to add this project (note: the next step will create a subfolder fully containing this project).
  2. Enter the following commands to create a new .NET Core Console Application with a name of "SpreadsheetGearConsoleApp" and then move the current directory to this project's root:
        dotnet new console -o SpreadsheetGearConsoleApp
        cd SpreadsheetGearConsoleApp

Add SpreadsheetGear for .NET Standard to your project

  1. Enter the following commands in the Command Prompt:
        dotnet add package SpreadsheetGear
        dotnet restore
        dotnet run
    The above commands should add a dependency to SpreadsheetGear for .NET Standard, restore all NuGet dependencies in your project, then build and run the application.
  2. If the application runs correctly, you should see the text "Hello World!" outputted in the Command Prompt Window.

Add code to create a workbook and calculate a value

  1. Using your preferred text editor, open the Program.cs file located under your project's /SpreadsheetGearConsoleApp/ folder.
  2. 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.");
    }
  3. Save Console.cs.

Build and run the application

  1. From the Command Prompt, enter the following command to build and run your application:
        dotnet run
  2. You should see the following output:
    The diameter of the earth is 7926.40954629997 miles.
Back To Tutorials