2015-11-02 49 views
2

我想在Visual Studio 2013中編寫一個C#自動化加載項。目標是能夠從MS Excel 2013中調用用C#編寫的UDF。我已閱讀了大部分關於這個問題的公開材料,並試圖修改幾個簡單的例子,such asMS Excel 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可見經由:

不幸的是,兩者都沒有VS 2013和2013年msexcel的代碼示例下寫入

項目 - >屬性 - >應用程序 - >組裝信息

而且我還在「構建」選項卡中註冊了COM互操作。

構建完成後,我可以在註冊表中看到註冊成功,並且加載項在正確的GUID下注冊。但是,當我打開Excel並轉到開發人員 - >加載項 - >自動化時,我看不到列表中的加載項。我驗證了我發佈的代碼適用於Excel 2010,但出於某種原因,我無法在Excel 2013中看到我的加載項。

任何人都可以幫助我解決這個問題嗎?

回答

0

好的,我找到了問題的原因。令人驚訝的是,事實證明,Visual Studio不具備在註冊表中的正確樹下自動註冊64位dll的功能。解決方案不是爲COM interop註冊項目,而是手動添加命令來調用RegAsm的64位版本作爲生成後事件。 dll的完整路徑和名稱需要包含,所以不幸的是,這不是一個完全自動化的解決方案。

相關問題