Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测 动画
当前位置: 主页 > vs2010教程 >

Visual C++2010 与office2010开发办公自动化(27)-实现Office晚期自动化绑定

时间:2010-08-06 18:01来源:未知 作者:admin 点击:

绑定
在使应用程序(如 Microsoft Office 应用程序)自动运行时,对 Office 应用程序对象的属性和方法的调用必须以某种方式连接到这些对象。将属性和方法调用连接到实现这些属性和方法的对象的过程通常称为绑定。在 Visual C++ 中,有两种类型的绑定,分别是早期绑定 和晚期绑定。所选择的绑定类型可以影响程序的许多方面,包括性能、灵活性和可维护性。

早期绑定
采用早期绑定时,Visual C++.net 使用有关所涉及的 Office 应用程序的可用类型信息直接绑定到它需要使用的方法或属性。编译器可以执行类型和语法检查,以确保传递到方法或属性的参数的数量和类型正确无误,并且返回的值是所期望的类型。由于早期绑定在运行时调用属性或方法所需的工作量较小,因此有时速度较快。然而,虽然早期绑定可能速度较快,但与晚期绑定之间的性能差异通常不大。


晚期绑定

与早期绑定不同,晚期绑定要等到运行时才会将属性和方法调用绑定到它们的对象。为此,目标对象必须实现一个特殊的 COM 接口:IDispatch。利用 IDispatch::GetIDsOfNames 方法,Visual C++.net可以询问对象支持哪些方法和属性,然后,IDispatch::Invoke 方法允许 Visual C++.net 调用这些方法和属性。这种晚期绑定的优点是:它消除了早期绑定所固有的某些版本依赖性。然而,它也有以下缺点:省略了对自动化代码完整性的编译时检查,也不提供“智能感知”功能(该功能可提供有助于正确调用方法和属性的提示)。

 好了,下面我来举例说明如何创建使用Office晚期自动化绑定的步骤:

1.启动VS2010。

2.创建一个CLR项目如下,在窗体中插入若干相关空间和文本,插入背景,详情参看下图:


3.在Form1.h中插入以下代码,详细见代码分析与注释
#pragma once  
 
 
namespace Yincheng {  
 
    using namespace System;  
    using namespace System::ComponentModel;  
    using namespace System::Collections;  
    using namespace System::Windows::Forms;  
    using namespace System::Data;  
    using namespace System::Drawing;  
 
    /// <summary>  
    /// Form1 摘要  
    ///  
    /// 警告: 如果更改此类的名称,则需要更改  
    ///          与此类所依赖的所有 .resx 文件关联的托管资源编译器工具的  
    ///          “资源文件名”属性。否则,  
    ///          设计器将不能与此窗体的关联  
    ///          本地化资源正确交互。  
    /// </summary>  
    public ref class Form1 : public System::Windows::Forms::Form  
    {  
    public:  
        Form1(void)  
        {  
            InitializeComponent();  
            //  
            //TODO: 在此处添加构造函数代码  
            //  
        }  
 
    protected:  
        /// <summary>  
        /// 清理所有正在使用的资源。  
        /// </summary>  
        ~Form1()  
        {  
            if (components)  
            {  
                delete components;  
            }  
        }  
    private: System::Windows::Forms::RichTextBox^  richTextBox1;  
    protected:   
    private: System::Windows::Forms::Button^  button1;  
    private: System::Windows::Forms::Button^  button2;  
 
    private:  
        /// <summary>  
        /// 必需的设计器变量。  
        /// </summary>  
        System::ComponentModel::Container ^components; 
 
#pragma region Windows Form Designer generated code  
        /// <summary>  
        /// 设计器支持所需的方法 - 不要  
        /// 使用代码编辑器修改此方法的内容。  
        /// </summary>  
        void InitializeComponent(void)  
        {  
            System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));  
            this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());  
            this->button1 = (gcnew System::Windows::Forms::Button());  
            this->button2 = (gcnew System::Windows::Forms::Button());  
            this->SuspendLayout();  
            //   
            // richTextBox1  
            //   
            this->richTextBox1->Location = System::Drawing::Point(104, 81);  
            this->richTextBox1->Name = L"richTextBox1";  
            this->richTextBox1->Size = System::Drawing::Size(388, 186);  
            this->richTextBox1->TabIndex = 0;  
            this->richTextBox1->Text = L"在使应用程序(如 Microsoft Office 应用程序)自动运行时,对 Office 应用程序对象的属性和方法的调用必须以某种方式连接到这些对象。将属性和方"   
                L"法调用连接到实现这些属性和方法的对象的过程通常称为绑定。在 Visual C++.net 中,有两种类型的绑定,分别是早期绑定 和晚期绑定。所选择的绑定类型可以"   
                L"影响程序的许多方面,包括性能、灵活性和可维护性。";  
            //   
            // button1  
            //   
            this->button1->Location = System::Drawing::Point(144, 276);  
            this->button1->Name = L"button1";  
            this->button1->Size = System::Drawing::Size(141, 38);  
            this->button1->TabIndex = 1;  
            this->button1->Text = L"启动Excel软件";  
            this->button1->UseVisualStyleBackColor = true;  
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);  
            //   
            // button2  
            //   
            this->button2->Location = System::Drawing::Point(313, 276);  
            this->button2->Name = L"button2";  
            this->button2->Size = System::Drawing::Size(141, 38);  
            this->button2->TabIndex = 2;  
            this->button2->Text = L"关闭应用程序";  
            this->button2->UseVisualStyleBackColor = true;  
            this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);  
            //   
            // Form1  
            //   
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);  
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;  
            this->BackColor = System::Drawing::SystemColors::ActiveCaptionText;  
            this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"$this.BackgroundImage")));  
            this->ClientSize = System::Drawing::Size(592, 386);  
            this->Controls->Add(this->button2);  
            this->Controls->Add(this->button1);  
            this->Controls->Add(this->richTextBox1);  
            this->Name = L"Form1";  
            this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;  
            this->Text = L"CSDN著名技术专家尹成-演示如何实现Office晚期自动化绑定";  
            this->ResumeLayout(false);  
 
        } 
#pragma endregion  
        //关闭应用程序  
    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {  
                 this->Close();  
             }  
     //启动Excel软件  
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {  
               Object^ MyApp;  
               Object^ MyBook;  
               Object^ MyBooks;  
               Object^ MySheet;  
               Object^ MySheets;  
               Object^ MyRange;  
               array<System::Object^,1>^ MyParameters;  
               array<System::Object^,1>^ MyArray;  
               String^ MyInfo;  
               System::Type^ MyClassType;  
               System::Reflection::Binder^ MyBinder;  
               try 
               {  
                // 获取Excel类型  
                   MyClassType=Type::GetTypeFromProgID("Excel.Application");  
                   MyApp=Activator::CreateInstance(MyClassType);  
                   //获取workbooks集合  
                   MyBooks=MyApp->GetType()->InvokeMember("Workbooks",System::Reflection::BindingFlags::GetProperty,MyBinder, MyApp,MyArray);  
                   //新增workbook.  
                   MyBook=MyBooks->GetType()->InvokeMember("Add",System::Reflection::BindingFlags::InvokeMethod,MyBinder, MyBooks,MyArray);  
                   //获取worksheets集合    
                   MySheets=MyBook->GetType()->InvokeMember("Worksheets",System::Reflection::BindingFlags::GetProperty,MyBinder, MyBook,MyArray);  
                   //获取第一个 worksheet.  
                   MyParameters=gcnew array<System::Object^,1>(1);  
                   MyParameters[0]=1;  
                   MySheet=MySheets->GetType()->InvokeMember("Item",System::Reflection::BindingFlags::GetProperty,MyBinder, MySheets, MyParameters );  
                   //获取A1单元格所在区域  
                   MyParameters=gcnew array<System::Object^,1>(2) ;  
                   MyParameters[0]="A1";  
                   MyParameters[1]= System::Reflection::Missing::Value;    
                   MyRange=MySheet->GetType()->InvokeMember("Range",System::Reflection::BindingFlags::GetProperty,MyBinder, MySheet, MyParameters );  
                   //在A1单元格中填充数据.  
                   MyParameters=gcnew array<System::Object^,1>(1) ;  
                   MyParameters[0]=this->richTextBox1->Text;     
                   MyRange->GetType()->InvokeMember("Value",System::Reflection::BindingFlags::SetProperty,MyBinder, MyRange, MyParameters );  
                   MyParameters=gcnew array<System::Object^,1>(1) ;  
                   //启动 Excel  
                   MyParameters[0]=true;   
                   MyApp->GetType()->InvokeMember("Visible",System::Reflection::BindingFlags::SetProperty,MyBinder, MyApp, MyParameters );    
                   MyApp->GetType()->InvokeMember("UserControl",System::Reflection::BindingFlags::SetProperty,MyBinder, MyApp, MyParameters );  
                }  
                catch (Exception^ MyEx)  
                {  
                    MessageBox::Show(MyEx->Message, "信息提示", MessageBoxButtons::OK, MessageBoxIcon::Information);  
                }  
             }  
    };  

5.启动调试运行如下:

点击”启动Excel软件“Excel中同步数据显示,如下所示:

 
(责任编辑:admin)
Tags:Visual C++2010 Office2010
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价