Getting Started
The easiest way to make an Excel-DNA addin is to create to follow these simple steps:
Create a Project in Visual Studio
- Select Create a new project and then select Class Library in either Visual Basic, C# or F#.
- Enter a name for the project.
- Under Framework, select the .NET 6.0 (Long-term support) option.
Write the Addin Code
Depending on the language of choice, in the .csproj, .vbproj, or .fsproj file, change the value between the TargetFramework tags to net6.0-windows.
Add the following under </PropertyGroup>:
<ItemGroup>
<PackageReference Include="ExcelDna.Addin" Version="1.7.0"/>
</ItemGroup>Depending on the language of choice (C#, Visual Basic.NET or F#), add the following code to the class file (.cs, .vb or .fs):
- C#
- VB.Net
- F#
using ExcelDna.Integration;
public static class MyFunctions
{
[ExcelFunction(Description = "My first .NET function")]
public static string SayHello(string name)
{
return "Hello " + name;
}
}Imports ExcelDna.Integration
Public Module MyFunctions
<ExcelFunction(Description:="My first .NET function")>
Public Function SayHello(ByVal name As String) As String
Return "Hello " & name
End Function
End Modulemodule MyFunctions =
open Excel.Integration
[<ExcelFunction(Description = "My first .NET function")>]
let SayHello name = "Hello " + name
Compile and Run
To compile the solution, ensure to explicitly select Build Solution, under the Build menu item at the top menu bar. Alternatively, press the Ctrl+Shift+B key combination.
To run the code after compilation, select Start Debugging, under the Debug menu item at the top menu bar. Alternatively, press F5.
When the solution is running, Excel will open and a security notice will pop-up. Select the Enable this add-in for this session only. option.
In Excel, open a new workbook and use the newly created function:
=SayHello("World!")
Debug
It is possible to debug the solution through Visual Studio. To do so, follow these simple steps while the solution is running:
- In Visual Studio, navigate to the line of code that is required debugging.
- Create a breakpoint by selecting Toggle Breakpoint, under the Debug menu item at the top menu bar. Alternatively, press F9. The line of code would be highlighted in red.
- In Excel, use the function that is needed to be debugged. The execution of the function will be caught by Visual Studio at the breakpoint. The line of code would be highlighted in yellow.
- In Visual Studio, inspect the code and change it as required. Once done, select Continue, under the Debug menu item at the top bar. Alternatively, press F5.
- Finally, see the new results reflect in Excel upon completion of execution of the debugged function.
Distribution
In order to use the newly created add-in, users would require the .NET 6 runtime to be installed. Additionally, the correct architecture (32bit or 64bit) of the installation should be taken into consideration.
Transitioning to .NET 6
In recent years, Microsoft has moved from the older .NET Framework to the newer, cross-platform .NET 6. The transition brings new features, better performance, and a simpler project format. For more details, see .NET Framework vs. .NET 6 and the walkthrough guide Updating Project File to SDK-style.