Excel 物件模型總覽Excel object model overview

若要開發使用 Microsoft Office Excel 的方案,您可以與 Excel 物件模型提供的物件進行互動。To develop solutions that use Microsoft Office Excel, you can interact with the objects provided by the Excel object model. 本主題將介紹最重要的物件:This topic introduces the most important objects:


有興趣開發跨多個平臺擴充 Office 體驗的解決方案嗎?Interested in developing solutions that extend the Office experience across multiple platforms? 查看新的 Office 增益集模型Check out the new Office Add-ins model. 相較于 VSTO 增益集和方案,Office 增益集的使用量較小,而且您可以使用幾乎任何 web 程式設計技術(例如 HTML5、JavaScript、CSS3 和 XML)來建立這些增益集。Office Add-ins have a small footprint compared to VSTO Add-ins and solutions, and you can build them by using almost any web programming technology, such as HTML5, JavaScript, CSS3, and XML.

這個物件模型密切遵循其使用者介面。The object model closely follows the user interface. Application 物件代表整個應用程式,而每個 Workbook 物件都包含 Worksheet 物件的集合。The Application object represents the entire application, and each Workbook object contains a collection of Worksheet objects. 其中,代表儲存格的主要抽象物件是 Range 物件,它可以讓您使用個別儲存格或儲存格群組。From there, the major abstraction that represents cells is the Range object, which enables you to work with individual cells or groups of cells.

除了 Excel 物件模型之外,Visual Studio 中的 Office 專案還提供 主專案主控制項 ,以擴充 excel 物件模型中的部分物件。In addition to the Excel object model, Office projects in Visual Studio provide host items and host controls that extend some objects in the Excel object model. 主項目和主控制項的行為與它們所擴充的 Excel 物件相同,但還具有其他功能 (例如資料繫結功能和額外事件)。Host items and host controls behave like the Excel objects they extend, but they also have additional functionality such as data-binding capabilities and extra events. 如需詳細資訊,請參閱 使用擴充物件自動化 Excel主專案和主控制項總覽For more information, see Automate Excel by using extended objects and Host items and host controls overview.

本主題提供 Excel 物件模型的簡短概觀。This topic provides a brief overview of the Excel object model. 如需可深入瞭解整個 Excel 物件模型的資源,請參閱 使用 excel 物件模型檔For resources where you can learn more about the entire Excel object model, see Use the Excel object model documentation.

存取 Excel 專案中的物件Access objects in an Excel project

當您建立 Excel 的新 VSTO 增益集專案時,Visual Studio 會自動建立 ThisAddIn .vbThisAddIn.cs 程式碼檔案。When you create a new VSTO Add-in project for Excel, Visual Studio automatically creates a ThisAddIn.vb or ThisAddIn.cs code file. 您可以使用 Me.Applicationthis.Application,來存取 Application 物件。You can access the Application object by using Me.Application or this.Application.

當您建立 Excel 的新文件層級專案時,可以選擇建立新的 Excel 活頁簿或 Excel 範本專案。When you create a new document-level project for Excel, you have the option of creating a new Excel Workbook or Excel Template project. Visual Studio 會在新的 Excel 專案中,自動為活頁簿和範本專案建立下列程式碼檔。Visual Studio automatically creates the following code files in your new Excel project for both workbook and template projects.

Visual BasicVisual Basic C#C#
ThisWorkbook.vbThisWorkbook.vb ThisWorkbook.csThisWorkbook.cs
Sheet1.vbSheet1.vb Sheet1.csSheet1.cs
Sheet2.vbSheet2.vb Sheet2.csSheet2.cs
Sheet3.vbSheet3.vb Sheet3.csSheet3.cs

您可以在專案中使用 Globals 類別,從個別的類別之外存取 ThisWorkbookSheet1Sheet2Sheet3You can use the Globals class in your project to access ThisWorkbook, Sheet1, Sheet2, or Sheet3 from outside of the respective class. 如需詳細資訊,請參閱 全域存取 Office 專案中的物件For more information, see Global access to objects in Office projects. 下列範例會呼叫的 PrintPreview 方法, Sheet1 不論程式碼是否放在其中一個 Sheet n 類別或 ThisWorkbook 類別中。The following example calls the PrintPreview method of Sheet1 regardless of whether the code is placed in one of the Sheetn classes or the ThisWorkbook class.


由於 Excel 文件包含高度結構化的資料,因此物件模型不僅層級分明,而且非常明確。Because the data in an Excel document is highly structured, the object model is hierarchical and straightforward. Excel 提供數百個您可能想要進行互動的物件,但是您可以將焦點放在可用物件的一小部分,以在物件模型上取得良好的起點。Excel provides hundreds of objects with which you might want to interact, but you can get a good start on the object model by focusing on a small subset of the available objects. 這些物件包含下列四個項目:These objects include the following four:

  • 應用程式Application

  • 活頁簿Workbook

  • 工作表Worksheet

  • 範圍Range

    使用 Excel 執行的大部分工作都是以這四個物件及其成員為中心。Much of the work done with Excel centers around these four objects and their members.

應用程式物件Application object

Excel Application 物件代表 Excel 應用程式本身。The Excel Application object represents the Excel application itself. Application 物件會公開有關執行中應用程式、套用至該執行個體的選項,以及目前在執行個體中開啟之使用者物件的大量資訊。The Application object exposes a great deal of information about the running application, the options applied to that instance, and the current user objects open within the instance.


請勿在 Excel 中,將 EnableEvents 物件的 Application 屬性設定為 false(Native Office Object)。You should not set the EnableEvents property of the Application object in Excel to false. 將這個屬性設定為 false,會導致 Excel 無法引發任何事件,包括主控制項的事件在內。Setting this property to false prevents Excel from raising any events, including the events of host controls.

活頁簿物件Workbook object

Workbook 物件代表 Excel 應用程式中的單一活頁簿。The Workbook object represents a single workbook within the Excel application.

Visual Studio 中的 Office 程式開發工具會藉由提供 Workbook 類型,來擴充 Workbook 物件。The Office development tools in Visual Studio extend the Workbook object by providing the Workbook type. 這個類型可讓您存取 Workbook 物件的所有功能。This type gives you access to all features of a Workbook object. 如需詳細資訊,請參閱活頁 簿主專案For more information, see Workbook host item.

Worksheet 物件Worksheet object

Worksheet 物件是 Worksheets 集合的成員。The Worksheet object is a member of the Worksheets collection. Worksheet 的許多屬性、方法和事件都與 ApplicationWorkbook 物件所提供的成員完全相同或類似。Many of the properties, methods, and events of the Worksheet are identical or similar to members provided by the Application or Workbook objects.

Excel 提供 Sheets 集合,做為 Workbook 物件的屬性。Excel provides a Sheets collection as a property of a Workbook object. Sheets 集合的每個成員都是 WorksheetChart 物件。Each member of the Sheets collection is either a Worksheet or a Chart object.

Visual Studio 中的 Office 程式開發工具會藉由提供 Worksheet 類型,來擴充 Worksheet 物件。The Office development tools in Visual Studio extend the Worksheet object by providing the Worksheet type. 這個類型可讓您存取 Worksheet 物件的所有功能,以及新功能 (例如可以裝載 Managed 控制項和處理新事件)。This type gives you access to all features of a Worksheet object, as well as new features such as the ability to host managed controls and handle new events. 如需詳細資訊,請參閱 工作表主專案For more information, see Worksheet host item.

Range 物件Range object

Range 物件是您在 Excel 應用程式中最常使用的物件。The Range object is the object you will use most within your Excel applications. 若要操作 Excel 中的任何區域,您必須先將其表示為 Range 物件,然後再搭配該範圍的方法和屬性使用。Before you can manipulate any region within Excel, you must express it as a Range object and work with methods and properties of that range. Range 物件代表一個儲存格、一個資料列、一個資料行、含有一或多個不一定連續之儲存格區塊的儲存格選取範圍,或甚至是多個工作表上的儲存格群組。A Range object represents a cell, a row, a column, a selection of cells that contains one or more blocks of cells, which might or might not be contiguous, or even a group of cells on multiple sheets.

Visual Studio 會藉由提供 NamedRangeXmlMappedRange 類型,來擴充 Range 物件。Visual Studio extends the Range object by providing the NamedRange and XmlMappedRange types. 這些類型具有與 Range 物件相同的大部分功能,以及新功能 (例如資料繫結功能和新事件)。These types have most of the same features as a Range object, as well as new features such as the data binding capability and new events. 如需詳細資訊,請參閱 NamedRange 控制項XmlMappedRange 控制項For more information, see NamedRange control and XmlMappedRange control.

使用 Excel 物件模型檔Use the Excel object model documentation

如需 Excel 物件模型的完整資訊,您可以參閱 Excel 主要 Interop 組件 (PIA) 參考和 VBA 物件模型參考。For complete information about the Excel object model, you can refer to the Excel primary interop assembly (PIA) reference and the VBA object model reference.

主要 interop 元件參考Primary interop assembly reference

Excel PIA 參考文件說明 Excel 主要 Interop 組件中的類型。The Excel PIA reference documentation describes the types in the primary interop assembly for Excel. 您可以從下列位置取得此檔: Excel 2010 主要 interop 元件參考This documentation is available from the following location: Excel 2010 primary interop assembly reference.

如需有關 Excel PIA 設計的詳細資訊,例如 PIA 中類別和介面的差異以及 PIA 中的事件的執行方式,請參閱 Office 主要 interop 元件中的類別和介面總覽For more information about the design of the Excel PIA, such as the differences between classes and interfaces in the PIA and how events in the PIA are implemented, see Overview of classes and interfaces in the Office primary interop assemblies.

VBA 物件模型參考VBA object model reference

VBA 物件模型參考記載公開給 Visual Basic for Applications (VBA) 程式碼時的 Excel 物件模型。The VBA object model reference documents the Excel object model as it is exposed to Visual Basic for Applications (VBA) code. 如需詳細資訊,請參閱 Excel 2010 物件模型參考For more information, see Excel 2010 object model reference.

VBA 物件模型參考中的所有物件和成員都會對應至 Excel PIA 中的類型和成員。All of the objects and members in the VBA object model reference correspond to types and members in the Excel PIA. 例如,VBA 物件模型參考中的工作表物件會對應至 Worksheet EXCEL PIA 中的物件。For example, the Worksheet object in the VBA object model reference corresponds to the Worksheet object in the Excel PIA. 雖然 VBA 物件模型參考提供大部分屬性、方法和事件的程式碼範例,但如果您想要在以 Visual Studio 建立的 Excel 專案中使用這些程式碼範例,則必須將這個參考中的 VBA 程式碼轉譯為 Visual Basic 或 Visual C#。Although the VBA object model reference provides code examples for most properties, methods, and events, you must translate the VBA code in this reference to Visual Basic or Visual C# if you want to use them in an Excel project that you create by using Visual Studio.

標題Title 描述Description
Excel 方案Excel solutions 說明如何建立 Microsoft Office Excel 的文件層級自訂和 VSTO 增益集。Explains how you can create document-level customizations and VSTO Add-ins for Microsoft Office Excel.
使用範圍Work with ranges 提供示範如何使用範圍執行常見工作的範例。Provides examples that show how to perform common tasks with ranges.
使用工作表Work with worksheets 提供示範如何使用工作表執行常見工作的範例。Provides examples that show how to perform common tasks with worksheets.
使用活頁簿Work with workbooks 提供示範如何使用活頁簿執行常見工作的範例。Provides examples that show how to perform common tasks with workbooks.