SpreadsheetGear Engine for .NET 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 Engine for .NET to perform a basic formula calculation.
Prerequisites:
- This tutorial assumes you already have the .NET Core SDK installed on your machine. If not, please go to https://dotnet.microsoft.com/download to install it now.
Create a new Console Application
- 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).
- 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 Engine for .NET to your project
- 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 Engine for .NET, restore all NuGet dependencies in your project, then build and run the application. - 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
- Using your preferred text editor, open the Program.cs file located under your project's /SpreadsheetGearConsoleApp/ folder.
- 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.
Build and run the application
- From the Command Prompt, enter the following command to build and run your application:
dotnet run
- You should see the following output:
The diameter of the earth is 7926.40954629997 miles.