[转].NET平台下的Excel编程|C#操作Excel|Application和ApplicationClass的联系和区别
Understanding Office Primary Interop Assembly Classes and Interfaces
The Office Primary Interop Assemblies (PIAs) expose many classes and interfaces that were previously hidden. Most of these classes and assemblies are displayed in the Object Browser in Visual Studio; however, some do not appear. Although these classes and interfaces can be confusing at first glance, it helps to understand the relationships between them, and how they are used.
This topic outlines some key points to keep in mind while working with PIA classes and interfaces.
Application Classes and Interfaces
Although the Object Browser displays Application interfaces for the Microsoft Word and Microsoft Excel PIAs, it does not directly expose Application classes for Word and Excel. However, when you use the Application interface to instantiate an Application object, the common language runtime internally uses the Application class.
For example, the following code uses the Excel Microsoft.Office.Interop.Excel.Application interface. At run time, it uses the Application class to instantiate an ExcelApplication object and open a worksheet.
Private Sub btnRunExcel_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRunExcel.Click Dim xl As Microsoft.Office.Interop.Excel.Application xl = New Microsoft.Office.Interop.Excel.Application Dim wb As Microsoft.Office.Interop.Excel.Workbook wb = xl.Workbooks.Add() Dim ws As Microsoft.Office.Interop.Excel.Worksheet ws = wb.ActiveSheet xl.Visible = True End Sub
classidClass Classes
It is possible to use a classidClass class, such as the ApplicationClass class in Microsoft Word and Microsoft Excel, or the WorkbookClass or WorksheetClass classes in Microsoft Excel, to instantiate an object. However, this practice should be avoided.
Using these classes has the potential to cause ambiguities if some members share the same name. For example, Microsoft Word exposes both anMicrosoft.Office.Interop.Word._Application.Quit(System.Object,System.Object,System.Object) method and anMicrosoft.Office.Interop.Word.ApplicationEvents4_Event.Quit event. These ambiguities can generate a compiler error.
Instead, use the exposed interface for a class—such as the Application, Microsoft.Office.Interop.Excel.Workbook, or Microsoft.Office.Interop.Excel.Worksheet interface—to instantiate an object of that class. The following Visual Basic example uses the Microsoft.Office.Interop.Word.Application interface with the Quit method in Microsoft Word.
Private wd As Microsoft.Office.Interop.Word.Application Private Sub btnRunWord_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRunWord.Click wd = New Microsoft.Office.Interop.Word.Application wd.Visible = True End Sub Private Sub btnQuitWord_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnQuitWord.Click wd.Quit() wd = Nothing End Sub
_classid Interfaces
To avoid the potential for ambiguities, members of classes in which some members share the same name are displayed in the Object Browser as members of the corresponding _classid interface. For example, Application class members are displayed as members of the _Application interface. Although they are not displayed as members of their class interface in the Object Browser, internally they are associated with the class when you use them in code.
The following table lists the classes that have corresponding _classid interfaces.
Class | _Class Interface | PIA |
---|---|---|
Application |
_Application |
Word |
Document |
_Document |
Word |
Font |
_Font |
Word |
Global |
_Global |
Word |
LetterContent |
_LetterContent |
Word |
OLEControl |
_OLEControl |
Word |
ParagraphFormat |
_ParagraphFormat |
Word |
Application |
_Application |
Excel |
Chart |
_Chart |
Excel |
Global |
_Global |
Excel |
OLEObject |
_OLEObject |
Excel |
QueryTable |
_QueryTable |
Excel |
Workbook |
_Workbook |
Excel |
Worksheet |
_Worksheet |
Excel |
Event Interfaces
The classidEventsx interfaces displayed in the Object Browser, such as ApplicationEvents4 in the Word PIA, map directly to interfaces in the original Component Object Model (COM) type libraries and expose a set of corresponding methods. However, these methods are not directly usable.
Similarly, the PIAs implement ClassIdEventsx_SinkHelper classes, such as ApplicationEvents4_SinkHelper, which expose methods and delegates corresponding to each event. These methods and delegates are also for internal use only.
To work with events, use the ClassIdEventsx_Event interfaces, such as ApplicationEvents4_Event, which are also based on the classidEventsx interfaces. In addition, the PIAs implement classidEventsx_eventEventHandler delegates for each event. Use these delegates to create event handlers.
For more information, see Understanding Office Primary Interop Assembly Events.
Dual Interfaces
The Office PIAs implement a dual interface for many interfaces, with each interface having a corresponding Iinterfaceid interface. For example, the AppEvents interface in the Excel PIA has a corresponding IAppEvents interface. The Iinterfaceid interfaces are for internal use only and can be ignored.
.NET平台下的Excel编程|C#操作Excel|Application和ApplicationClass的联系和区别
1. Interop含义
Interop是互操作的含义。
Microsoft.Office.Interop.Excel.dll 是 Excel COM的.NET封装。
.NET code通过这些被重新封装的COM来操作Excel。
2. 基础环境
在运行环境中必须安装Office,否则即使有Microsoft.Office.Interop.Excel.dll也无法用.NET code来操作Excel。
当然,有些工具可以在没有安装Office的情况下操作Excel,比如GemBox.ExcelLite。但是,这些工具一般都要收费。(这些工具的原理是:使用Office文档的开放规范来直接生成Office文档。)
3. Application和ApplicationClass的联系和区别
Application和ApplicationClass都继承自接口_Application。
Application为接口。ApplicationClass为类。
Application和ApplicationClass所拥有的属性、方法基本相同,但是也有一些小的差别。
比如:ApplicationClass有一个方法:OpenText;而Application却没有这个方法。通过这个方法,可以直接操作Excel去打开用分隔符分割的.txt文件。(注意,是.txt文件而不是.csv文件。)
4. 代码实例
命名空间:Microsoft.Office.Interop.Excel
a. 创建Excel实例
Application excel = new Application();
b. 打开已有的一个workbook
Workbook workbook = excel.Workbooks.Open(FilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
c. 打开已有的一个worksheet
Worksheet worksheet = (Worksheet)workbook.Worksheets[1]; (打开第一张worksheet。)
d. 选取一整列
Range column = ((Range)worksheet.Cells[1, 1]).EntireColumn;(选取A列;方法:先选取A1单元格,然后选取A1单元格所在的这一整列。)
e. 改变单元格的格式
column.NumberFormat = "@";(将d中选取的一整列的格式设置成General。)