0

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

似乎我不明白的是如何设置我的插件以正确调用这些方法。

4

1 回答 1

1

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

<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.

于 2013-03-21T12:58:37.183 回答