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。)
