工作表主專案Worksheet host item

Worksheet 主項目是可從 Excel 的主要 Interop 組件擴充 Worksheet 類型的一種類型。The Worksheet host item is a type that extends the Worksheet type from the primary interop assembly for Excel. Worksheet 主項目除了提供與 Worksheet 物件相同的所有屬性、方法和事件之外,也會公開其他事件,並做為主控制項和 Windows Forms 控制項的容器。The Worksheet host item provides all of the same properties, methods, and events as a Worksheet object, but it also exposes additional events and acts as a container for host controls and Windows Forms controls.

適用于: 本主題中的資訊適用于 Excel 的檔 - 層級專案和 VSTO 新增 - 專案。Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Excel. 如需詳細資訊,請參閱 依 Office 應用程式和專案類型提供的功能For more information, see Features available by Office application and project type.

在文件層級專案中,您可以在設計階段將 Worksheet 主項目加入專案。In document-level projects, you can add Worksheet host items to your project at design time. 在 VSTO 增益集專案中,您可以在執行階段產生 Worksheet 主項目。In VSTO Add-in projects, you can generate Worksheet host items at run time.

瞭解檔層級專案中的工作表主專案Understand worksheet host items in document-level projects

當您建立 Excel 的文件層級專案時,Visual Studio 會自動在專案中建立三個 Worksheet 主項目。When you create a document-level project for Excel, Visual Studio automatically creates three Worksheet host items in the project. 這些工作表的預設名稱為 Sheet1Sheet2Sheet3The default names of the worksheets are Sheet1, Sheet2, and Sheet3. 如果您根據現有的活頁簿建立專案,主項目的數目取決於活頁簿中的工作表數目。If you create a project based on an existing workbook, the number of host items depends on the number of worksheets in the workbook.

這些工作表類別可讓您存取 Worksheet 主項目的成員,以便在自訂中執行基本工作,例如修改工作表的內容。These worksheet classes give you access to members of the Worksheet host item to perform basic tasks in your customization, such as modifying the contents of a worksheet. 您也可以使用這些類別將控制項加入工作表。You can also use these classes to add controls to worksheets. 藉由合併不同的控制項集合並撰寫程式碼,您可以將控制項繫結至資料、從使用者收集資訊,以及回應使用者動作。By combining different sets of controls and writing code, you can bind the controls to data, collect information from the user, and respond to user actions. 如需詳細資訊,請參閱 程式檔層級自訂程式For more information, see Program document-level customizations.

工作表類別提供了一個位置,供您開始在專案中撰寫程式碼。The worksheet classes provide a location in which you can start writing code in your project. 由於該類別會提供與 Excel 之主要 Interop 組件中的 Worksheet 物件相同的所有屬性、方法和事件,因此您也可以使用這些類別存取 Excel 的物件模型。Because the class provides all of the same properties, methods, and events as the Worksheet object in the primary interop assembly for Excel, you can also use these classes to access the object model of Excel. 如需詳細資訊,請參閱 Excel 物件模型總覽For more information, see Excel object model overview.

在文件層級專案中,您可以在設計階段將其他 Worksheet 主項目加入專案,方法是在設計工具中將新的工作表加入活頁簿。In document-level projects, you can add additional Worksheet host items to the project at design time by adding a new worksheet to the workbook in the designer.

重新命名工作表Rename worksheets

在文件層級專案中,您可以於 Visual Studio 設計工具中為工作表重新命名,但這樣做只會變更工作表的顯示名稱。In a document-level project, you can rename the worksheets in the Visual Studio designer, but this only changes the display name of the worksheet. 程式設計名稱仍是工作表的預設名稱。The programmatic name is still the default name of the worksheet. 如果您在 [屬性] 視窗中為工作表重新命名,則只會變更程式設計名稱。If you rename the worksheet in the Properties window, only the programmatic name is changed.

檔層級專案中工作表主專案的限制Limitations of the worksheet host item in document-level projects

您無法在文件層級專案的執行階段建立新的 Worksheet 主項目。You cannot create new Worksheet host items at run time in a document-level project. 如果您在執行階段建立新的 Excel 工作表,該工作表會是 Worksheet類型。If you create a new Excel worksheet at run time, it will be of the type Worksheet. 由於這不是主項目,因此無法包含任何主控制項或 Windows Forms 控制項。Because it is not a host item, it cannot contain any host controls or Windows Forms controls. 如需在執行時間建立檔的詳細資訊,請參閱 如何:以程式設計方式在活頁簿中加入新的工作表For more information about creating documents at run time, see How to: Programmatically add new worksheets to workbooks.

瞭解 VSTO 增益集專案中的工作表主專案Understand worksheet host items in VSTO Add-in projects

在應用程式層級專案中,您可以在執行階段為使用 Excel 開啟的任何工作表產生 Worksheet 主項目。In application-level projects, you can generate a Worksheet host item at run time for any worksheet that is open in Excel. 您可以使用 Worksheet 主項目將控制項加入相關聯的工作表,或處理 Worksheet 物件上沒有的事件。You can use the Worksheet host item to add controls to the associated worksheet, or to handle events that are not available on Worksheet objects.

若要產生 Worksheet 主項目,請使用 GetVstoObject 方法。To generate a Worksheet host item, use the GetVstoObject method. 如需詳細資訊,請參閱 在 VSTO 增益集中,于執行時間擴充 Word 檔和 Excel 活頁簿For more information, see Extend Word documents and Excel workbooks in VSTO Add-ins at run time.

另請參閱See also