我在使用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)";
}
}
看來我不瞭解的是如何設置我的添加以使這些方法正確調用。
沒有調整大小調用它工作嗎?如果您使用最新發行版中的Resize,您是否在AutoOpen處理程序中有AsyncUtil.Initialize()調用? – Govert 2013-03-20 18:28:41
好吧,我沒有意識到我需要這個,我會嘗試一下,看看是否可行 – 2013-03-20 18:48:55
發生了什麼事情是我沒有ArrayResizer類可用於我,所以我的通話失敗了。通過包含ArrayResizer類,我能夠使其工作,現在唯一要做的就是弄清楚如何使用源代碼中的dll實現它,而不是將他的代碼複製到我的代碼中。 – 2013-03-20 19:20:56