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

如何在C# ExcelDNA XLL项目中引用固定绝对路径的第三方DLL

解决方案:ExcelDNA XLL引用固定路径第三方DLL

1. 修改项目引用配置(csproj)

先调整项目引用的配置,确保编译时能找到DLL,同时禁止将第三方DLL复制到本地输出目录:

<ItemGroup>
  <Reference Include="SIMExporterEbmlImport">
    <!-- 替换为机器上的固定绝对路径 -->
    <HintPath>C:\ProgramData\EB Bridging Tools\SimExporter\SIMExporterEbmlImport.dll</HintPath>
    <!-- 禁止复制DLL到项目输出文件夹 -->
    <Private>False</Private>
  </Reference>
  <Reference Include="System.Windows.Forms" />
</ItemGroup>

说明:<Private>False</Private>等价于Visual Studio里的「复制本地」设为False,避免把第三方DLL打包到XLL输出目录。

2. 运行时程序集解析(适配Excel进程)

ExcelDNA的XLL运行在Excel的进程域中,默认只会在XLL所在目录、系统路径等位置查找DLL。需要添加程序集解析逻辑,引导系统去固定路径加载目标DLL:

using System;
using System.Reflection;
using System.IO;
using ExcelDna.Integration;

public class AddIn : IExcelAddIn
{
    public void AutoOpen()
    {
        // 订阅程序集解析事件,当系统找不到DLL时触发
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
    }

    public void AutoClose()
    {
        // 取消订阅,避免内存泄漏
        AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
    }

    private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        // 匹配目标程序集名称
        if (args.Name.StartsWith("SIMExporterEbmlImport,"))
        {
            // 使用系统特殊文件夹获取路径,避免硬编码兼容性问题
            string dllPath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramData),
                "EB Bridging Tools", 
                "SimExporter", 
                "SIMExporterEbmlImport.dll"
            );
            
            try
            {
                // 从固定路径加载程序集
                return Assembly.LoadFrom(dllPath);
            }
            catch (FileNotFoundException)
            {
                // 给Excel返回错误提示
                ExcelIntegration.ReturnError("找不到目标DLL,请检查安装状态", ExcelError.ExcelErrorValue);
                return null;
            }
            catch (Exception ex)
            {
                ExcelIntegration.ReturnError($"加载DLL失败: {ex.Message}", ExcelError.ExcelErrorValue);
                return null;
            }
        }
        // 非目标程序集,返回null让系统继续处理
        return null;
    }
}

3. 业务代码中的错误处理

在实际调用第三方类的地方,添加额外异常捕获,确保用户能直观看到错误:

using SIMExporterEbmlImport;
using ExcelDna.Integration;

public static class ExcelFunctions
{
    [ExcelFunction(Description = "测试第三方DLL调用")]
    public static object TestThirdPartyCall()
    {
        try
        {
            MyClass obj = new MyClass();
            // 执行第三方类的方法逻辑
            return "DLL调用成功";
        }
        catch (FileNotFoundException)
        {
            return ExcelError.ExcelErrorNA; // 返回Excel标准#N/A错误
        }
        catch (Exception ex)
        {
            return $"调用失败: {ex.Message}";
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 11:31:03