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

来源:互联网 发布:以下淘宝禁止出售商品 编辑:程序博客网 时间:2024/06/07 04:56

最近刚做了个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;    }

以上作为记录.

原创粉丝点击