|
绑定 早期绑定
好了,下面我来举例说明如何创建使用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中同步数据显示,如下所示: ![]() |








骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价