显示数组Data Excel Dna

时间:2013-03-20 17:25:26

标签: c# .net-4.0 excel-dna

我在使用Excel-Dna和C#显示数据时遇到问题。我有一个函数,它接收数据并处理它来制作一个表,所以我写了一个测试函数只是为了显示数据,我无法得到一个值。错误是#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)";
    }
}

似乎我不理解的是如何设置我的添加以正确调用这些方法。

1 个答案:

答案 0 :(得分:1)

因此,答案是包含AsyncFunctions.dll以及包含ExcelAsyncUtil.Initialize()。您需要将.dna文件更改为如下所示:

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

要编译库供外部使用,您必须进入他的Distribution文件夹并找到Async文件夹[Excel-Dna \ Distribution \ Samples \ Async \ AsyncFunctions]并打开此解决方案并构建库。您可能需要获取Reactive Extensions Library。您可以使用命令Install-Pakcage Rx-Main从NuGet获取它。