用于 Excel 的文档级自定义项编程入门Get started programming document-level customizations for Excel

如果你刚开始使用 Visual Studio 创建 Microsoft Office Excel 文档级自定义项,下面是您需要了解。If you are just getting started creating document-level customizations for Microsoft Office Excel by using Visual Studio, here is what you need to know.

适用于: 本主题中的信息适用于文档-Excel 级别项目。Applies to: The information in this topic applies to document-level projects for Excel. 有关详细信息,请参阅按 Office 应用程序和项目类型提供的功能For more information, see Features available by Office application and project type.

了解如何使用文档级自定义项的 Excel 工作Understand how document-level customizations for Excel work

基于 Excel 的文档级自定义项的是单个工作簿。A document-level customization for Excel is based around a single workbook. 若要开始使用自定义项,最终用户打开该工作簿或 Excel 模板从创建该工作簿。To start using the customization, the end user opens the workbook or creates the workbook from an Excel template. 工作簿,例如在单元格中键入或单击按钮和菜单项中的事件可以在程序集中调用事件处理方法。Events in the workbook, for example typing in cells or clicking buttons and menu items, can call event-handling methods in the assembly. 关闭工作簿后,不再在 Excel 中,仅在包含它们的文档中提供自定义项提供的功能。When the workbook is closed, the features provided by the customization are no longer available in Excel, only in the document that contained them.

有关详细信息,请参阅的文档级自定义体系结构For more information, see Architecture of document-level customizations.

创建 Excel 文档级项目Create document-level projects for Excel

若要创建 Excel 文档级自定义,请使用中的 Excel 工作簿或 Excel 模板项目模板新的项目对话框。To create a document-level customization for Excel, use the Excel Workbook or Excel Template project template in the New Project dialog box. 这些模板包括所需程序集引用和项目文件。These templates include required assembly references and project files.

有关如何创建 Excel 文档级项目的详细信息,请参阅如何:在 Visual Studio 中创建 Office 项目For more information about how to create a document-level project for Excel, see How to: Create Office projects in Visual Studio. 有关项目模板的详细信息,请参阅Office 项目模板概述For more information about the project templates, see Office project templates overview.

通过使用程序 Excel 工作簿主机项和主机控件Program Excel workbooks by using host items and host controls

主机项承载控件是提供用于使用 Visual Studio 创建的文档级自定义项编程模型的类。Host items and host controls are classes that provide the programming model for document-level customizations created by using Visual Studio.

宿主项提供的入口点为你的代码,并且它们还可以充当宿主控件和 Windows 窗体控件的容器。Host items provide an entry point for your code, and they can also act as containers for host controls and Windows Forms controls. 在 excel 文档级项目中,这些主机项由ThisWorkbookSheet1Sheet2,和Sheet3类。In document-level projects for Excel, these host items are represented by the ThisWorkbook, Sheet1, Sheet2, and Sheet3 classes.

主机控件基于本机 Excel 对象,例如对象列表和范围。Host controls are based on native Excel objects, such as list objects and ranges. 宿主控件提供本机 Excel 对象,与类似的功能,但它们还具有新的事件、 设计器支持和数据绑定功能。Host controls provide similar functionality to the native Excel objects, but they also have new events, designer support, and data binding capability. 它们显示为在你的项目代码和 IntelliSense,因此可以轻松地引用直接在代码中的特定对象,而无需导航 Excel 对象模型中的第一类对象。They appear as first-class objects in your project code and in IntelliSense, which makes it easier to refer to specific objects directly in your code without having to navigate the Excel object model.

有关详细信息,请参阅下列主题:For more information, see the following topics:

自定义 Excel 的用户界面Customize the user interface of Excel

大多数 Microsoft Office 解决方案的 Office 应用程序提供用户交互的解决方案某种方式修改用户界面 (UI)。Most Microsoft Office solutions modify the user interface (UI) of the Office application to provide some way for users to interact with the solution. 有许多方法通过使用文档级自定义可以在其中修改 UI Excel。There are many ways in which you can modify the UI of Excel by using a document-level customization. 例如,可以将控件添加到功能区中,或者,可以显示操作窗格。For example, you can add controls to the ribbon, or you can display an actions pane. 有关详细信息,请参阅Office UI 自定义For more information, see Office UI customization.

此外可以打开与你直接在 Visual Studio 中的项目相关联的工作簿。You can also open the workbook that is associated with your project directly in Visual Studio. 在 Visual Studio 中打开工作簿时,可以通过使用 Excel 用户界面来修改该工作簿。When the workbook is open in Visual Studio, you can modify the workbook by using the Excel user interface. 此外可以为设计图面上,这样就可以将控件拖到工作表上使用该工作簿。You can also use the workbook as a design surface, which enables you to drag controls onto worksheets. 有关详细信息,请参阅Visual Studio 环境中的 Office 项目For more information, see Office projects in the Visual Studio environment.

使用数据绑定Use data binding

主机控件的控件,您可以拖动它从列表中也存在数据源窗口。The host controls are also in the list of controls that you can drag from the Data Sources window. 在此方法会自动添加宿主控件将将它们绑定到设置使用在窗口的数据源。Adding host controls in this way automatically binds them to the data source that you set up using the window. 无需编写任何代码,可以显示来自数据库、 web 服务和业务对象数据。Without writing any code, you can display data from databases, web services, and business objects. 有关详细信息,请参阅将数据绑定到 Office 解决方案中的控件For more information, see Bind data to controls in Office solutions.

后续步骤Next steps

若要了解如何创建 Excel 文档级自定义,请参阅演练:创建在第一个文档级自定义 excelTo learn how to create a document-level customization for Excel, see Walkthrough: Create your first document-level customization for Excel. 本演练向您介绍 Visual Studio 和 Excel 文档级自定义项的编程模型中的 Office 开发工具。This walkthrough introduces you to the Office development tools in Visual Studio and the programming model for Excel document-level customizations.

有关指导你完成某些 Excel 项目中的常见任务的主题的列表,请参阅Office 编程中的常见任务For a list of topics that walk you through some of the common tasks in Excel projects, see Common tasks in Office programming.

请参阅See also