WorkbookEvents Interface WorkbookEvents_ActivateEventHandler Delegate WorkbookEvents_AddinInstallEventHandler Delegate WorkbookEvents_AddinUninstallEventHandler Delegate WorkbookEvents_AfterXmlExportEventHandler Delegate WorkbookEvents_AfterXmlImportEventHandler Delegate WorkbookEvents_BeforeCloseEventHandler Delegate WorkbookEvents_BeforePrintEventHandler Delegate WorkbookEvents_BeforeSaveEventHandler Delegate WorkbookEvents_BeforeXmlExportEventHandler Delegate WorkbookEvents_BeforeXmlImportEventHandler Delegate WorkbookEvents_DeactivateEventHandler Delegate WorkbookEvents_Event Interface WorkbookEvents_NewSheetEventHandler Delegate WorkbookEvents_OpenEventHandler Delegate WorkbookEvents_PivotTableCloseConnectionEventHandler Delegate WorkbookEvents_PivotTableOpenConnectionEventHandler Delegate WorkbookEvents_SheetActivateEventHandler Delegate WorkbookEvents_SheetBeforeDoubleClickEventHandler Delegate WorkbookEvents_SheetBeforeRightClickEventHandler Delegate WorkbookEvents_SheetCalculateEventHandler Delegate WorkbookEvents_SheetChangeEventHandler Delegate WorkbookEvents_SheetDeactivateEventHandler Delegate WorkbookEvents_SheetFollowHyperlinkEventHandler Delegate WorkbookEvents_SheetPivotTableUpdateEventHandler Delegate WorkbookEvents_SheetSelectionChangeEventHandler Delegate WorkbookEvents_SinkHelper Class WorkbookEvents_SyncEventHandler Delegate WorkbookEvents_WindowActivateEventHandler Delegate WorkbookEvents_WindowDeactivateEventHandler Delegate WorkbookEvents_WindowResizeEventHandler Delegate RefreshEvents Interface RefreshEvents_AfterRefreshEventHandler Delegate RefreshEvents_BeforeRefreshEventHandler Delegate RefreshEvents_Event Interface RefreshEvents_SinkHelper Class OLEObjectEvents_GotFocusEventHandler Delegate OLEObjectEvents_LostFocusEventHandler Delegate DocEvents_ActivateEventHandler Delegate DocEvents_BeforeDoubleClickEventHandler Delegate DocEvents_BeforeRightClickEventHandler Delegate DocEvents_CalculateEventHandler Delegate DocEvents_ChangeEventHandler Delegate DocEvents_DeactivateEventHandler Delegate DocEvents_Event Interface DocEvents_FollowHyperlinkEventHandler Delegate DocEvents_PivotTableUpdateEventHandler Delegate DocEvents_SelectionChangeEventHandler Delegate ChartEvents_ActivateEventHandler Delegate ChartEvents_BeforeDoubleClickEventHandler Delegate ChartEvents_BeforeRightClickEventHandler Delegate ChartEvents_CalculateEventHandler Delegate ChartEvents_DeactivateEventHandler Delegate ChartEvents_DragOverEventHandler Delegate ChartEvents_DragPlotEventHandler Delegate ChartEvents_Event Interface ChartEvents_MouseDownEventHandler Delegate ChartEvents_MouseMoveEventHandler Delegate ChartEvents_MouseUpEventHandler Delegate ChartEvents_ResizeEventHandler Delegate ChartEvents_SelectEventHandler Delegate ChartEvents_SeriesChangeEventHandler Delegate using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; using System.Reflection; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { StartExcelAndSinkEvents(); } //Excel Automation variables: Excel.Application xlApp; Excel.Workbook xlBook; Excel.Worksheet xlSheet1, xlSheet2, xlSheet3; //Excel event delegate variables: Excel.AppEvents_WorkbookBeforeCloseEventHandler EventDel_BeforeBookClose; Excel.DocEvents_ChangeEventHandler EventDel_CellsChange; Excel.DocEvents_BeforeRightClickEventHandler Event_RightClick; private void StartExcelAndSinkEvents() { //Start Excel, and then create a new workbook. xlApp = new Excel.Application(); xlBook = xlApp.Workbooks.Open(@"C:UsersAdministrator.PC-20150725BXRIDesktop新建 Microsoft Excel 工作表.xlsx");//.Add(); xlBook.Windows.get_Item(1).Caption = "XL Event Test"; xlSheet1 = (Excel.Worksheet)xlBook.Worksheets.get_Item(1); xlSheet2 = (Excel.Worksheet)xlBook.Worksheets.get_Item(2); xlSheet3 = (Excel.Worksheet)xlBook.Worksheets.get_Item(3); xlSheet1.Activate(); //Add an event handler for the WorkbookBeforeClose Event of the //Application object. EventDel_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeCloseEventHandler(BeforeBookClose); xlApp.WorkbookBeforeClose += EventDel_BeforeBookClose; //Add an event handler for the Change event of both worksheet objects. EventDel_CellsChange = new Excel.DocEvents_ChangeEventHandler(CellsChange); xlSheet1.Change += EventDel_CellsChange; xlSheet2.Change += EventDel_CellsChange; xlSheet3.Change += EventDel_CellsChange; Event_RightClick = new Excel.DocEvents_BeforeRightClickEventHandler(RightChange); xlSheet1.BeforeRightClick += Event_RightClick; xlSheet2.BeforeRightClick += Event_RightClick; xlSheet3.BeforeRightClick += Event_RightClick; //Make Excel visible and give the user control. xlApp.Visible = true; xlApp.UserControl = true; } private void CellsChange(Excel.Range Target) { //This is called when any cell on a worksheet is changed. MessageBox.Show("Delegate: You Changed Cells " + Target.get_Address(Missing.Value, Missing.Value, Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value) + " on " + Target.Worksheet.Name); } private void RightChange(Excel.Range Target, ref bool bl) { MessageBox.Show(bl.ToString() + Target.get_Address()); } private void BeforeBookClose(Excel.Workbook Wb, ref bool Cancel) { //This is called when you choose to close the workbook in Excel. //The event handlers are removed, and then the workbook is closed //without saving the changes. Wb.Saved = true; MessageBox.Show("Delegate: Closing the workbook and removing event handlers."); xlSheet1.Change -= EventDel_CellsChange; xlSheet2.Change -= EventDel_CellsChange; xlSheet3.Change -= EventDel_CellsChange; xlApp.WorkbookBeforeClose -= EventDel_BeforeBookClose; } } }