首页 > 代码库 > Excel Events

Excel Events

WorkbookEvents InterfaceWorkbookEvents_ActivateEventHandler DelegateWorkbookEvents_AddinInstallEventHandler DelegateWorkbookEvents_AddinUninstallEventHandler DelegateWorkbookEvents_AfterXmlExportEventHandler DelegateWorkbookEvents_AfterXmlImportEventHandler DelegateWorkbookEvents_BeforeCloseEventHandler DelegateWorkbookEvents_BeforePrintEventHandler DelegateWorkbookEvents_BeforeSaveEventHandler DelegateWorkbookEvents_BeforeXmlExportEventHandler DelegateWorkbookEvents_BeforeXmlImportEventHandler DelegateWorkbookEvents_DeactivateEventHandler DelegateWorkbookEvents_Event InterfaceWorkbookEvents_NewSheetEventHandler DelegateWorkbookEvents_OpenEventHandler DelegateWorkbookEvents_PivotTableCloseConnectionEventHandler DelegateWorkbookEvents_PivotTableOpenConnectionEventHandler DelegateWorkbookEvents_SheetActivateEventHandler DelegateWorkbookEvents_SheetBeforeDoubleClickEventHandler DelegateWorkbookEvents_SheetBeforeRightClickEventHandler DelegateWorkbookEvents_SheetCalculateEventHandler DelegateWorkbookEvents_SheetChangeEventHandler DelegateWorkbookEvents_SheetDeactivateEventHandler DelegateWorkbookEvents_SheetFollowHyperlinkEventHandler DelegateWorkbookEvents_SheetPivotTableUpdateEventHandler DelegateWorkbookEvents_SheetSelectionChangeEventHandler DelegateWorkbookEvents_SinkHelper ClassWorkbookEvents_SyncEventHandler DelegateWorkbookEvents_WindowActivateEventHandler DelegateWorkbookEvents_WindowDeactivateEventHandler DelegateWorkbookEvents_WindowResizeEventHandler DelegateRefreshEvents InterfaceRefreshEvents_AfterRefreshEventHandler DelegateRefreshEvents_BeforeRefreshEventHandler DelegateRefreshEvents_Event InterfaceRefreshEvents_SinkHelper ClassOLEObjectEvents_GotFocusEventHandler DelegateOLEObjectEvents_LostFocusEventHandler DelegateDocEvents_ActivateEventHandler DelegateDocEvents_BeforeDoubleClickEventHandler DelegateDocEvents_BeforeRightClickEventHandler DelegateDocEvents_CalculateEventHandler DelegateDocEvents_ChangeEventHandler DelegateDocEvents_DeactivateEventHandler DelegateDocEvents_Event InterfaceDocEvents_FollowHyperlinkEventHandler DelegateDocEvents_PivotTableUpdateEventHandler DelegateDocEvents_SelectionChangeEventHandler DelegateChartEvents_ActivateEventHandler DelegateChartEvents_BeforeDoubleClickEventHandler DelegateChartEvents_BeforeRightClickEventHandler DelegateChartEvents_CalculateEventHandler DelegateChartEvents_DeactivateEventHandler DelegateChartEvents_DragOverEventHandler DelegateChartEvents_DragPlotEventHandler DelegateChartEvents_Event InterfaceChartEvents_MouseDownEventHandler DelegateChartEvents_MouseMoveEventHandler DelegateChartEvents_MouseUpEventHandler DelegateChartEvents_ResizeEventHandler DelegateChartEvents_SelectEventHandler DelegateChartEvents_SeriesChangeEventHandler Delegateusing 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:\Users\Administrator.PC-20150725BXRI\Desktop\新建 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;        }    }}

 

Excel Events