2013-03-20 52 views
0

我在使用C#使用Excel-Dna显示数据时遇到了问题。我有一个函数,它接收数据并对其进行处理以创建表格,所以我编写了一个测试函数来显示数据,但我无法获得一个值。错误是#VALUE。显示数组数据Excel Dna

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)"; 
    } 
} 

看来我不了解的是如何设置我的添加以使这些方法正确调用。

+0

没有调整大小调用它工作吗?如果您使用最新发行版中的Resize,您是否在AutoOpen处理程序中有AsyncUtil.Initialize()调用? – Govert 2013-03-20 18:28:41

+0

好吧,我没有意识到我需要这个,我会尝试一下,看看是否可行 – 2013-03-20 18:48:55

+0

发生了什么事情是我没有ArrayResizer类可用于我,所以我的通话失败了。通过包含ArrayResizer类,我能够使其工作,现在唯一要做的就是弄清楚如何使用源代码中的dll实现它,而不是将他的代码复制到我的代码中。 – 2013-03-20 19:20:56

回答

1

所以,答案是包括AsyncFunctions.dll以及ExcelAsyncUtil.Initialize()。你需要改变你的。DNA文件看起来像这样:

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

要编译外用库,你将不得不进入他的分发文件夹,找到文件夹异步[EXCEL-DNA \分布\样本\ Async \ AsyncFunctions]并打开此解决方案并构建库。您可能需要获取Reactive Extensions Library。您可以使用命令Install-Pakcage Rx-Main通过NuGet获取它。

+0

除了包含整个AsyncFunction示例外,您还可以将ArrayResizer.cs代码放入自己的库中。 – Govert 2013-03-21 18:00:21

+0

我编译了这个库,而不是为了开发的目的而添加了代码以保持创建者的代码不同于我的代码,并且一定要归功于原作者。 – 2013-03-21 22:41:22