ExcelRna.Extensions.Logging 1.8.0

dotnet add package ExcelRna.Extensions.Logging --version 1.8.0
                    
NuGet\Install-Package ExcelRna.Extensions.Logging -Version 1.8.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ExcelRna.Extensions.Logging" Version="1.8.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ExcelRna.Extensions.Logging" Version="1.8.0" />
                    
Directory.Packages.props
<PackageReference Include="ExcelRna.Extensions.Logging" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ExcelRna.Extensions.Logging --version 1.8.0
                    
#r "nuget: ExcelRna.Extensions.Logging, 1.8.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=ExcelRna.Extensions.Logging&version=1.8.0
                    
Install ExcelRna.Extensions.Logging as a Cake Addin
#tool nuget:?package=ExcelRna.Extensions.Logging&version=1.8.0
                    
Install ExcelRna.Extensions.Logging as a Cake Tool

ExcelRna.Extensions.Hosting

License NuGet Build codecov

Take advantage of .NET Generic Host in your Excel-DNA add-in.

Features

This package integrates Microsoft.Extensions.* libraries (dependency injection, configuration, logging, etc.) with ExcelDna.AddIn.

public class QuickStartAddIn : HostedExcelAddIn
{
    protected override IHostBuilder CreateHostBuilder() => Host.CreateDefaultBuilder()
        .ConfigureServices(services =>
        {
            services.AddTransient<IQuickStartService, QuickStartService>();
            services.AddExcelRibbon<QuickStartRibbon>();
            services.AddExcelFunctions<QuickStartFunctions>();
        });
}

Installation

Install ExcelRna.Extensions.Hosting package from nuget.org.

dotnet add package ExcelRna.Extensions.Hosting

Usage/Examples

Check settings in the .dna file

Make sure ExplicitRegistration is set to true in the .dna file. See Method Registration for more details. For example:

<?xml version="1.0" encoding="utf-8"?>
<DnaLibrary Name="QuickStart" RuntimeVersion="v4.0"
            xmlns="http://schemas.excel-dna.net/addin/2018/05/dnalibrary">
  <ExternalLibrary Path="QuickStart.dll" ExplicitRegistration="true" />
</DnaLibrary>
Define excel functions

ExcelFunction attributes must be declared on instance methods, not the static ones. The dependencies are resolved by DI container.

public class QuickStartFunctions
{
    private readonly IQuickStartService _quickStartService;

    public QuickStartFunctions(IQuickStartService quickStartService)
    {
        _quickStartService = quickStartService;
    }

    [ExcelFunction(Description = "Say hello to somebody")]
    public string SayHello(string name)
    {
        _quickStartService.SayHello(name);
    }
}
Define excel ribbons

Ribbon classes must be derived from HostedExcelRibbon base class. The presence of this base class stops ExcelDna from automatically discovering the ribbon and attempting to instantiate it using the default parameterless constructor. HostedExcelAddIn registers ribbons in Excel at a later stage (AutoOpen) after DI container is created.

[ComVisible(true)]
public class QuickStartRibbon : HostedExcelRibbon
{
    private readonly IQuickStartService _quickStartService;

    public QuickStartRibbon(IQuickStartService quickStartService)
    {
        _quickStartService = quickStartService;
    }

    public override string GetCustomUI(string ribbonId)
    {
        return @"
        <customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui'>
            <ribbon>
                <tabs>
                    <tab id='tab1' label='Quick Start'>
                        <group id='group1' label='Hosting'>
                            <button id='button1' label='Say Hello' tag='Ribbon' onAction='OnButtonPressed'/>
                        </group >
                    </tab>
                </tabs>
            </ribbon>
        </customUI>
        ";
    }

    public void OnButtonPressed(IRibbonControl control)
    {
        MessageBox.Show(_quickStartService.SayHello(control.Tag));
    }
}
Create and configure your add-in

Derive a class from HostedExcelAddIn and implement CreateHostBuilder abstract method. Register your services, ExcelFunctions container classes, and ExcelRibbons using ConfigureServices method. Here we use Host.CreateDefaultBuilder() from Microsfot.Extensions.Hosting (must be installed separately).

public class QuickStartAddIn : HostedExcelAddIn
{
    protected override IHostBuilder CreateHostBuilder() => Host.CreateDefaultBuilder()
        .ConfigureServices(services =>
        {
            services.AddTransient<IQuickStartService, QuickStartService>();
            services.AddExcelRibbon<QuickStartRibbon>();
            services.AddExcelFunctions<QuickStartFunctions>();
        });
}

See complete example in Source/Samples.

FAQ

What is the lifetime for ExcelFunction container classes and ExcelRibbons?

Both functions and ribbons are registered as singletons. There are no DI scopes as it's hard to define a scope for an Excel add-in.

Microsoft.Extensions.Hosting has tons of dependencies. How do I reference them all in the .dna file?

You can use msbuild target to automatically register all dependencies. See discussion or QuickStart.csproj.

License

MIT

Product Compatible and additional computed target framework versions.
.NET Framework net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.8.0 158 3/17/2025
1.0.9 3,230 4/18/2023
1.0.7 284 4/17/2023
1.0.6 236 4/17/2023
1.0.5 1,181 2/26/2022
1.0.4 496 2/17/2022
1.0.3 485 2/16/2022
1.0.2 359 11/18/2021
1.0.1 356 11/16/2021