C#开发excel插件 用外接程序方法?

C#开发excel插件 用外接程序方法?,第1张

推荐你下载《VSTO开发指南》学习一下,VSTO是一种新的Office插件开发方式,允许程序员使用伍羡笑C#进行插件开发。如果你安装的是office2007,那么添加菜单这种东西很简单,可以直接通过一个XML文件来完成。如果安装的office2003,那么可以通过编程来实现。本人用过这个东东,非常不错。派肢如果有什腔含么不懂的,后面可以通过百度联系我~~

public partial class ThisAddIn

{

// the Outlook Inspectors collection

Outlook.Inspectors _Inspectors

// the Outlook Explorers collection

Outlook.Explorers _Explorers

// a collection of wrapped objects

Dictionary<guid,WrappedObject>_WrappedObjects

/// <summary>

/// The entrypoint for the application

/// </summary>

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

_WrappedObjects = new Dictionary<guid,WrappedObject>()

// Inspectors stuff

_Inspectors = this.Application.Inspectors

// Any open Inspectors after startup ?

for (int i = _Inspectors.Counti >= 1i--)

{

// wrap the Inspector

WrapInspector(_Inspectors[i])

}

// get notified for new inspectors

_Inspectors.NewInspector += new

Outlook.InspectorsEvents_NewInspectorEventHandler(_Inspectors_NewInspector)

// Explorer stuff

_Explorers = this.Application.Explorers

// Are there any open Explorers after Startup ?

for (int i = _Explorers.Counti >= 1i--)

{

/升隐历/ Wrap the Explorer and do something useful with it

WrapExplorer(_Explorers[i])

}

// get notified for new application windows

_Explorers.NewExplorer += new

Outlook.ExplorersEvents_NewExplorerEventHandler(_Explorers_NewExplorer)

}

/// <summary>

/// Event sink for the NewExplorer event.

/// </summary>

/// <param name=""""Explorer"吵搜""" />The new Explorer instance</param />

void _Explorers_NewExplorer(Outlook.Explorer Explorer)

{

WrapExplorer(Explorer)

}

/// <summary>

/// The Explorer is "wrapped" and used in the application.

/// </summary>

/// <param name=""""explorer"""" />The new Explorer instance<携碰/param />

void WrapExplorer(Outlook.Explorer explorer)

{

ExplorerWrapper wrappedExplorer = new ExplorerWrapper(explorer)

wrappedExplorer.Closed += new WrapperClosedDelegate(wrappedObject_Closed)

_WrappedObjects[wrappedExplorer.Id] = wrappedExplorer

}

/// <summary>

/// Event sink for the NewInspector event.

/// </summary>

/// <param name=""""Inspector"""" />The new Inspector instance</param />

void _Inspectors_NewInspector(Outlook.Inspector Inspector)

{

WrapInspector(Inspector)

}

/// <summary>

/// The Inspector is "wrapped" and used in the application.

/// </summary>

/// <param name=""""inspector"""" />The new Inspector instance</param />

void WrapInspector(Outlook.Inspector inspector)

{

InspectorWrapper wrappedInspector = new InspectorWrapper(inspector)

wrappedInspector.Closed += new WrapperClosedDelegate(wrappedObject_Closed)

_WrappedObjects[wrappedInspector.Id] = wrappedInspector

}

/// <summary>

/// Event sink for the WrappedInstanceClosed event.

/// </summary>

/// <param name=""""id"""" />The unique ID of the closed object</param />

void wrappedObject_Closed(Guid id)

{

_WrappedObjects.Remove(id)

}

/// <summary>

/// Exitpoint for the application, do the cleanup here.

/// </summary>

/// <param name=""""sender"""" /></param />

/// <param name=""""e"""" /></param />

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

{

_WrappedObjects.Clear()

_Inspectors.NewInspector -= new

Outlook.InspectorsEvents_NewInspectorEventHandler(_Inspectors_NewInspector)

_Inspectors = null

_Explorers.NewExplorer -= new

Outlook.ExplorersEvents_NewExplorerEventHandler(_Explorers_NewExplorer)

_Explorers = null

GC.Collect()

GC.WaitForPendingFinalizers()

}

#region VSTO generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InternalStartup()

{

this.Startup += new System.EventHandler(ThisAddIn_Startup)

this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown)

}

#endregion

}

The abstract WrapperClass:

/// <summary>

/// Delegate signature to inform the application about closed objects.

/// </summary>

/// <param name=""""id"""" />The unique ID of the closed object.</param />

public delegate void WrapperClosedDelegate(Guid id)

/// <summary>

/// The Wrapperclass itself has a unique ID and a closed event.

/// </summary>

internal abstract class WrapperClass

{

/// <summary>

/// The event occurs when the monitored item has been closed.

/// </summary>

public event WrapperClosedDelegate Closed

/// <summary>

/// The unique ID of the wrapped object.

/// </summary>

public Guid Id { getprivate set}

protected void OnClosed()

{

if (Closed != null) Closed(Id)

}

/// <summary>

/// The constructor creates a new unique ID.

/// </summary>

public WrapperClass()

{

Id = Guid.NewGuid()

}

}

The Inspector wrapper class:

/// <summary>

/// The InspectorWrapper used to monitor the state of an Inspector during its lifetime.

/// </summary>

internal class InspectorWrapper : WrapperClass

{

/// <summary>

/// The Outlook Inspector Instance.

/// </summary>

public Outlook.Inspector Inspector { getprivate set}

/// <summary>

/// Construction code.

/// </summary>

/// <param name=""""inspector"""" />The Inspector Object</param />

public InspectorWrapper(Outlook.Inspector inspector)

{

Inspector = inspector

ConnectEvents()

}

/// <summary>

/// Register the events to get notified of Inspector statechanges within the application.

/// </summary>

void ConnectEvents()

{

((Outlook.InspectorEvents_10_Event)Inspector).Close +=

new Outlook.InspectorEvents_10_CloseEventHandler(InspectorWrapper_Close)

((Outlook.InspectorEvents_10_Event)Inspector).Activate +=

new Outlook.InspectorEvents_10_ActivateEventHandler(InspectorWrapper_Activate)

((Outlook.InspectorEvents_10_Event)Inspector).Deactivate +=

new Outlook.InspectorEvents_10_DeactivateEventHandler(InspectorWrapper_Deactivate)

}

/// <summary>

/// Unregister the events / cleanup.

/// </summary>

void DisconnectEvents()

{

((Outlook.InspectorEvents_10_Event)Inspector).Close -=

new Outlook.InspectorEvents_10_CloseEventHandler(InspectorWrapper_Close)

((Outlook.InspectorEvents_10_Event)Inspector).Activate -=

new Outlook.InspectorEvents_10_ActivateEventHandler(InspectorWrapper_Activate)

((Outlook.InspectorEvents_10_Event)Inspector).Deactivate -=

new Outlook.InspectorEvents_10_DeactivateEventHandler(InspectorWrapper_Deactivate)

}

/// <summary>

/// Event sink for the Close event. Memory Cleanup and inform the application.

/// </summary>

void InspectorWrapper_Close()

{

DisconnectEvents()

Inspector = null

GC.Collect()

GC.WaitForPendingFinalizers()

// inform the application to release al references.

OnClosed()

}

/// <summary>

/// Event sink for the Activate event

/// </summary>

void InspectorWrapper_Activate()

{

}

/// <summary>

/// Event sink for the deactivate event

/// </summary>

void InspectorWrapper_Deactivate()

{

}

}

详细请见http://www.codeproject.com/KB/office/CustomAddressDialog.aspx?display=Print


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8236089.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存