已打包 Excel DNA .xll 文件未加载功能区

Packed Excel DNA .xll file does not load Ribbon

我尝试部署使用 Excel-DNA 工具构建的 Excel 插件。

当 运行 来自 Visual Studio 时,加载项完美运行,但是,当我尝试从其他地方打开打包的 .xll 文件时,正如 Govert 所建议的那样 (Excel DNA 的创造者),色带中的插件无法加载。激活插件错误消息后,我收到非显式消息:对 GetCustomUI() 的调用失败。就是这样。

所以我有两个问题:

一样,我用 try/catch 块包围了 GetCustomUI() 方法重写并将异常 记录到文本文件中。这让我可以访问插件启动时引发的异常。

而且,最重要的是,问题是我有一个额外的 JSON 配置文件,打包的 XLL 没有考虑到它,似乎没有直接的方法可以通过 DNA 文件包含它。

解释了变通方法here:将您的外部文件设置为嵌入式资源并从清单资源流中读取它.

在我的特殊情况下,我将它用于 DI 服务提供程序,并按如下方式构建它:

private IServiceProvider BuildServiceProvider()
    {
        var serviceCollection = new ServiceCollection();

        //Configuration
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.SetBasePath(Directory.GetCurrentDirectory());

        var assembly = Assembly.GetExecutingAssembly();
        var resourceName = "otherconfig.json";
        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        using (StreamReader reader = new StreamReader(stream)) {
            string result = reader.ReadToEnd();
            string tempPath = Path.GetTempFileName();
            File.WriteAllText(tempPath, result);
            builder.AddJsonFile(tempPath);
        }

        IConfiguration config = builder.Build();
        serviceCollection.AddSingleton(config);

        //other dependency injection service registration

        return serviceCollection.BuildServiceProvider();
    }

如果您要覆盖方法 GetCustomUI,请在 GetCustomUI 上放置一个 try...catch,然后查看异常详细信息。

[ComVisible(true)]
public class RibbonController : ExcelRibbon
{
    public override string GetCustomUI(string RibbonID)
    {
        try
        {
             // ...
        }
        catch(Exception ex)
        {
             MessageBox.Show(ex.ToString());
        }
    }

    // ...
}