0

我正在测试Excel-Dna并想要在 Excel 中运行一些简单代码的按钮。据我了解,以下代码应在 Excel 的“加载项”选项卡中添加一个按钮:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExcelDna.Integration.CustomUI;
using NetOffice;
using NetOffice.ExcelApi;
using ExcelDna.Integration;
using ExcelPluginTest;

namespace ExcelPluginTest
{
    public class AddIn : IExcelAddIn
    {
        public static Application Excel { get; set; }        
        public void AutoOpen()
        {
            Factory.Initialize();
            Excel = new Application(null, ExcelDnaUtil.Application);

        }

        public void AutoClose()
        {           
        }
    }

    public class ExcelHelper
    {
        public static Application Excel { get { return AddIn.Excel; } }

        [ExcelCommand(MenuName = "ExcelPlugin Test", MenuText = "Write the Excel version")]
        public void WriteTheVersion()
        {
            var ver = Excel.Version;
            var rng = Excel.Range("B3");
            rng.Value = ver;
        }
    }
}

但是,即使在 File | 中选中了 Add-Ins 选项卡,它甚至也不可见。选项 | 自定义功能区。我不确定是否会发生这种情况,因为没有什么可以显示(我的代码中也有问题)或者按钮实际上存在但我看不到它,因为加载项选项卡被隐藏(我确实确认加载项已加载(在 AutoOpen 中设置 BP))。

4

1 回答 1

1

为了让 Excel-DNA 将您的方法注册为 Excel 中的宏,它必须是“静态的”。所以这应该工作:

    [ExcelCommand(MenuName = "ExcelPlugin Test", MenuText = "Write the Excel version")]
    public static void WriteTheVersion()
    {
    }

请注意,有一个 Excel 错误会影响[ExcelCommand...]Excel 2013 下的样式菜单 - 这已在即将发布的 Excel-DNA 版本 0.32 中修复。

于 2014-02-28T13:18:47.420 回答