基于Visual C++2010 与office2010开发办公自动化(18)- 如何使用自动化导出Word文件

VS2010新特性:

基于Visual C++2010 与office2010开发办公自动化(18)- 如何使用自动化导出Word文件_第1张图片

 

  Office Word 2010
  Office Word 2010 增强了 Navigation Pane 特性,用户可在 Navigation Pane 中快速切换至任何一章节的开头(根据标题样式判断),同时也可在输入框中进行即时搜索,包含关键词的章节标题会高亮显示。例如
  Office Word 2010 也增加了在线实时协作功能,用户可以从 Office Word Web App 中启动 Word 2010 进行在线文档的编辑,并可在左下角看到同时编辑的其他用户(包括其他联系方式、IM 等信息,需要 Office Communicator)。而当其他用户修改了某处后,Word 2010 会提醒当前用户进行同步。(注:此功能也存在于部分其他 Office 2010 程序中)
  另外,用户可在 Office Word 2010, Office PowerPoint 2010 以及 Office OneNote 2010 等 Office 2010 程序中直接插入其他正在运行的软件的截图。

 

基于Visual C++2010 与office2010开发办公自动化(18)- 如何使用自动化导出Word文件_第2张图片

如果在我们的程序中能够把我们需求的内容,表格自动生动WORD一份文件,图/表/文/排版并茂,如此大大缓解我们工作量,有其是在日常大量重复的功能流程中更是顺畅,也大大降低了成本。

下面来用vc++2010实现在应用程序中导出一个简单的表格的office2010文档的功能流程。

 

1.启动VS2010

 基于Visual C++2010 与office2010开发办公自动化(18)- 如何使用自动化导出Word文件_第3张图片

2.创建一个CLR项目如下,界面控件布局见下图:

基于Visual C++2010 与office2010开发办公自动化(18)- 如何使用自动化导出Word文件_第4张图片

 

3.添加下列引用

基于Visual C++2010 与office2010开发办公自动化(18)- 如何使用自动化导出Word文件_第5张图片

4.在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; using namespace System::Data::OleDb; using namespace System::Reflection; /// <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::Data::DataSet^ dataSet1; protected: private: System::Data::OleDb::OleDbCommand^ oleDbCommand1; private: System::Data::OleDb::OleDbConnection^ oleDbConnection1; private: System::Data::OleDb::OleDbCommand^ oleDbSelectCommand1; private: System::Data::OleDb::OleDbCommand^ oleDbInsertCommand1; private: System::Data::OleDb::OleDbCommand^ oleDbUpdateCommand1; private: System::Data::OleDb::OleDbCommand^ oleDbDeleteCommand1; private: System::Data::OleDb::OleDbDataAdapter^ oleDbDataAdapter1; private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::TextBox^ textBox1; private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::Button^ button2; private: System::Windows::Forms::DataGridView^ dataGridView1; 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->dataSet1 = (gcnew System::Data::DataSet()); this->oleDbCommand1 = (gcnew System::Data::OleDb::OleDbCommand()); this->oleDbConnection1 = (gcnew System::Data::OleDb::OleDbConnection()); this->oleDbSelectCommand1 = (gcnew System::Data::OleDb::OleDbCommand()); this->oleDbInsertCommand1 = (gcnew System::Data::OleDb::OleDbCommand()); this->oleDbUpdateCommand1 = (gcnew System::Data::OleDb::OleDbCommand()); this->oleDbDeleteCommand1 = (gcnew System::Data::OleDb::OleDbCommand()); this->oleDbDataAdapter1 = (gcnew System::Data::OleDb::OleDbDataAdapter()); this->label1 = (gcnew System::Windows::Forms::Label()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->button1 = (gcnew System::Windows::Forms::Button()); this->button2 = (gcnew System::Windows::Forms::Button()); this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView()); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataSet1))->BeginInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView1))->BeginInit(); this->SuspendLayout(); // // dataSet1 // this->dataSet1->DataSetName = L"NewDataSet"; // // oleDbDataAdapter1 // this->oleDbDataAdapter1->DeleteCommand = this->oleDbDeleteCommand1; this->oleDbDataAdapter1->InsertCommand = this->oleDbInsertCommand1; this->oleDbDataAdapter1->SelectCommand = this->oleDbSelectCommand1; this->oleDbDataAdapter1->UpdateCommand = this->oleDbUpdateCommand1; // // label1 // this->label1->AutoSize = true; this->label1->ForeColor = System::Drawing::SystemColors::ButtonFace; this->label1->Location = System::Drawing::Point(64, 61); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(83, 12); this->label1->TabIndex = 0; this->label1->Text = L"SQL查询语句:"; // // textBox1 // this->textBox1->Location = System::Drawing::Point(151, 58); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(305, 21); this->textBox1->TabIndex = 1; this->textBox1->Text = L"Select top 10 客户ID,公司名称,地址,电话 From 客户"; // // button1 // this->button1->Location = System::Drawing::Point(462, 57); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(67, 23); this->button1->TabIndex = 2; this->button1->Text = L"显示数据"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // button2 // this->button2->Location = System::Drawing::Point(535, 58); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(78, 23); this->button2->TabIndex = 3; this->button2->Text = L"导出Word表"; this->button2->UseVisualStyleBackColor = true; this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click); // // dataGridView1 // this->dataGridView1->BackgroundColor = System::Drawing::SystemColors::ButtonHighlight; this->dataGridView1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D; this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize; this->dataGridView1->Location = System::Drawing::Point(60, 84); this->dataGridView1->Name = L"dataGridView1"; this->dataGridView1->RowTemplate->Height = 23; this->dataGridView1->Size = System::Drawing::Size(553, 267); this->dataGridView1->TabIndex = 4; // // 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(677, 451); this->Controls->Add(this->dataGridView1); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Controls->Add(this->textBox1); this->Controls->Add(this->label1); this->Name = L"Form1"; this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen; this->Text = L"CSDN著名技术专家尹成-演示使用自动化导出Word文件"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataSet1))->EndInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView1))->EndInit(); this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion //显示数据 private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { try { this->dataSet1 = gcnew DataSet(); if (this->oleDbConnection1->State == ConnectionState::Open) this->oleDbConnection1->Close(); this->oleDbConnection1->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb"; this->oleDbConnection1->Open(); String^ MySQL = this->textBox1->Text; this->oleDbCommand1->CommandText=MySQL; this->oleDbCommand1->Connection=this->oleDbConnection1; this->oleDbDataAdapter1->SelectCommand=this->oleDbCommand1; this->oleDbDataAdapter1->Fill(this->dataSet1); this->dataGridView1->DataSource =this->dataSet1->Tables[0]; } catch (Exception^ MyEx) { MessageBox::Show(MyEx->Message, "信息提示", MessageBoxButtons::OK, MessageBoxIcon::Information); } } //导出Word表 private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { Microsoft::Office::Interop::Word::ApplicationClass^ MyWord; Microsoft::Office::Interop::Word::_Document^ MyDoc; Microsoft::Office::Interop::Word::Selection^ MySelection; Microsoft::Office::Interop::Word::Table^ MyTable; array<System::Object^,2>^ MyData=gcnew array<System::Object^,2>(5000,30); OleDbDataReader^ MyReader; System::Object^ MyObj; int MyRows, MyColumns, i, j; try { MyObj = Missing::Value; MyWord = gcnew Microsoft::Office::Interop::Word::ApplicationClass(); MyWord->Visible = true; MyDoc=MyWord->Documents->Add(MyObj, MyObj, MyObj, MyObj); MyDoc->Select(); MySelection = MyWord->Selection; MyRows = this->dataSet1->Tables[0]->Rows->Count; MyColumns = this->dataSet1->Tables[0]->Columns->Count; MyTable = MyDoc->Tables->Add(MySelection->Range, MyRows + 1, MyColumns,MyObj, MyObj); //设置列宽 for (i = 1; i < MyColumns + 1; i++) { MyTable->Columns[i]->SetWidth(110, Microsoft::Office::Interop::Word::WdRulerStyle::wdAdjustNone); } //设置第一行的背景颜色 MyTable->Rows[1]->Cells->Shading->BackgroundPatternColorIndex =Microsoft::Office::Interop::Word::WdColorIndex::wdGray25; //设置第一行的字体 MyTable->Rows[1]->default->Bold= 1; MyReader = this->oleDbCommand1->ExecuteReader(); //输出列标题数据 for(i=0;i<this->dataSet1->Tables[0]->Columns->Count;i++) { MyDoc->Tables[1]->Cell(1,i+1)->default->InsertAfter(this->dataSet1->Tables[0]->Columns[i]->ColumnName); } //输出数据库记录 j=2; while(MyReader->Read()) { for(i=0;i<MyReader->FieldCount;i++) { MyDoc->Tables[1]->Cell(j,i+1)->default->InsertAfter(MyReader[i]->ToString()); } j=j+1; } this->oleDbConnection1->Close(); } catch (Exception^ MyEx) { MessageBox::Show(MyEx->Message, "信息提示", MessageBoxButtons::OK, MessageBoxIcon::Information); } } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { } }; }  

5.启动调试运行如下:

基于Visual C++2010 与office2010开发办公自动化(18)- 如何使用自动化导出Word文件_第6张图片

查询显示数据如下:

基于Visual C++2010 与office2010开发办公自动化(18)- 如何使用自动化导出Word文件_第7张图片

点击”导出word表“按钮启动word,显示如下,word中自动排版了程序中的表格:

基于Visual C++2010 与office2010开发办公自动化(18)- 如何使用自动化导出Word文件_第8张图片

 

 

你可能感兴趣的:(C++,Office,System,2010,Forms,Interop)