简体   繁体   中英

How to use ExcelDNA developed XLL from C# without Excel

we have built XLL in C# using ExcelDna as described in https://excel-dna.net/ --- everything works when tested from Excel.

We would like to be able to use this XLL in other .NET projects -- in other words, how do we "host" this XLL from .NET? The XLL does not appear to be a valid .NET assembly -- so I cannot add it as a .NET reference to other projects

Is it done through ExcelDna.Integration dll somehow (which IS a .net assembly)?

Thank you

Excel-DNA is just a glue between Excel and .NET assemblies.

If you want to reuse functionality that is inside a .NET assembly that is being loaded by an Excel-DNA Add-In (ie XLL ), then you don't need the XLL for anything... All you need is the .NET assembly that the XLL is loading - Add a reference to that in your .NET application and you're good to go... Just like you would do with any other class library.

In other words, inside your .dna file that is used by Excel-DNA to determine what to load, you have something that looks like this:

<DnaLibrary Name="Your Add-In" RuntimeVersion="v4.0">
  <ExternalLibrary Path="YourAssembly.dll" ExplicitExports="false" ... />
</DnaLibrary>

YourAssembly.dll is already the .NET assembly that contains the functions that are exposed to Excel-DNA.


More importantly, if you know you're going to have different .NET "clients" for your functions, then you should design for that, isolate the reusable functions in a dedicated assembly, and have your Excel-DNA assembly reference that, and expose the functions to Excel, instead of forcing clients to depend on Excel-DNA assemblies.

eg

  • C# Class Library Functions.dll (does not reference anything)
  • C# Class Library ExcelAddIn.dll (references Functions.dll )
  • C# App MyCsApp.exe (references Functions.dll )
  • C# Class Library FunctionsComInterop.dll (references Functions.dll )
  • C++ App MyCppApp.exe (calls FunctionsComInterop.dll )
  • etc.

图

In the example above:

  • Functions.dll does not reference any Excel-DNA assembly and it does not expose anything via to COM. It should have as little dependencies as possible (ideally none!)

  • ExcelAddIn.dll makes the bridge to Excel-DNA and exposes Excel functions that can be called by Excel through Excel-DNA. These functions simply forward calls to the Functions.dll assembly... Nothing more.

  • FunctionsComInterop.dll makes the bridge to C++ and exposes COM functions that can be called by the C++ app. These functions simply forward calls to the Functions.dll assembly... Nothing more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM