VS2010开发Excel2007插件以及制作打包安装程序

最近刚做了个Excel2007的插件玩玩,顺便记录一下自己的开发过程;

刚开始新建一个"Excel2007外接程序",这样开发结果是制作了一个office加载项,通过Excel自己的功能加载项中可以将开发的*.dll加载到菜单中,但是有个问题是制作安装包的时候,无法加载成功,可能是权限的问题,结果自己也没有找到解决方法.

后来用了另外一种方法,同样是在vs2010上面有个"其他项目类型"->"扩展性"->"共享的外接程序":

这个会自动创建一个插件的注册项目和打包安装项目,借鉴微软的相关文档:http://support.microsoft.com/default.aspx?scid=kb;en-us;Q302901.

根据上面文档参考,打包的安装程序可以直接注册到系统中,至于注册表方面不需要自己去考虑了,系统给你做好.

主要注册代码如下:

    /// <summary>
    ///   The object for implementing an Add-in.
    /// </summary>
    /// <seealso class='IDTExtensibility2' />
    [GuidAttribute("E6DACA4D-DE5F-4016-862A-696DA731F77C"), ProgId("DateExcelAddIn.Connect")]
    public class Connect : Object, Extensibility.IDTExtensibility2
    {
        /// <summary>
        /// 定义一个功能按钮,时间插件
        /// </summary>
        private Office.CommandBarButton dateButton;
        /// <summary>
        ///		Implements the constructor for the Add-in object.
        ///		Place your initialization code within this method.
        /// </summary>
        public Connect()
        {
        }

        /// <summary>
        ///      Implements the OnConnection method of the IDTExtensibility2 interface.
        ///      Receives notification that the Add-in is being loaded.
        /// </summary>
        /// <param term='application'>
        ///      Root object of the host application.
        /// </param>
        /// <param term='connectMode'>
        ///      Describes how the Add-in is being loaded.
        /// </param>
        /// <param term='addInInst'>
        ///      Object representing this Add-in.
        /// </param>
        /// <seealso class='IDTExtensibility2' />
        public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
        {
            applicationObject = application;
            addInInstance = addInInst;
            if (connectMode != ext_ConnectMode.ext_cm_Startup)
            {
                OnStartupComplete(ref custom);
            }
        }

        /// <summary>
        ///     Implements the OnDisconnection method of the IDTExtensibility2 interface.
        ///     Receives notification that the Add-in is being unloaded.
        /// </summary>
        /// <param term='disconnectMode'>
        ///      Describes how the Add-in is being unloaded.
        /// </param>
        /// <param term='custom'>
        ///      Array of parameters that are host application specific.
        /// </param>
        /// <seealso class='IDTExtensibility2' />
        public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
        {
            if (disconnectMode != ext_DisconnectMode.ext_dm_HostShutdown)
            {
                OnBeginShutdown(ref custom);
            }
            applicationObject = null;
        }

        /// <summary>
        ///      Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.
        ///      Receives notification that the collection of Add-ins has changed.
        /// </summary>
        /// <param term='custom'>
        ///      Array of parameters that are host application specific.
        /// </param>
        /// <seealso class='IDTExtensibility2' />
        public void OnAddInsUpdate(ref System.Array custom)
        {
        }

        /// <summary>
        ///      Implements the OnStartupComplete method of the IDTExtensibility2 interface.
        ///      Receives notification that the host application has completed loading.
        /// </summary>
        /// <param term='custom'>
        ///      Array of parameters that are host application specific.
        /// </param>
        /// <seealso class='IDTExtensibility2' />
        public void OnStartupComplete(ref System.Array custom)
        {
            ///定义菜单集合
            Office.CommandBars oCommandBars;
            Office.CommandBar oStandardBar;

            try
            {
                oCommandBars = applicationObject.GetType().InvokeMember("CommandBars", System.Reflection.BindingFlags.GetProperty, null, applicationObject, null) as Office.CommandBars;
            }
            catch (Exception)
            {
                object oActiveExplorer;
                oActiveExplorer = applicationObject.GetType().InvokeMember("ActiveExplorer", BindingFlags.GetProperty, null, applicationObject, null);
                oCommandBars = oActiveExplorer.GetType().InvokeMember("CommandBars", BindingFlags.GetProperty, null, oActiveExplorer, null) as Office.CommandBars;
            }

            try
            {
                oStandardBar = oCommandBars["Standard"];
            }
            catch (Exception)
            {
                oStandardBar = oCommandBars["Database"];
            }

            try
            {
                dateButton = (Office.CommandBarButton)oStandardBar.Controls["CaculateDate"];
            }
            catch (Exception)
            {
                object omissing = Missing.Value;

                dateButton = (Office.CommandBarButton)oStandardBar.Controls.Add(1, omissing, omissing, omissing, omissing);
                dateButton.Caption = "计算时间";
                dateButton.Style = Office.MsoButtonStyle.msoButtonCaption;
            }
            dateButton.Tag = "CaculateDate";
            dateButton.OnAction = "!<DateExcelAddIn.Connect>";

            dateButton.Visible = true;
            dateButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(dateButton_Click);
            oStandardBar = null;
            oCommandBars = null;
        }

        /// <summary>
        ///      Implements the OnBeginShutdown method of the IDTExtensibility2 interface.
        ///      Receives notification that the host application is being unloaded.
        /// </summary>
        /// <param term='custom'>
        ///      Array of parameters that are host application specific.
        /// </param>
        /// <seealso class='IDTExtensibility2' />
        public void OnBeginShutdown(ref System.Array custom)
        {
            object omissing = System.Reflection.Missing.Value;
            dateButton.Delete(omissing);
            dateButton = null;
        }

        void dateButton_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            //点击之后可以选择两列,然后,计算结果显示;
            using (RibbonConfig configForm = new RibbonConfig())
            {
                if (DialogResult.OK == configForm.ShowDialog())
                {
                    //计算数值;
                    DateExcel.GetSelectedCells((Excel.Application)applicationObject);
                }
            }
        }

        private object applicationObject;
        private object addInInstance;

    }

以上作为记录.

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值