设计和创建 Office 解决方案Design and create Office solutions

Visual Studio 提供可用于创建几种不同类型的 Office 解决方案的项目模板。Visual Studio provides project templates that you can use to create several different types of Office solutions. 文档的此部分将介绍项目模板和提供有关创建 Office 项目的指导。This section of the documentation describes the project templates and provides guidance about creating Office projects. 有关在创建项目后如何实现代码和用户界面自定义的信息,请参阅开发 Office 解决方案For information about how to implement code and user interface customizations after you have created your project, see Develop Office solutions.

适用于: 本主题中的信息适用于文档-级别项目和 VSTO 添加-在项目中。Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects. 请参阅按 Office 应用程序和项目类型提供的功能See Features available by Office application and project type.

Note

有兴趣开发跨多个平台扩展 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.

创建 Office 项目Create Office projects

在开始之前,应确定你的需求并找到最适合你的解决方案类型。Before you begin, you should determine your requirements and discover the type of solution that offers the best fit. 例如,如果每次使用应用程序时都必须运行 Office 解决方案,则 VSTO 外接程序最适合你的要求。For example, if your Office solution must run every time the application is used, a VSTO Add-in best fits your requirements. 如果代码与单个文档紧密集成,则可创建文档级自定义项。If the code is tightly integrated with a single document, create a document-level customization. 这些项目类型都可用作 Visual Studio 项目模板。These project types are available as Visual Studio project templates. 有关包含在 Visual Studio 中的 Office 项目模板的详细信息,请参阅office 项目模板概述For more information about the Office project templates that are included with Visual Studio, see Office project templates overview. 有关如何创建 Office 项目的详细信息,请参阅如何:在 Visual Studio 中创建 Office 项目For more information about how to create Office projects, see How to: Create Office projects in Visual Studio.

Office 项目具有的功能和项目项与 Visual Studio 中的其他类型的项目不同。Office projects have features and project items that are different from other types of projects in Visual Studio. 例如,当创建文档级项目时,项目中的文档和工作簿都可以在 Visual Studio 内部打开和编辑。For example, when you create a document-level project, the document or workbook in your project can be opened and edited inside Visual Studio. 有关详细信息,请参阅Visual Studio 环境中的 Office 项目For more information, see Office projects in the Visual Studio environment.

选择 .NET Framework 版本Choose a .NET Framework version

选择最适合你需求的项目类型之后,可以选择在开发过程中要使用的 .NET Framework 版本。After selecting the project type that best fits your requirements, you can choose which version of the .NET Framework to use in your development process. 你可以选择 Office 项目中的以下 .NET Framework 版本作为目标:You can target the following .NET Framework versions in Office projects:

  • .NET Framework 4.NET Framework 4

  • .NET Framework 4 Client Profile.NET Framework 4 Client Profile

  • .NET Framework 4.5.NET Framework 4.5

    要运行解决方案的最终用户计算机上需要为项目选择的 .NET Framework 版本。The .NET Framework version that you choose for your project is required on end-user computers for your solution to run. 例如,如果你的项目以 .NET Framework 4.NET Framework 4为目标,则在最终用户计算机上需要 .NET Framework 4.NET Framework 4For example, if your project targets the .NET Framework 4.NET Framework 4, the .NET Framework 4.NET Framework 4 is required on end-user computers. 在此示例中,如果最终用户计算机上仅安装了 .NET Framework 3.5,则解决方案将不会运行。In this example, your solution will not run if only the .NET Framework 3.5 is installed on end-user computers.

    如果迁移的 VSTO 外接程序项目面向 .NET Framework 3.5,则 Visual Studio 会将你项目的目标框架更改为 .NET Framework 4.NET Framework 4 或更高版本,具体取决于已安装的 Office 版本。If you migrate a VSTO Add-in project that targets the .NET Framework 3.5, Visual Studio changes the target framework of your project to .NET Framework 4.NET Framework 4 or later depending on the version of Office that you have installed.

    但是,在 Visual Studio 更改目标框架之后,你可能需要修改项目中的某些代码才能使用某些功能。However, after Visual Studio changes the target framework, you might need to modify some of the code in your project if it uses certain features. 有关如何更改目标框架的详细信息,请参阅如何:面向某个版本的 .NET FrameworkFor more information about how to change the target framework, see How to: Target a version of the .NET Framework. 有关你可能需要在项目中进行的更改的详细信息,请参阅将Office 解决方案迁移到 .NET Framework 4 或更高版本For more information about changes you might need to make in your project, see Migrate Office solutions to the .NET Framework 4 or later.

    如果 Visual Studio 更改项目的目标 .NET Framework 并且使用 ClickOnce 部署解决方案,请确保在 "系统必备" 对话框中选择相应的 .NET Framework 版本。If Visual Studio changes the target .NET Framework for your project and you are using ClickOnce to deploy your solution, make sure that you also select the corresponding version of the .NET Framework in the Prerequisites dialog box. 此选择不会在你更改项目的目标框架时自动更改。This selection does not change automatically when you change the target framework for your project. 有关详细信息,请参阅如何:在最终用户计算机上安装必备组件以运行 Office 解决方案For more information, see How to: Install prerequisites on end-user computers to run Office solutions.

Note

不能面向通过使用 Visual Studio 2013Visual Studio 2013 创建的 Office 项目中的 .NET Framework 3.5 或更早版本。You cannot target the .NET Framework 3.5 or earlier in Office projects that you create by using Visual Studio 2013Visual Studio 2013. 通过使用 Visual Studio 2013Visual Studio 2013 创建的 Office 项目需要在 .NET Framework 4 Client Profile.NET Framework 4 Client Profile 中首次引入的功能Office projects that you create by using Visual Studio 2013Visual Studio 2013 require features that were first introduced in the .NET Framework 4 Client Profile.NET Framework 4 Client Profile

了解最终用户计算机上需要 Office Pia 的时间Understand when the Office PIAs are required on end-user computers

默认情况下,如果项目中每个 Office PIA 引用的 "嵌入互操作类型" 属性设置为 " True" (默认值),则无需在最终用户计算机上安装 Office 主互操作程序集(pia)。By default, Office primary interop assemblies (PIAs) do not need to be installed on end-user computers if the Embed Interop Types property of each Office PIA reference in the project is set to True, which is the default value. 在本方案中,你的解决方案使用的 PIA 类型的类型信息会在生成项目时嵌入到解决方案程序集中。In this scenario, the type information for the PIA types that are used by your solution is embedded into the solution assembly when you build the project. 在运行时,使用嵌入的类型信息(而不是 Pia)来调入 Office 应用程序的基于 COM 的对象模型。At run time, the embedded type information is used instead of the PIAs to call into the Office application's COM-based object model. 有关如何在解决方案中嵌入 Pia 的类型的详细信息,请参阅类型等效性和嵌入的互操作类型For more information about how types from PIAs are embedded into your solution, see Type equivalence and embedded interop types.

如果项目中每个 Office PIA 引用的 "嵌入互操作类型" 属性设置为 " False",则必须在运行该解决方案的每个最终用户计算机上的全局程序集缓存中安装并注册 Office pia。If the Embed Interop Types property of each Office PIA reference in the project is set to False, Office PIAs must be installed and registered in the global assembly cache on each end-user computer that runs the solution. 在大多数情况下,PIA 会随 Office 一起默认安装,但你还可以将PIA 可再发行组件作为解决方案的必备组件包括进去。In most cases, the PIAs are installed by default with Office, but you can also include the PIA redistributable as a prerequisite for your solution. 有关详细信息,请参阅部署的 Office 解决方案必备组件For more information, see Office solution prerequisites for deployment.

了解客户端配置文件Understand the client profile

.NET Framework Client Profile 是完整版 .NET Framework 的子集。The .NET Framework Client Profile is a subset of the full .NET Framework. 如果只需使用 .NET Framework 中的客户端功能,并且想要为你的 Office 解决方案提供最快的部署体验,则可以面向 .NET Framework Client Profile。You can target the .NET Framework Client Profile if you need to use only the client features in the .NET Framework and you want to provide the fastest possible deployment experience for your Office solution. 有关详细信息,请参阅.NET Framework 客户端配置文件For more information, see .NET Framework client profile.

当创建面向 .NET Framework 4.NET Framework 4 的 Office 项目时,默认情况下,将面向 .NET Framework 4 Client Profile.NET Framework 4 Client ProfileWhen you create an Office project that targets the .NET Framework 4.NET Framework 4, the .NET Framework 4 Client Profile.NET Framework 4 Client Profile is targeted by default. 如果要开发完整 .NET Framework 4.NET Framework 4,则必须在创建项目后设置此选项。If you want to develop for the full .NET Framework 4.NET Framework 4, you must set this option after the project is created. 有关详细信息,请参阅如何:面向 .NET Framework 的某个版本For more information, see How to: Target a version of the .NET Framework.

为64位版本的 Microsoft Office 创建解决方案Create solutions for the 64-bit edition of Microsoft Office

Microsoft Office 可提供 64 位和 32 位版本。Microsoft Office is available in 64-bit and 32-bit editions. 若要创建可在任一版本中运行的 Office 解决方案,必须将项目的 "平台目标" 设置设置为 "任何 CPU"。To create Office solutions that can run in either edition, the platform target setting for your project must be set to Any CPU. 这是 Office 项目的默认值。This is the default value for Office projects. 有关详细信息,请参阅生成 Office 解决方案For more information, see Build Office solutions.

存在供 64 位和 32 位版本的 Microsoft Office 使用的单独的 64 位和 32 位版本的 Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtimeThere are separate 64-bit and 32-bit versions of the Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime that are used by the 64-bit and 32-bit editions of Microsoft Office. 有关详细信息,请参阅Visual Studio Tools for Office 运行时概述For more information, see Visual Studio Tools for Office runtime overview.

Office 解决方案中的程序集Assemblies in Office solutions

当使用 Visual Studio 中的 Office 开发工具创建 Office 项目时,你编写的代码最终将被编译成程序集。When you create an Office project by using the Office development tools in Visual Studio, the code that you write is eventually compiled into an assembly. 该程序集将部署到共享服务器或客户端计算机上的目录中。The assembly is deployed to a shared server or to a directory on the client computer.

Office 解决方案中的程序集通过 Office 应用程序加载。Assemblies in Office solutions are loaded by an Office application. 加载程序集之后,程序集中的代码可以响应在应用程序中引发的事件(例如,用户单击菜单项时)。After the assembly is loaded, code in the assembly can respond to events that are raised in the application, for example, when a user clicks a menu item. 程序集中的代码也可以调入对象模型,以便自动处理和扩展应用程序,并且它可以使用 .NET Framework.NET Framework 中的任何类。Code in the assembly can also call into the object model to automate and extend the application, and it can use any of the classes in the .NET Framework.NET Framework. 有关详细信息,请参阅文档级自定义项的体系结构VSTO 外接程序的体系结构For more information, see Architecture of document-level customizations and Architecture of VSTO Add-ins.

Office 解决方案使用部署清单和应用程序清单来标识程序集。Office solutions use deployment manifests and application manifests to identify the assembly. 清单包含有关程序集的名称、版本和位置信息,因此,应用程序可以查找、链接到并运行正确的程序集。The manifests contain information about the assembly's name, version, and location, so that the application can find, link to, and run the correct assembly. 有关详细信息,请参阅Office 解决方案中的应用程序和部署清单For more information, see Application and deployment manifests in Office solutions.

文档级项目包括一个文档和一个程序集。Document-level projects include a document in addition to an assembly. 该文档可充当应用程序的前端,是所有用户发生交互的地方。The document acts as the front end of the application and is where all user interaction takes place. 每个文档只能包含一个与之相关的主项目程序集;但是,多个文档可以指向同一个程序集。Each document can have only one main project assembly associated with it; however, multiple documents can point to the same assembly.

文档级项目中的程序集不会嵌入到文档;而是存储到其他位置,可通过文档的应用程序清单进行标识。Assemblies in document-level projects are not embedded in the document; instead, they are stored elsewhere and are identified by the document's application manifest.

程序集的安全注意事项Security considerations for assemblies

对于要在一台计算机上运行 Office 解决方案,必须信任该解决方案使用的程序集才能运行。For an Office solution to run on a computer, the assemblies used by the solution must be trusted to run. 有关安全性的详细信息,请参阅保护 Office 解决方案的安全For more information about security, see Secure Office solutions.

默认情况下,解决方案程序集和项目输出文件夹中任何引用的程序集是受信任的,以便在生成项目时在开发计算机上运行。By default, the solution assembly and any referenced assemblies that are in your project's output folder are trusted to run on the development computer when you build the project. 有关详细信息,请参阅生成 Office 解决方案For more information, see Build Office solutions.

出于安全考虑,最好是在本地计算机上创建项目,而不是在共享位置进行开发。For security reasons, it is best to create projects on your local computer, rather than developing on a shared location. 有关详细信息,请参阅Office 解决方案的协作开发For more information, see Collaborative development of Office solutions.

引用的程序集Referenced assemblies

该程序集可以引用项目引用中列出的其他程序集。The assembly can reference other assemblies, which are listed in the project's references. 但是,一个文档级项目程序集不能引用另一个文档级项目程序集。However, one document-level project assembly cannot reference another document-level project assembly.

请参阅See also