Search code examples
c#.net-4.0excel-dna

Display array Data Excel Dna


I am having trouble displaying data using Excel-Dna with C#. I have a function which takes in data and processes it to make a table, so I wrote a test function just to display the data, and I am unable to get a value out. The error is #VALUE.

public class Functions : IExcelAddIn
{
    public static String Username { get; set; }
    public static String Password { get; set; }
    public static Random rnd = new Random();

    public void AutoOpen()
    {
        ExcelAsyncUtil.Initialize();
    }

    public void AutoClose()
    {
        ExcelAsyncUtil.Uninitialize();
    }

    [ExcelFunction(Description="My first Excel-DNA function")]
    public static string MyFirstFunction(string name)
    {
        return "Hello, " + name + ".";
    }

    public static string ShowCurrentUser()
    {
        return (String.IsNullOrWhiteSpace(Username)) ? "Noone is logged in." : Username;
    }
    public static string LogIn(string user, string password)
    {
        const string connectionString = "server=localhost;userid={0};password={1};";
        MySqlConnection connection = new MySqlConnection(String.Format(connectionString, user, password));
        string output = "";
        try
        {
            connection.Open();
            Username = user;
            Password = password;
            output = "Successfully logged in!";
        }
        catch (Exception e)
        {
            output = "Errors: " + e.ToString();
        }
        finally
        {
            connection.Close();
        }

        return output;
    }
    public static object QMRTable(int SynNum, int YoA, int qtr, int TabNum)
    {
        object[,] response = new object[16, 3];

        for (int r = 0; r < response.GetLength(0); r++)
            for (int c = 0; c < response.GetLength(1); c++)
                response[r, c] = String.Format("Synd: {0}, YoA: {1}, Qtr: {2}, ({3},{4})", SynNum, YoA, qtr, r, c);

        return XlCall.Excel(XlCall.xlUDF, "Resize", response);
        //return response;
    }
    public static object QMRItem(int SynNum, int YoA, string qtr, string item)
    {
        return (rnd.NextDouble() * (100.0 - 0.0) + 0.0) + " GBP (M)";
    }
}

It seems what I am not understanding is how to set up my add in to have these methods be called correctly.


Solution

  • So, the answer is to include the AsyncFunctions.dll as well as include the ExcelAsyncUtil.Initialize(). You need to change your .dna file to look like this:

    <DnaLibrary Name="MyExcel Add-In" RuntimeVersion="v4.0">
        <ExternalLibrary Path="MyExcelLibrary.dll" />
        <ExternalLibrary Path="AsyncFunctions.dll" />
    </DnaLibrary>
    

    To compile the library for external use, you will have to go into his Distribution folder and find the Async folder [Excel-Dna\Distribution\Samples\Async\AsyncFunctions] and open this solution and build the library. You may need to get the Reactive Extensions Library. You can get it via NuGet with the command, Install-Pakcage Rx-Main.