创建后期绑定的comserver

有自动提示的参考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

猜你喜欢

转载自blog.csdn.net/qq_24499417/article/details/105087738
今日推荐