C# MS Excel 2013的自动化加载项
我正试图在VisualStudio2013中编写一个C#automation插件。目标是能够从MS Excel 2013中调用以C#编写的UDF。我已经阅读了关于这个主题的大部分公开材料,并尝试改编了几个简单的例子 不幸的是,它们都不是根据VS 2013和MSExcel 2013编写的。代码示例:C# MS Excel 2013的自动化加载项,c#,excel,C#,Excel,我正试图在VisualStudio2013中编写一个C#automation插件。目标是能够从MS Excel 2013中调用以C#编写的UDF。我已经阅读了关于这个主题的大部分公开材料,并尝试改编了几个简单的例子 不幸的是,它们都不是根据VS 2013和MSExcel 2013编写的。代码示例: using System; using System.Net; using System.Runtime.InteropServices; using System.Globalization; us
using System;
using System.Net;
using System.Runtime.InteropServices;
using System.Globalization;
using Microsoft.Win32;
namespace MyFirstAddIn
{
// Early binding. Doesn't need AutoDual.
[Guid("5E6CD676-553F-481E-9104-4701C4DAB272")]
[ComVisible(true)]
public interface IFinancialFunctions
{
double Bid(string symbol);
double Ask(string symbol);
double[,] BidnAsk(string symbol, string direction = null);
}
[Guid("B9B7A498-6F84-43EB-A50C-6D26B72895DA")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class FinancialFunctions : IFinancialFunctions
{
// Private class members.
private static readonly WebClient webClient = new WebClient();
private const string UrlTemplate = "http://finance.yahoo.com/d/quotes.csv?s={0}&f={1}";
// Private method - data download.
private static double GetDoubleDataFromYahoo(string symbol, string field)
{
string request = string.Format(UrlTemplate, symbol, field);
string rawData = webClient.DownloadString(request);
return double.Parse(rawData.Trim(), CultureInfo.InvariantCulture);
}
// Public "interface" methods.
public double Bid(string symbol)
{
return GetDoubleDataFromYahoo(symbol, "b3");
}
public double Ask(string symbol)
{
return GetDoubleDataFromYahoo(symbol, "b2");
}
public double[,] BidnAsk(string symbol, string direction = null)
{
double bid = GetDoubleDataFromYahoo(symbol, "b3");
double ask = GetDoubleDataFromYahoo(symbol, "b2");
return direction == "v" ? new[,]{{bid}, {ask}} : new[,]{{bid, ask}};
}
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type type)
{
Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type, "Programmable"));
RegistryKey key = Registry.ClassesRoot.OpenSubKey(GetSubKeyName(type, "InprocServer32"), true);
key.SetValue("",System.Environment.SystemDirectory + @"\mscoree.dll",RegistryValueKind.String);
}
[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type type)
{
Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type, "Programmable"), false);
}
private static string GetSubKeyName(Type type, string subKeyName)
{
System.Text.StringBuilder s = new System.Text.StringBuilder();
s.Append(@"CLSID\{");
s.Append(type.GUID.ToString().ToUpper());
s.Append(@"}\");
s.Append(subKeyName);
return s.ToString();
}
}
}
我已通过以下方式使组件COM可见:
项目->属性->应用程序->程序集信息
我还在“构建”选项卡中注册了COM互操作
构建之后,我可以在注册表中看到注册成功,并且外接程序以正确的GUID注册。但是,当我打开Excel并转到“开发人员->加载项->自动化”时,我无法在列表中看到我的加载项。我验证了我发布的代码是否适用于Excel 2010,但由于某些原因,我无法在Excel 2013中看到我的加载项
有人能帮我解决这个问题吗?好的,我已经找到了问题的原因。令人惊讶的是,VisualStudio没有在注册表的正确树下自动注册64位DLL的能力。解决方案不是为COM互操作注册项目,而是手动添加命令以作为生成后事件调用64位版本的RegAsm。需要包括dll的完整路径和名称,因此不幸的是,这不是一个完全自动化的解决方案