Getting started with XlBridge in C#

Writing your first server

  1. Create a new console app and go to the project directory

     dotnet new console XlBridgeTest
     cd XlBridgeTest
    
  2. Add relevant NuGet packages

     dotnet package add XlBridge.GrpcConnect.Native
    
  3. Add a file CustomFunctions.cs with the following content:

     namespace XlBridgeTest 
     {
         public class CustomFunctions
         {
             public static double MyAdd(double foo, double bar)
             {
                 return foo + bar;
             }
         }
     }
    
  4. Replace the content of Program.cs with:

    This file uses the new top-level statements style available from C# 9.0. It needs to be adjusted for use with older versions of C# using namespace, class and a Main method.

     using System;
     using XlBridgeTest;
     using XlBridge.Integration;
    
     var testServer = new BridgeServiceBuilder()
         .AddFunctions.FromType<CustomFunctions>()
         .CreateNativeGrpcServer();
    
     testServer.Start();
    
     Console.WriteLine("Server is started. Press Enter to quit.");
     Console.ReadLine();
    
     await testServer.ShutdownAsync();
    
  5. Start the server

     dotnet run
    
  6. After a few seconds, there should be a new formula MYADD available in Excel. Edit a cell and enter:

     =MYADD(20,22)
    
  7. If all went well, the cell should now display 42.

Read more