我是C#的新手,创建了excel插件,对ExcelDNA也是新手。我得到了关于http://exceldna.codeplex.com/wikipage?title=Getting%20Started的例子。UDF“multiplethem”按预期工作。
当我修改那个站点上的示例3以从mysql数据库中获取数据时。在我的项目中,我不仅引用了ExcelDna.Integration.dll,还引用了MySql.Data.dll。然后我用以下语句编译它:
c:\windows\microsoft.net\framework\v2.0.50727\csc.exe /target:library /reference:ExcelDna.Integration.dll /reference:MySql.Data.dll TestLib.cs
打开excel加载项并开始键入自定义项(在本例中,“=multipleThem()”)时,没有名为“multipleThem”的自定义项。为什么突然停止工作?这是我的密码:
using ExcelDna.Integration;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
public class MyFunctions
{
[ExcelFunction(Description = "Grabs data from database", Category = "Useful functions")]
public static string MultiplyThem(string[] args)
{
string connString = "Server=localhost;Port=3306;Database=test;Uid=root;password=pword";
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
command.CommandText = "SELECT field_value FROM customers";
try
{
conn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
string myvariable = "bad";
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
myvariable = reader["field_value"].ToString();
}
return myvariable;
}
}
以及我的Test1.dna文件(我的项目中的目标是.NET Framework 4):
<DnaLibrary RuntimeVersion="v4.0">
<ExternalLibrary Path="TestLib.dll"/>
</DnaLibrary>
最佳答案
Excel DNA当前不支持将字符串数组作为参数。如果将字符串[]args更改为对象[]args,则应该可以。