/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("6D3788F4-9529-429E-BA5D-09695F85687A"), ProgId("SimpleExcelServicesDemo.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
private Microsoft.Office.Interop.Excel.Application app;
private Microsoft.Office.Core.COMAddIn addIn;
3、在OnConnection事件里初始化:
代码
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
this.app = application as Microsoft.Office.Interop.Excel.Application;
this.addIn = addInInst as Microsoft.Office.Core.COMAddIn;
}
4、在OnStartupComplete事件中设置一个按钮,关联事件处理逻辑:
代码
public void OnStartupComplete(ref System.Array custom)
{
CommandBars commandBars;
CommandBar standardBar下载数据;
CommandBarButton simpleButton下载数据;
commandBars = this.app.CommandBars;
// Get the standard CommandBar from Word
standardBar下载数据 = commandBars["Standard"];
try
{
// try to reuse the button is hasn't already been deleted
simpleButton下载数据 = (CommandBarButton)standardBar下载数据.Controls["下载数据"];
}
catch (System.Exception)
{
// If it's not there add a new button
simpleButton下载数据 = (CommandBarButton)standardBar下载数据.Controls.Add(1);
simpleButton下载数据.Caption = "下载数据";
simpleButton下载数据.Style = MsoButtonStyle.msoButtonCaption;
}
// Make sure the button is visible
simpleButton下载数据.Visible = true;
simpleButton下载数据.Click += new _CommandBarButtonEvents_ClickEventHandler(btnDownload_Click);
standardBar下载数据 = null;
commandBars = null;
}