Excel-DNA

repository·master·Indexed 23 days ago

https://github.com/excel-dna/exceldna

A .NET framework for building Excel Add-ins and User Defined Functions (UDFs) using C# and the .NET ecosystem. It provides tools for creating .xll add-ins, implementing Ribbon UIs, and leveraging Native AOT publishing. The ecosystem includes packages such as ExcelDna.AddIn for full add-in development, ExcelDna.Integration for API referencing, and ExcelDna.Interop for Microsoft Office primary interop assemblies.

Tokens
5.9K
Snippets
14
Records
36
Agent score
80%

What's inside Excel-DNA

  1. Use ExcelDna.Interop for Office Interop Assemblies

    master

    The ExcelDna.Interop package provides local copies of essential Microsoft Office primary interop assemblies, allowing you to automate Excel and the Visual Basic Editor (VBE) from .NET.

    Included assemblies:

    • Microsoft.Office.Interop.Excel.dll
    • Microsoft.Vbe.Interop.dll
    • office.dll

    Supported frameworks:

    • .NET Framework 4.5.2
    • net6.0-windows7.0

    Build Behavior: For C# and Visual Basic projects, the package is configured to embed the interop types into your assembly rather than copying the DLLs to the output directory. This helps keep your deployment clean. Note that F# projects do not follow this automatic embedding behavior and may require manual configuration.

  2. Use ExcelDna.Interop.Dao for Microsoft Access DAO support

    master

    The ExcelDna.Interop.Dao package provides a local copy of the Microsoft.Office.Interop.Access.Dao.dll primary interop assembly. This allows your Excel-DNA project to interact with the Microsoft Access Data Access Objects (DAO) library.

    Supported Frameworks

    • .NET Framework 4.5.2
    • net6.0-windows7.0 (and compatible .NET versions for Windows)

    Build Behavior

    • C# and Visual Basic projects: The package is configured to embed the interop types directly into your assembly. This prevents the Microsoft.Office.Interop.Access.Dao.dll from being copied to your output directory, keeping your deployment cleaner.
    • F# projects: Note that F# projects are excluded from this automatic embedding behavior; the assembly will not be automatically embedded in the same way as C# or VB.
  3. Choose between ExcelDna.Integration and ExcelDna.AddIn

    master

    Decide which package to use based on your project type:

    • Use ExcelDna.AddIn if you are creating a complete .xll add-in. This package includes ExcelDna.Integration and provides the necessary build targets, loaders, and packing support required to generate the add-in.
    • Use ExcelDna.Integration if you only need to compile against the Excel-DNA API, such as when developing a shared library that contains logic or attributes intended to be used by an Excel-DNA add-in.
  4. Migrate from Excel-DNA.Lib to current packages

    master

    The Excel-DNA.Lib package is deprecated. For new projects or maintenance, you should migrate to the following packages based on your use case:

    • For new Excel add-ins: Use ExcelDna.AddIn.
    • For helper libraries: If you only need to reference the Excel-DNA API (without the full add-in scaffolding) from a separate library project, use ExcelDna.Integration.

    Existing projects referencing Excel-DNA.Lib will continue to work for compatibility, but migration to the new package names is recommended.

  5. Load Ribbon images in NativeAOT

    master

    Image loading is opt-in. To use embedded resources as ribbon images:

    1. Add loadImage='LoadImage' to the customUI element. LoadImage is a built-in Excel-DNA callback that you do not implement.
    2. Set the image attribute of your control to the name of an embedded manifest resource (typically <RootNamespace>.<FileName>).
    3. Ensure the file is included as an EmbeddedResource in your .csproj.
    <!-- in the .csproj -->
    <ItemGroup>
      <EmbeddedResource Include="app.ico" />
    </ItemGroup>
    <customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui' loadImage='LoadImage'>
      ...
      <button id='b' label='Go' image='MyAddIn.app.ico' onAction='OnGo'/>
      ...
    </customUI>
  6. Migrate from Excel-DNA.Interop to ExcelDna.Interop

    master

    The Excel-DNA.Interop package is deprecated. If you are using this package to provide Microsoft Office primary interop assemblies for your Excel-DNA add-ins, you should migrate to the ExcelDna.Interop package on NuGet.

    To migrate, replace your dependency on Excel-DNA.Interop with ExcelDna.Interop.

  7. Configure IntelliSense for NativeAOT add-ins

    master

    IntelliSense works with NativeAOT add-ins, but it must be deployed as a standalone IntelliSense loader rather than being referenced as a NuGet package in your project.

    Correct Setup

    1. Write metadata using [ExcelFunction(Description=...)] and [ExcelArgument(Description=...)].
    2. Download the appropriate ExcelDna.IntelliSense.xll (32-bit or 64-bit) from the ExcelDna.IntelliSense releases.
    3. Place the .xll next to your NativeAOT add-in and load it in Excel.

    What to Avoid

    Do not add the ExcelDna.IntelliSense NuGet package to your NativeAOT project. Doing so will cause build errors (warning EXCELDNA001) because the package is not AOT-compatible and introduces managed ExcelDna.Integration assemblies that conflict with NativeAOT types like IExcelRibbon.

  8. Enable IntelliSense for .dna files via NuGet (Recommended)

    master

    To enable XML validation and IntelliSense for .dna files and Office Custom UI elements (Ribbon, CTP) within a specific project, install the ExcelDna.XmlSchemas NuGet package.

    After installation, you must add the specific Excel-DNA XML namespace to your .dna files to ensure they are correctly associated with the schema.

    # 1. Install the NuGet package
    install-package ExcelDna.XmlSchemas
    
    # 2. Add the namespace to your .dna file
    <?xml version="1.0" encoding="utf-8"?>
    <DnaLibrary Name="Your Add-In" RuntimeVersion="v4.0" xmlns="http://schemas.excel-dna.net/addin/2020/07/dnalibrary">
      <!-- (...) -->
    </DnaLibrary>