有自動提示的參考https://blog.csdn.net/qq_24499417/article/details/105079648。
後期綁定簡單不少。主要設置com的classinterface爲autodispatch.也就是接口的後期綁定。另外需要加上progid,基本步驟與上一篇一樣的。後期綁定沒有自動提示,就不需要用到dll文件和導出類型庫。需要在配置文件裏添加ComServer="true",直接使用生成的xll文件。
using ExcelDna.ComInterop;
using ExcelDna.Integration;
using System.Runtime.InteropServices;
//
namespace ExcelInterface
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[ProgId("CSharp_functionLibrary")]
public class InterfaceFunctions
{
public double add(double x, double y)
{
return x + y;
}
}
//
[ComVisible(false)]
class ExcelAddin : IExcelAddIn
{
public void AutoOpen()
{
ComServer.DllRegisterServer();
}
public void AutoClose()
{
ComServer.DllUnregisterServer();
}
}
}
我的64位excel直接在開發工具excel加載項里加載64位的xll.32位的excel就用32位的xll
測試代碼:
Sub crea()
Dim lib As Object
Set lib = CreateObject("CSharp_functionLibrary")
Debug.Print lib.Add(12, 13)
Set lib = Nothing
End Sub