Visual Studio Tools for Office: Using C# with Excel, Word, Outlook, and InfoPath【10】

The second part: .NET programming in Office
before two chapters of this book describes the Office object models and Office PIA. You also see how to use Visual Studio to build using VSTO function console application document, add-ins and code. The second part of the book more in-depth introduction to the Office object model. If you are only interested in the development of Excel, please read Chapter 3 to Chapter 5. If you are only interested in the development of Word, please read Chapter 6 to Chapter 9. If you are only interested in the development of Outlook, please read Chapter 7 to Chapter 11. If you are only interested in InfoPath development, read Chapter 12.

Chapter 3 Excel Programming

 

Custom method of Excel
Excel is the Office family are often programmed applications. Excel has a very rich object model, the object of which 196 have a combination of more than 4500 properties and methods. It supports a variety of models for integration code, including documentation and code add-ins. Most of these models originally designed to allow COM component to VB 6, VBA, C or C ++ of integration. However, through COM interop, written in C # or Visual Basic Management Objects can be disguised as a COM object and to participate in most of these models. This chapter introduces you to the code integration with Excel in several ways, and references to more in-depth discussion of its other sections of these methods. This chapter also discusses the creation of user-defined Excel functions, and introduced the Excel object model.

 

Performed automatically
as described in Chapter 2 "Office Solution Brief" in the most simple way is to start the integration with Excel Excel from a console application or Windows Forms applications, and automatically executed from an external program. Chapter 2 provides an example of the automatic execution of Word.

 

COM add-ins
Excel can load a COM add-in, it is a class of DLL implementation IDTExtensibility2 included. IDTExtensibility2 achieve class must be registered in the registry, so that it can be discovered and calls to other extensions like Excel COM add-ins the same.

 

COM add-ins are typically used to add Excel workbook can be used for any open application-level functionality. For example, you could write a COM add-in, it will add a menu item to the selected Excel worksheet cells according to the current exchange rate of currency into another currency.

 

Excel has a COM Add-Ins dialog box that allows users to open and close the COM add-ins. Note that selecting "Add-ons" from access via the "Tools" menu in the dialog box is not a COM Add-Ins dialog box. This dialog box is used to open and close the automation XLA add-ins and add-ons, which will be discussed later in this chapter. To access the COM Add-Ins dialog box, you must perform follow these steps:

 

1. Right-click the Excel menu or toolbar, then select "Custom" from the pop-up menu, or select "Custom" from the "Tools" menu to display the "Customize" dialog box.

 

 
"Command" 2. Click the "Customize" dialog tab.

 

 
3. Select Tools from the category list.

 

 
4. Scroll down the list of commands until you see a command COM add-in instructions.

 

 
5. Drag the COM Add-Ins command and place it on the toolbar.

 

 
6. Close the "Customize" dialog box.

 

 

 

After completing these steps, click COM add-ins that you added to a toolbar button on the toolbar. COM add-in dialog box shown in Figure 3-1.

 


Figure 3-1 Excel Add-Ins dialog box in COM

 

You can use the "Add" button to add the COM add-ins, and use the "Delete" button to delete them. Typically, you will not be able to use this dialog box to administer the COM add-ins. Instead, you install the COM add-in program items created with the registry settings to install and remove COM add-ins by using as.

Excel to find the COM add-ins installed by reading from the registry. You can go to the "Windows Start" menu and select "Run" to view the registry on your computer. In the "Run" dialog box, type regedit program you want to run, then click the "OK" button. Find Excel COM add-ins in the registry key \ \ Microsoft \ under HKEY_CURRENT_USER Software Office \ Excel \ Addins in. Excel also HKEY_LOCAL_MACHINE \ Software \ Microsoft \ registry entry under the Office \ Excel \ Addins find COM add-ins. COM add-ins that are registered under the HKEY_LOCAL_MACHINE COM add-ins will not appear in the COM Add-Ins dialog box, users can not open or close. We recommend that you do not register COM add-ins under HKEY_LOCAL_MACHINE, because it hides the user's COM add-ins.

COM add-ins are discussed in detail in Chapter 23, "Developing Word and Excel COM add-ins" in the.

Automation add-ins
automation add-in is registered in the registry is registered as a COM object class, open public functions they can be used in Excel formulas. Automation add-ins already installed is displayed in the Add-Ins dialog box, you can select "Add-ons" is displayed by the "Tools" menu. In discussing how to create user-defined functions for Excel Excel formulas, this chapter will be a detailed examination automation add-ins.

Visual Studio Tools for Office code,
Visual Studio 2005 Tools (VSTO) enables you to C # or Visual Basic code can be placed in Excel templates and workbooks after. VSTO is the basis for the design of C # and Visual Basicso, this model is a model for all the custom Excel's most ".NET" is. When you want to customize the behavior of a particular set of specific workbook workbook or generic template created you will use this model. Use the example, you can create an expense report to any person in your company expense reports to create a workbook template. The template can be added using commands and functions when it created the workbook is always available.

VSTO support on the back of the workbook code will be discussed in the third part of the book in detail.

Smart Document XML expansion pack and
intelligent document is another method code and Excel template or workbook associated. Intelligent document relies on the XML schema to a workbook or template, and the code associated with that pattern. And the associated code combination pattern called XML expansion pack. XML expansion pack can choose XML expansion pack associated with Excel workbooks by data from the menu in the XML menu. Figure 3-2 shows the XML Expansion Packs dialog box.

Figure 3-2 Excel in XML Expansion Packs dialog box 

 

When the XML expansion pack attached to the workbook, Excel loads the associated code and run it when you open the workbook. Smart document solutions can create custom user interface "Document operations" task pane. You can view the task pane in Excel by selecting Task Pane from the View menu. Figure 3-3 shows the Excel Custom Document Actions task pane.


Figure 3-3 Excel Custom Document Actions task pane

 

Can be written from scratch smart document solution in C # or Visual Basic. This book does not cover this method. Instead, the book focuses on the VSTO method, which aims to make the smart document development easier, and allows you to create custom Document Actions task pane using Windows Forms. Chapter 15, "Using the Actions pane," discusses this feature in more detail.

智能标签
智能标签启用弹出式菜单,其中包含与工作簿中识别的文本相关的操作。您可以通过创建智能标记DLL或在文档后面使用VSTO代码来控制Excel识别的文本以及对该文本可用的操作。

智能标记DLL包含Excel使用的两种类型的组件:识别器和关联操作。识别器确定工作簿中的哪些文本被识别为智能标记。一个动作对应于弹出菜单中显示的菜单命令。

可以创建一个识别器来告诉Excel识别股票代码符号(如MSFT股票代码),并显示一组可用于该符号的动作:买入,卖出,获取最新价格,获取历史等上。例如,“获取历史记录”操作可以启动Web浏览器来显示被识别的股票符号的股票历史网页。

识别器识别某些文本时,Excel会在相关单元格的右下角显示一个小三角形。如果用户将鼠标悬停在单元格上,则单元格旁边会出现一个弹出菜单图标,用户可以单击该菜单图标,以下载识别的文本文件的操作菜单。图3-4显示了一个示例菜单。选择一个动作后,Excel会调用相关联的操作来执行代码。


图3-4  Excel中的智能标签

 

智能标签从“自动更正”对话框的“智能标签”页面进行管理,如图3-5所示。 您可以通过从“工具”菜单中选择“自动更正选项”来显示“智能标记”页面。 在这里,用户可以打开和关闭单个识别器,以及控制与智能标签如何在工作簿中显示的其他选项。


图3-5  “自动更正”对话框中的“智能标记”页面

VSTO提供了一个简单的模型,用于创建在工作簿或模板级别工作的智能标记。第16章“使用VSTO中的智能标签”,更详细地介绍了使用智能标签的VSTO模型。

将Smart Tag识别器和动作类写入可在应用程序级别工作的DLL,但它比VSTO模型复杂得多。第16章还描述了这种方法。

XLA加载项
加载项对话框(通过从工具菜单中选择加载项显示)也是XLA加载项。 XLA加载项将生命当作一个在其背后具有VBA代码的工作簿。然后,开发人员可以使用“文件”菜单中的“另存为”将文件另存为XLA或Excel加载项文件,并选择XLA作为文件格式。 XLA文件作为一个不可见工作簿的形式的应用程序级加载项,在Excel的生命周期中保持打开状态。虽然可以将使用VSTO定制的工作簿保存为XLA文件,但是当工作簿转换为XLA文件时,VSTO的许多功能不起作用。一些不起作用的功能包括VSTO对“文档操作”任务窗格和“智能标记”的支持。因此,Microsoft不支持或建议将使用VSTO定制的工作簿保存为XLA文件。因此,这本书没有进一步介绍。

服务器生成的文档
VSTO使您可以在服务器上编写代码,使用数据填充Excel工作簿,而无需在服务器上启动Excel。例如,您可以创建一个ASP.NET页面,它从数据库中读取一些数据,然后将其放在Excel工作簿中,并将该工作簿返回给Web页面的客户端。 VSTO提供了一个名为ServerDocument的类,可以轻松实现。您还可以使用Office的XML文件格式在服务器上以XML格式生成Excel文档,但这要复杂得多。此外,Excel XML文件格式是有损的,这意味着您无法以Excel XML格式表示Excel电子表格中的所有内容。因此,在Excel XML文件格式的服务器上生成文档时,我们更喜欢使用ServerDocument方法。

第18章“服务器数据场景”介绍了使用ServerDocument在服务器上生成文档。

研究服务
Excel具有称为“研究”任务窗格的任务窗格,使您能够输入搜索词并搜索该搜索词的各种来源。图3-6显示了研究任务窗格。


图3-6 研究任务窗格

 

Excel使开发人员能够编写一个名为研究服务的特殊Web服务,实现由Excel定义的一组Web方法。 可以在Excel中注册研究服务,并在Office的研究任务窗格中使用。 例如,您可以编写一个在公司数据库中搜索搜索字词的研究服务。

第6章“编程词”讨论了更详细地创建研究服务。

Guess you like

Origin www.cnblogs.com/qiys/p/11367300.html