国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
OFFICE 的加載項(add in)(轉(zhuǎn))

OFFICE 的加載項(add in)(轉(zhuǎn))  

2012-04-23 12:01:18|  分類: MFC |字號 訂閱

原文:http://www.cnblogs.com/brooks-dotnet/archive/2010/03/01/1675324.html

一、加載項簡介

Office提供了多種用于擴(kuò)展Office應(yīng)用程序功能的模式,常見的有:

1、Office 自動化程序(Automation Executables)

2、Office加載項(COM or Excel Add-In)

3、Office文檔代碼或模板(Code Behind an Office Document or Template)

4、Office 智能標(biāo)簽(Smart Tags)

本次我們將學(xué)習(xí)使用VSTO 4.0編寫一個簡單的Office COM 加載項,構(gòu)建于Office 2010 x64.

示例代碼下載

本系列所有示例代碼均在 Visual Studio 2010 Ultimate RC 和 Office 2010 Professional Plus Beta x64 中測試通過。

二、為什么要使用加載項

Office加載項提供了一種擴(kuò)展應(yīng)用程序核心功能的機(jī)制,由此添加的功能可以在整個應(yīng)用程序或單個應(yīng)用程序中使用,極大的擴(kuò)充了Office的應(yīng)用領(lǐng)域:

1、擴(kuò)展現(xiàn)有功能:對于特定需求,特別是和業(yè)務(wù)緊密相關(guān)的需求,如果能夠以加載項的方式在Office中解決,那么將節(jié)省軟件成本、培訓(xùn)成本等。

2、數(shù)據(jù)整合:隨著Office由辦公平臺正式轉(zhuǎn)變?yōu)橐粋€計算平臺之后,將Office與其他平臺進(jìn)行整合的需求也變得愈來愈頻繁。如將Web服務(wù)器中數(shù)據(jù)導(dǎo)入到Excel中,將SAP系統(tǒng)中數(shù)據(jù)導(dǎo)入到Excel中,生成日報表等。

三、COM加載項的工作原理

要使用COM加載項,必須要在注冊表中寫入相應(yīng)的信息。Office 要判斷使用哪些加載項時,以Excel為例,需要查看注冊表中的兩個位置:

(PS:推薦一個注冊表編輯器Registry Workshop,功能很強大,支持64位,可以在這里下載)

1、HKEY_CURRENT_USER\Software\Microsoft\Office\Excel\AddIns

這個位置是針對于特定用戶的,也是推薦的位置。在該位置注冊的COM加載項將出現(xiàn)在Office中的COM加載項對話框中:

在該位置,有幾項是必備的。

1)FriendlyName:字符串值,包含了顯示在COM對話框中的COM加載項的名稱;

2)Description:字符串值,包含了COM加載項的簡短描述信息;

3)LoadBehavior:DWORD類型,用于描述COM加載項的加載方式,通常設(shè)置為3(1 + 2).


描述

0

斷開,不加載COM加載項

1

連接,加載 COM加載項

2

啟動時加載,主應(yīng)用程序啟動時加載并連接COM加載項

8

需要時加載,主應(yīng)用程序需要(觸發(fā)加載事件)時加載并連接COM加載項

16

首次連接,用戶注冊加載項后,首次運行主應(yīng)用程序時加載并連接COM加載項


除了上面的三個,還需要在HKEY_CLASSES_ROOT\CLSID下創(chuàng)建幾個注冊表項:

2、HKEY_LOCAL_MACHINE\Software\Microsoft\Office\Excel\AddIns

這個位置是針對于所有用戶的,但是這些加載項對用戶隱藏,即不會出現(xiàn)在COM加載項對話框中。

這些注冊表項如果純手工寫將會非常麻煩,而且容易出錯,幸運的是,Visual Studio提供了一組模板來方便的創(chuàng)建Office加載項,但是我們還是應(yīng)該理解這些注冊表項及其代表含義。

四、理解IDTExtensibility2接口

所有Office應(yīng)用程序都是用IDTExtensibility2接口與COM加載項進(jìn)行通信,該接口提供了一種通用的初始化機(jī)制,并具有在Office應(yīng)用程序的對象模型中傳遞數(shù)據(jù)的能力,因此COM加載項可以與Office應(yīng)用程序通信。

IDTExtensibility2接口并不復(fù)雜,但是對于標(biāo)注COM加載項的加載順序,以及它在何處影響我們編寫的代碼來說,此接口至關(guān)重要。Office在對COM加載項進(jìn)行實例化時,會創(chuàng)建主Connect類,注意不能用Connect的構(gòu)造函數(shù)創(chuàng)建類的實例,應(yīng)該用OnConnection方法;類似的,加載項的關(guān)閉不能調(diào)用析構(gòu)函數(shù),而要用OnDisconnection方法。

五、ExcelCOMAddInDemo

現(xiàn)在我們來動手開發(fā)一個簡單的Excel COM Add-In.

1、新建一個Share Add-In項目:

2、在彈出的項目向?qū)е?,點擊【Next】:

3、選擇你所熟悉的語言,點擊【Next】:

4、選擇COM 加載項的目標(biāo)宿主,點擊【Next】:

5、輸入Add-In的名稱和描述,點擊【Next】:

6、選擇"啟動時加載",點擊【Next】:

7、點擊【Finish】:

8、項目建立完成后會自動添加一個安裝項目,它會負(fù)責(zé)安裝生成的COM加載項,處理注冊表的信息等,無需我們手動參與。


64-bit Support and Code Compatibility

Office 2010 will ship in both 32- and 64-bit versions. 64-bit is particularly significant to some Excel applications, which hit a wall today in terms of available memory address space. This is an important topic in itself, which we have covered here.

Excel workbooks can be freely opened and edited in both 32- and 64-bit Excel; there is nothing architecture specific in a saved workbook. For custom code solutions, however, 64-bit Excel introduces some challenges:


ActiveX controls need to be ported – they need a 64-bit version to work in a 64-bit process. This includes Microsoft's own controls, of which many have been ported. We are evaluating the popularity and criticality of the remaining ones for possible porting.
COM add-ins, similarly, need to be compiled for 64-bit in order to work in 64-bit Excel.
XLL add-ins also need to be compiled for 64-bit, and the new Excel 2010 XLL SDK supports that.
VBA: Embedded VBA code gets re-compiled when the containing Excel workbook is opened on a new platform; 64-bit Excel now includes a 64-bit version of VBA. So most VBA code just works in 64-bit. However, a subset of VBA solutions needs some tweaking. It has to do with declarations and calls to external APIs with pointers/handles in the parameter list. VBA7, the new version of VBA that ships with Office 2010, supports the development of code that can run in both 32- and 64-bit Office.

摘自Excel 2010官方博客,可以看出,在Office 2010 x64中,必須編寫64位的COM Add-In.

這不是問題,稍后我們將看到如何編譯64位的COM Add-In和生成64位的安裝文件。

9、設(shè)置項目屬性:

在【Build】標(biāo)簽中,將【Platform】設(shè)置為:Active(x64),取消【Register for COM】的選擇:

在【Debug】標(biāo)簽中,將【Platform】設(shè)置為:Active(x64),同時將【Start Action】設(shè)置為Excel的安裝路徑,這樣做可以啟動Excel來調(diào)試COM Add-In:

配置編譯選項:

配置安裝項目屬性:

10、我們要做的是在Excel 的Ribbon中添加一個按鈕,點擊后彈出一則歡迎信息。

首先添加引用:

Microsoft.Office.Interop.Excel

System.Windows.Forms

打開Connect.cs,添加如下代碼:


代碼
namespace ExcelCOMAddInDemo
{
using System;
using Extensibility;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using System.Windows.Forms;
#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the ExcelCOMAddInDemoSetup project,
// right click the project in the Solution Explorer, then choose install.
#endregion
/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("DDC49E0C-03FE-4134-9829-65EF0351CECE"), ProgId("ExcelCOMAddInDemo.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
private Microsoft.Office.Interop.Excel.Application applicationObject;
private Microsoft.Office.Core.COMAddIn addInInstance;
private CommandBarButton simpleButton;
/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}
/// <summary>
/// Implements the OnConnection method of the IDTExtensibility2 interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application as Microsoft.Office.Interop.Excel.Application;
addInInstance = addInInst as Microsoft.Office.Core.COMAddIn;
}
/// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2 interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{
}
/// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}
/// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2 interface.
/// Receives notification that the host application has completed loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref System.Array custom)
{
CommandBars commandBars;
CommandBar standardBar;
commandBars = applicationObject.CommandBars;
// Get the standard CommandBar from Word
standardBar = commandBars["Standard"];
try
{
// try to reuse the button is hasn't already been deleted
simpleButton = (CommandBarButton)standardBar.Controls["Excel COM Addin"];
}
catch (System.Exception)
{
// If it's not there add a new button
simpleButton = (CommandBarButton)standardBar.Controls.Add(1);
simpleButton.Caption = "Excel COM Addin";
simpleButton.Style = MsoButtonStyle.msoButtonCaption;
}
// Make sure the button is visible
simpleButton.Visible = true;
simpleButton.Click += new _CommandBarButtonEvents_ClickEventHandler(simpleButton_Click);
standardBar = null;
commandBars = null;
}
/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2 interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
}
void simpleButton_Click(CommandBarButton ctrl, ref bool cancelDefault)
{
MessageBox.Show("Welcome to COM Add In");
}
}
}

聲明了一個CommandBarButton的實例,然后將其添加到CommandBar中,并關(guān)聯(lián)了一個Click事件,輸出一則歡迎信息。

11、編譯成功后,安裝生成的setup.exe,執(zhí)行安裝:

12、安裝成功后,打開Excel,會在【Add-In】中發(fā)現(xiàn)我們新添加的按鈕:

六、調(diào)試COM加載項

1、通過前面的設(shè)置為Excel啟動來進(jìn)行調(diào)試,這也是首先的方法,很方便。

2、可以通過附加Excel進(jìn)程的方式來進(jìn)行調(diào)試:

七、小結(jié)

本次主要學(xué)習(xí)了COM 加載項的開發(fā)流程,對使用托管代碼開發(fā)COM組件有了初步的認(rèn)識,尤其是IDTExtensibility2接口,在VSTO開發(fā)中占據(jù)重要地位,理解其與COM交互的過程是很有必要的。其次是64位COM加載項的部署問題,與32位傳統(tǒng)加載項有些區(qū)別,請注意區(qū)別對待。后續(xù)篇章會繼續(xù)深入介紹VSTO開發(fā)的內(nèi)容,及其與其他技術(shù)的整

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
HOWTO:使用VisualC#.NET生成OfficeCOM外接程序
使用VB快速制作WPS的COM加載項
打開Excel時卡卡卡,你該何去何從?
如何用C#開發(fā)Excel擴(kuò)展插件
用VBA編寫Excel加載宏
COM加載項編寫過程(Excel、以工具欄響應(yīng))
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服