MS Excel 2013的自动化加载项

我正在尝试在Visual Studio 2013中编写一个C#自动化加载项。目标是能够从MS Excel 2013中调用用C#编写的UDF。我已经阅读了大部分有关该主题的公开可用材料,并试图适应几个简单的例子, 比如 。

不幸的是,它们都不是在VS 2013和MSExcel 2013下编写的。代码示例:

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可见:

Project-> Properties-> Application-> Assembly Information

我也在“生成”选项卡中注册了COM互操作。

构build完成后,我可以在registry中看到注册成功,并且加载项在正确的GUID下注册。 但是,当我打开Excel并转到开发人员 – >加载项 – >自动化,我看不到我的加载项在列表中。 我validation了我发布的代码与Excel 2010一起工作,但由于某些原因,我无法在Excel 2013中看到我的加载项。

谁能帮我这个?

好的,我find了问题的原因。 令人惊讶的是,事实certificate,Visual Studio不具备在registry中的正确树下自动注册64位DLL的能力。 解决scheme不是为COM interop注册项目,而是手动添加命令来调用RegAsm的64位版本作为生成后事件。 dll的完整path和名称需要包含,所以不幸的是这不是一个完全自动化的解决scheme。