如何使用 Visual C# .NET 处理 Excel 事件

Posted on 2014-09-24 09:10  且行且歌  阅读(998)  评论(0编辑  收藏  举报
事件处理概述 
Visual C# .NET 使用委派处理来自组件对象模型 (COM) 服务器的事件。委派是 Microsoft Visual Studio .NET 中的一个新概念。对于 COM 事件,委派是一种特殊对象,它侦听来自 COM 服务器的事件,然后将其转发给 Visual C# 函数。要使用委派,必须创建对象的实例,然后将该对象实例添加到要侦听的事件中。每个事件都有一个委派,该委派专门设计用于将 COM 事件(使用本机数据类型)转换为标准 Microsoft .NET 调用(使用托管数据类型)。


创建 Visual C# .NET 自动化客户端 
要使用委派从使用 Visual C# .NET 开发的自动化客户端处理 Excel 事件,请按照下列步骤操作:1. 启动 Visual Studio .NET 2002 或 Visual Studio .NET 2003。在“文件”菜单上,单击“新建”,然后单击“项目”。在“Visual C# 项目”下,选择“Windows 应用程序”。将项目命名为 XLEventTest,然后单击“确定”。
默认情况下会创建 Form1。  
2. 添加对“Microsoft Excel 对象库”的引用。为此,请按照下列步骤操作: a.  在“项目”菜单上,单击“添加引用”。  
b.  在“COM”选项卡上,找到“Microsoft Excel 11.0 对象库”,然后单击“选择”。 
c.  在“添加引用”对话框中单击“确定”,接受您的选择。如果系统提示您为选定的库生成包装,请单击“是”。  
 
3. 在解决方案资源管理器中,双击“Form1.cs”以在“设计”视图中显示该窗体。  
4. 在“视图”菜单上,单击“工具箱”以显示工具箱,然后向 Form1 中添加一个按钮。将该按钮的“Text”属性更改为启动 Excel。  
5. 双击“启动 Excel”以显示该窗体的“代码”窗口。将以下代码添加到该按钮的 Click 事件处理程序中:  private void button1_Click(object sender, System.EventArgs e)
{
   StartExcelAndSinkEvents();

 
6. 在靠近文件顶部、另一个 using 语句下方添加以下代码: using System.Reflection;
using System.Diagnostics;
using Excel = Microsoft.Office.Interop.Excel; 
 
7. 将以下代码添加到 Form1 类中,使其位于步骤 5 中的 Click 事件处理程序的下方:  //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;
private void StartExcelAndSinkEvents()
{
   //Start Excel, and then create a new workbook.
   xlApp = new Excel.Application();
   xlBook = xlApp.Workbooks.Add( Missing.Value );
   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;
   //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.
   Debug.WriteLine("Delegate: You Changed Cells " +
      Target.get_Address( Missing.Value, Missing.Value,
      Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value ) +
      " on " + Target.Worksheet.Name);
}
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;
   Debug.WriteLine("Delegate: Closing the workbook and removing event handlers.");
   xlSheet1.Change -= EventDel_CellsChange;
   xlSheet2.Change -= EventDel_CellsChange;
   xlSheet3.Change -= EventDel_CellsChange;
   xlApp.WorkbookBeforeClose -= EventDel_BeforeBookClose;
}      
 
 
测试代码 
1. 按 Ctrl+Alt+O 以显示“输出”窗口。 
2. 按 F5 生成并运行该程序。 
3. 在窗体上,单击“启动 Excel”按钮。
程序将启动 Excel,然后创建一个具有三张工作表的工作簿。 
4. 向任一张工作表的单元格中添加任意数据。
查看 Visual Studio 中的“输出”窗口,以确认调用了事件处理程序。 
5. 退出 Excel,然后关闭窗体以结束调试会话。
 回到顶端
疑难解答 
编译代码时,可能会收到以下编译器错误消息:

命名空间已经包含了“Excel”的定义

如果没有安装用于 Excel 的主互操作程序集 (PIA),则会收到此错误消息。要解决此问题,请按照下列步骤操作:1. 运行 Microsoft Office 安装程序,然后安装 Excel PIA。在 Office 安装程序中,PIA 显示为 Excel 下的一个组件“.NET 可编程性支持”。 
2. 打开您的项目,删除对 Excel 互操作程序集的引用,然后重复本文“创建 Visual C# .NET 自动化客户端”部分中的步骤 2,以正确地引用 PIA。 
测试该代码时,您可能会收到以下错误消息:

未处理的“System.InvalidCastException”类型的异常出现在 interop.excel.dll 中。
其他信息:不支持此种接口

有关此错误消息的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章: 
316653 (http://support.microsoft.com/kb/316653/) PRB:使用 WithEvents 或委派从 Visual Basic .NET 或 Visual C# .NET 处理 Excel 事件时出现错误 
 回到顶端
参考
有关其他信息,请访问下面的 Microsoft Developer Network (MSDN) 网站: 
http://msdn.microsoft.com/library/en-us/dnoxpta/html/vsofficedev.asp(http://msdn.microsoft.com/library/en-us/dnoxpta/html/vsofficedev.asp)
有关在 Visual C# .NET 中实现 Excel 自动化的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章: 
302084 (http://support.microsoft.com/kb/302084/) 如何在 Microsoft Visual C# .NET 中实现 Microsoft Excel 自动化 
302096 (http://support.microsoft.com/kb/302096/) 如何在 Visual C# .NET 中使 Excel 自动运行以使用数组填充或获取某个区域中的数据 
302902 (http://support.microsoft.com/kb/302902/) 用 Visual C# 进行 Office 自动化服务器的绑定 

Copyright © 2024 且行且歌
Powered by .NET 8.0 on Kubernetes