Getting started with XlBridge in C#
Writing your first server
-
Create a new console app and go to the project directory
dotnet new console XlBridgeTest cd XlBridgeTest
-
Add relevant NuGet packages
dotnet package add XlBridge.GrpcConnect.Native
-
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; } } }
-
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 aMain
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();
-
Start the server
dotnet run
-
After a few seconds, there should be a new formula
MYADD
available in Excel. Edit a cell and enter:=MYADD(20,22)
-
If all went well, the cell should now display
42
.