XLL+ Class Library (7.0)

Importing .NET assemblies

XLL+ (6.2 and above) includes a set of tools to import a .NET assembly into an add-in. Point the tools at your assembly, and they will generate wrapper functions for you, with little or no assistance from you. Any public method or property of a .NET class can be exported as an Excel add-in function or macro.

Transformations

Suppose you have this class definition in C#:

CopyC#
public class MyClass
{
    public static string FormatDate(DateTime dt, string format)
    {
        return dt.Date.ToString(format);
    }
    public static string MostPopularMonth(DateTime[] dts)
    {
        // Omitted for brevity
    }
}

The XLL+ Reflector tools will generate 2 add-in functions as follows:

=FormatDate(dt, format)
=MostPopularMonth(dts)

Each of these functions will contain code to check the type of the inputs, convert them to .NET types, call the .NET method, handle any errors, and convert the results to Excel data types.

You can also specify a prefix, so that the add-in functions become, for instance:

=MyClass.FormatDate(dt, format)
=MyClass.MostPopularMonth(dts)

Comments

If you have added documentation comments to your C# code, they will be carried across to the Excel add-in function:

CopyC#
public class MyClass
{
    /// <summary> 
    /// Converts a date to text, using a .NET formatting string 
    /// </summary> 
    /// <param name="dt">is a date/time value</param> 
    /// <param name="format">is a .NET formatting string</param> 
    public static string FormatDate(DateTime dt, string format)
    {
        return dt.Date.ToString(format);
    }
}

Objects

You can return objects from your .NET methods, and they will be represented in Excel as handles. You can then pass the handle to any method of the object that is exported as an add-in function.

For example, the two classes below include the factory method Factory.CreateThing(), which returns an object of type Thing, and the instance method Thing.GetInversion(), which returns some calculated information from a Thing object.

CopyC#
public static class Factory
{
    public static Thing CreateThing(string key, int value)
    {
        return new Thing(key, value);
    }
}
public class Thing : IDisposable
{
    public int GetInversion(int order, bool reversed)
    {
        // Omitted for brevity
    }
}

These could be exported as the following Excel add-in functions:

=Factory.CreateThing(key, value)
=Thing.GetInversion(thing, order, reversed)

Note that the first add-in function returns a handle value, and that the second function takes an extra first argument, which must be a handle to a Thing (such as that returned by Factory.CreateThing).

Next: Preparing to import >>