我是 C# 新手,创建了 excel 插件,也是 ExcelDNA 的新手。我得到了在http://exceldna.codeplex.com/wikipage?title=Getting%20Started上工作的示例。UDF“MultiplyThem”按预期工作。
当我修改该站点上的示例 #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-add in 并开始输入我的 UDF(在本例中为“=MultiplyThem()”)时,没有名为“MultiplyThem”的 UDF。为什么它突然停止工作?这是我的 C# 代码:
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>