You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8创建可被Excel调用的COM类:注册与调用问题排查

无法在Excel中访问.NET 8 COM类的问题

我需要创建一个可被Excel访问的.NET 8 COM类,参考相关视频后实现了如下测试类:

namespace COMTestBedCS
{
    [Guid("26a0aa6d-5aba-458f-92b4-b9a30ae0c65c")]
    [GeneratedComInterface]
    public partial interface ITestBed
    {
        int GetXPTO();
        void SetXPTO(int value);
    }

    [Guid("3e178f98-522e-4e95-8a9c-6d80dc48b7d5")]
    [GeneratedComClass]
    public partial class TestBed : ITestBed
    {
        private int _XPTO = 1024;

        public int GetXPTO() => _XPTO;
        public void SetXPTO(int value)=>_XPTO = value;
    }
}

项目编译正常无错误,但尝试在Excel中引用该测试类时,出现错误:Can't add a reference to the specified file.

使用regsvr32注册时也报错:
regsvr32错误消息

我哪里操作有误?


补充项目文件内容:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>

</Project>

更新

我已成功编译代码并通过regsvr32注册,但仍无法在Excel中引用该类。在引用列表中找不到它,浏览DLL(程序集或comhost)时仍提示:Can't add a reference to the specified file.

目前仅需解决在Excel VBA中添加引用的问题。

新代码

项目文件

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <RootNamespace>COMTestBedCS</RootNamespace>
    <TargetFramework>net8.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <AllowUnsafeBlocks>False</AllowUnsafeBlocks>
    <EnableComHosting>true</EnableComHosting>
    <RegisterForComInterop>True</RegisterForComInterop>
    <Platforms>x86</Platforms>
    <RegisterAssemblyMSBuildArchitecture>x86</RegisterAssemblyMSBuildArchitecture>
  </PropertyGroup>

</Project>

C#代码

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace COMTestBedCS
{
    [Guid("26a0aa6d-5aba-458f-92b4-b9a30ae0c65c")]
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface ITestBed
    {
        int GetXPTO();
        void SetXPTO(int value);
    }

    [Guid("3e178f98-522e-4e95-8a9c-6d80dc48b7d5")]
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    internal class TestBed : ITestBed
    {
        private int _XPTO = 1024;

        public int GetXPTO() => _XPTO;
        public void SetXPTO(int value)=>_XPTO = value;
    }
}

注意:编译时需构建两次,首次构建Visual Studio会报错:

MSB3217 
Cannot register assembly "<assembly file>" Could not load file or 
assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. 
The system cannot find the file specified.

不清理项目直接重新构建才可成功编译。


更新2

我已将测试项目上传至代码仓库。


更新3

按照建议使用dscom创建并注册TypeLib后,可在Excel中添加引用,但执行如下VBA测试代码时出现错误:Class not registered

Sub Test()

    Dim xpto As New COMTestBedCS.TestBed
    Debug.Print xpto.GetXPTO()
    
End Sub

内容的提问来源于stack exchange,提问作者Paulo Santos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 08:10:02