天天看點

基于Visual C++2010 與office2010開發辦公自動化(26)-如何使用自動化輸出Excel查詢表

VS2010新特性:

基于Visual C++2010 與office2010開發辦公自動化(26)-如何使用自動化輸出Excel查詢表

  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 程式中直接插入其他正在運作的軟體的截圖。

Office 2010,是微軟推出新一代辦公軟體,開發代号為Office 14,實際是第12個發行版。該軟體共有6個版本,分别是初級版、家庭及學生版、家庭及商業版、标準版、專業版和專業進階版,此外還推出Office 2010免費版本,其中僅包括Word和Excel應用。除了完整版以外,微軟還将釋出針對Office 2007的更新版Office 2010。Office 2010可支援32位和64位vista及Windows7,僅支援32位WindowsXP,不支援64位XP。

基于Visual C++2010 與office2010開發辦公自動化(26)-如何使用自動化輸出Excel查詢表

Office 2010所包括的全部應用有,Microsoft Access 2010,Microsoft Excel 2010;Microsoft InfoPath Designer 2010;Microsoft InfoPath Filler 2010;Microsoft OneNote 2010;Microsoft Outlook 2010;Microsoft PowerPoint 2010;Microsoft Publisher 2010;Microsoft SharePoint Workspace 2010;Microsoft Word 2010;Office Communicator 2010等。

基于Visual C++2010 與office2010開發辦公自動化(26)-如何使用自動化輸出Excel查詢表

新增功能

  1、截屏工具

  新元件:sharepoint 的加載狀态Windows Vista就自帶了一個簡單的截屏工具,Office2010的Word、PowerPoint等元件裡也增加了這個非常有用的功能,在插入标簽裡可以找到(Screenshot),支援多種截圖模式,特别是會自動緩存目前打開視窗的截圖,點選一下滑鼠就能插入文檔中。

  2、背景移除工具(Background Removal)

  可以在Word的圖檔工具下或者圖檔屬性菜單裡找到,在執行簡單的摳圖操作時就無需動用Photoshop了,還可以添加、去除水印。

  3、保護模式(Protected Mode)

  如果打開從網絡上下載下傳的文檔,Word 2010會自動處于保護模式下,預設禁止編輯,想要修改就得點一下啟用編輯(Enble Editing)。大概是出于版權保護的考慮吧。

  4、新的SmartArt模闆

  SmarArt是Office 2007引入的一個很酷的功能,可以輕松制作出精美的業務流程圖,而Office 2010在現有類别下增加了大量新模闆,還新添了數個新的類别。

  5、作者許可(Author Permissions)

  線上協作是Office 2010的重點努力方向,也符合當今辦公趨勢。Office 2007裡審閱标簽下的保護文檔現在變成了限制編輯(Restrict Editing),旁邊還增加了阻止作者(Block Authors)。

  6、Office按鈕文檔選項

  Office 2007左上角的圓形按扭及其下的菜單讓人印象深刻,到了Office 2010裡功能更豐富了,特别是文檔操作方面,比如在文檔中插入中繼資料、快速通路權限、儲存文檔到SharePoint位置等等。

  7、Office按鈕列印選項

  列印部分此前隻有寥寥三個選項,現在幾乎成了一個控制台,基本可以完成所有列印操作。

  8、Outlook 2010 Jumplist

  Jumplist是Windows 7工作列的新特性,Outlook 2010也得到了支援,可以迅速通路預約、聯系人、任務、月曆等功能。

基于Visual C++2010 與office2010開發辦公自動化(26)-如何使用自動化輸出Excel查詢表

  1.全新的安全政策

  Office2010的開發團隊給出了一套新的安全戰略,通過分析過去的漏洞來了解漏洞是如何被利用的,還要如何做才能防止今後出現類似的情況。Office2010基于五個基本準則建立了新的多層次的安全模型,這是任何一個開發者都應該記住的:

  在執行之前驗證所有使用者輸入

  查找随機缺陷和不典型使用案例

  不要相信第三方庫

  不要把安全措施推給使用者

  選擇降一級的使用者體驗,而不是全盤否定

  此外,Office2010中還包含一系列的安全性改進,如加強密碼的複雜性、檢視下載下傳檔案的保護模式、對Outlook中電子郵件線程更好的控制等。

  2.Office2010中的程式設計代碼有所改變

  對開發者來講,為了支援64位,VBA(應用程式的可視化基礎)進行了更新,而Office2010對象模型也已經得到了更新。是以,當企業使用者自Office2003更新至Office2010的時候,很有可能需要處理舊的、不相容的程式代碼。

  在Office2010中,Outlook可能是受到影響最大的應用程式,對象模型的變化導緻了MAPI接口界面以及導航視窗的調整。

  如果企業使用者不想要因為無效代碼而導緻業務中斷的話,那麼關鍵就是對代碼相容性進行計劃和測試。

  3.強調雲共享功能,提升協同作業效率

  新版Office2010的雲共享功能包括跟企業SharePoint伺服器的整合,讓PowerPoint、Word、Excell等Office檔案皆可通過SharePoint平台,同時間供多位員工編輯、浏覽,提升檔案協同作業效率。

  微軟還增強了Office與其他微軟通信服務之間的聯系。如果使用者通過Office2010來使用OfficeCommunicationsServer2007R2和OfficeCommunicator2007R2,就可以看到同僚的狀态并通過電子郵件和即時通信與之舉得聯系。SharePoint與Office的聯系更為緊密,并允許使用者對Office文檔進行協作。

  4. 新增的Project 2010

  Project2010中的複制粘貼功能也得到了完善,你可以複制粘貼完整格式的内容,任務增至11個,還支援自動換行。在Project2010中可以自動或手動制訂任務,自動制訂任務功能和之前版本相同,手動制動的任務自動被配置設定有圖釘按鈕,預設不會變動。

  新增的TeamPlanner允許使用者輕松地檢視團隊的工作随着時間的變動,快速找出問題,通過拖放等操作解決問題。

  Project2010還新增了TimelineView,使用者可以通過該功能總結你的項目然後和其它Office應用分享這一時間線,使用者還可以直接将其複制粘貼到其它Office應用比如Outlook或是PowerPoint中。

  5. 子產品産品新增功能

基于Visual C++2010 與office2010開發辦公自動化(26)-如何使用自動化輸出Excel查詢表

  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 程式中直接插入其他正在運作的軟體的截圖。

  Office Excel 2010

  Office Excel 2010 新增了 Sparklines 特性,可根據使用者選擇的資料直接在單元格内畫出折線圖、柱狀圖等,并配有 Sparklines 設計面闆供自定義樣式。

  Office PowerPoint 2010

  Office PowerPoint 2010 除了新增更多幻燈片切換特效、圖檔處理特效之外,還增加了更多視訊功能,使用者可直接在 PowerPoint 2010 中設定(調節)開始和終止時間剪輯視訊,也可将視訊嵌入之 PowerPoint 檔案中。

  PowerPoint 2010 左側的幻燈片面闆也新增了分區特性,使用者可将幻燈片分區歸類,也可對整個區内的所有幻燈片進行操作。見下圖中的 Default Section 和 LiveSino Section。

  PowerPoint 2010 也增加了類似格式刷的工具 – Animation Painter,可将動畫效果應用至其他對象,用法同格式刷。另外就是上次我提到的 Broadcast Slide Show 線上幻燈片播放功能。

  Office Web Applications 2010

   Office Web Applications 2010 包含 Word Web App, Excel Web App, PowerPoint Web App 以及 OneNote Web App,支援簡單的編輯及實時協作功能,支援 IE, Firefox, 和 Safari

  Office Mobile 2010

  Office Mobile 2010 包含新版 Word Mobile, Excel Mobile, PowerPoint Mobile, OneNote Mobile 以及支援對話模式的 Outlook Mobile

基于Visual C++2010 與office2010開發辦公自動化(26)-如何使用自動化輸出Excel查詢表

 好了,現在我再簡單的示範一下如何使用自動化輸出Excel查詢表的大概流程,僅作引導,别無它意。

1.啟動VS2010

基于Visual C++2010 與office2010開發辦公自動化(26)-如何使用自動化輸出Excel查詢表

2.建立一個CLR項目如下,在窗體中插入若幹相關空間和文本,插入背景,詳情參看下圖:

基于Visual C++2010 與office2010開發辦公自動化(26)-如何使用自動化輸出Excel查詢表

3.添加下列引用

基于Visual C++2010 與office2010開發辦公自動化(26)-如何使用自動化輸出Excel查詢表

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;

/// <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::OpenFileDialog^ openFileDialog1;

protected:

private: System::Windows::Forms::SaveFileDialog^ saveFileDialog1;

private: System::Windows::Forms::Label^ label1;

private: System::Windows::Forms::Button^ button1;

private: System::Windows::Forms::TextBox^ textBox1;

private: System::Windows::Forms::TextBox^ textBox2;

private: System::Windows::Forms::Button^ button2;

private: System::Windows::Forms::Label^ label2;

private: System::Windows::Forms::TextBox^ textBox3;

private: System::Windows::Forms::Button^ button3;

private: System::Windows::Forms::Label^ label3;

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->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());

this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());

this->label1 = (gcnew System::Windows::Forms::Label());

this->button1 = (gcnew System::Windows::Forms::Button());

this->textBox1 = (gcnew System::Windows::Forms::TextBox());

this->textBox2 = (gcnew System::Windows::Forms::TextBox());

this->button2 = (gcnew System::Windows::Forms::Button());

this->label2 = (gcnew System::Windows::Forms::Label());

this->textBox3 = (gcnew System::Windows::Forms::TextBox());

this->button3 = (gcnew System::Windows::Forms::Button());

this->label3 = (gcnew System::Windows::Forms::Label());

this->SuspendLayout();

//

// openFileDialog1

//

this->openFileDialog1->FileName = L"openFileDialog1";

//

// label1

//

this->label1->AutoSize = true;

this->label1->BackColor = System::Drawing::Color::Transparent;

this->label1->ForeColor = System::Drawing::Color::Lime;

this->label1->Location = System::Drawing::Point(174, 131);

this->label1->Name = L"label1";

this->label1->Size = System::Drawing::Size(149, 12);

this->label1->TabIndex = 0;

this->label1->Text = L"Access資料庫檔案全路徑:";

//

// button1

//

this->button1->Location = System::Drawing::Point(397, 126);

this->button1->Name = L"button1";

this->button1->Size = System::Drawing::Size(142, 23);

this->button1->TabIndex = 1;

this->button1->Text = L"浏覽Access檔案";

this->button1->UseVisualStyleBackColor = true;

this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);

//

// textBox1

//

this->textBox1->Location = System::Drawing::Point(174, 156);

this->textBox1->Name = L"textBox1";

this->textBox1->Size = System::Drawing::Size(365, 21);

this->textBox1->TabIndex = 2;

//

// textBox2

//

this->textBox2->Location = System::Drawing::Point(174, 220);

this->textBox2->Name = L"textBox2";

this->textBox2->Size = System::Drawing::Size(365, 21);

this->textBox2->TabIndex = 5;

//

// button2

//

this->button2->Location = System::Drawing::Point(397, 190);

this->button2->Name = L"button2";

this->button2->Size = System::Drawing::Size(142, 23);

this->button2->TabIndex = 4;

this->button2->Text = L"浏覽Excel檔案";

this->button2->UseVisualStyleBackColor = true;

this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);

//

// label2

//

this->label2->AutoSize = true;

this->label2->BackColor = System::Drawing::Color::Transparent;

this->label2->ForeColor = System::Drawing::Color::Lime;

this->label2->Location = System::Drawing::Point(174, 195);

this->label2->Name = L"label2";

this->label2->Size = System::Drawing::Size(131, 12);

this->label2->TabIndex = 3;

this->label2->Text = L"Excel導出檔案全路徑:";

//

// textBox3

//

this->textBox3->Location = System::Drawing::Point(174, 286);

this->textBox3->Name = L"textBox3";

this->textBox3->Size = System::Drawing::Size(365, 21);

this->textBox3->TabIndex = 8;

//

// button3

//

this->button3->Location = System::Drawing::Point(397, 256);

this->button3->Name = L"button3";

this->button3->Size = System::Drawing::Size(142, 23);

this->button3->TabIndex = 7;

this->button3->Text = L"輸出查詢表到Excel檔案";

this->button3->UseVisualStyleBackColor = true;

this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);

//

// label3

//

this->label3->AutoSize = true;

this->label3->BackColor = System::Drawing::Color::Transparent;

this->label3->ForeColor = System::Drawing::Color::Lime;

this->label3->Location = System::Drawing::Point(174, 261);

this->label3->Name = L"label3";

this->label3->Size = System::Drawing::Size(155, 12);

this->label3->TabIndex = 6;

this->label3->Text = L"輸出資料表的SQL查詢語句:";

//

// 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(661, 407);

this->Controls->Add(this->textBox3);

this->Controls->Add(this->button3);

this->Controls->Add(this->label3);

this->Controls->Add(this->textBox2);

this->Controls->Add(this->button2);

this->Controls->Add(this->label2);

this->Controls->Add(this->textBox1);

this->Controls->Add(this->button1);

this->Controls->Add(this->label1);

this->Name = L"Form1";

this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;

this->Text = L"CSDN著名技術專家尹成-示範使用自動化輸出Excel查詢表";

this->ResumeLayout(false);

this->PerformLayout();

}

#pragma endregion

//浏覽Access檔案

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

this->openFileDialog1->Filter="所有Access類型檔案(*.mdb)|*.mdb";

if(this->openFileDialog1->ShowDialog()==System::Windows::Forms::DialogResult::OK)

{

this->textBox1->Text=this->openFileDialog1->FileName;

}

}

//浏覽Excel檔案

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {

this->saveFileDialog1->Filter="所有Excel類型檔案(*.xls)|*.xls";

if(this->saveFileDialog1->ShowDialog()==System::Windows::Forms::DialogResult::OK)

{

this->textBox2->Text=this->saveFileDialog1->FileName;

}

}

//輸出查詢表到Excel檔案

private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {

String^ MyExcelFile = this->textBox2->Text;

String^ MyAccessFile = this->textBox1->Text;

String^ MySQL = this->textBox3->Text;

Excel::ApplicationClass^ MyExcel;

Excel::Workbooks^ MyBooks;

Excel::_Workbook^ MyBook;

Excel::_Worksheet^ MySheet;

Excel::Range^ MyRange;

Excel::QueryTables^ MyQueryTables;

Excel::_QueryTable^ MyQueryTable;

if (!System::IO::File::Exists(MyAccessFile))

{

MessageBox::Show(MyAccessFile + "檔案不存在!", "資訊提示", MessageBoxButtons::OK, MessageBoxIcon::Information);

return;

}

try

{

//建立一個新的工作簿

MyExcel=gcnew Excel::ApplicationClass();

MyBooks=MyExcel->Workbooks;

MyBook=MyBooks->Add(System::Reflection::Missing::Value);

//在A1單元格中建立一個QueryTable

MySheet=(Excel::Worksheet^)MyBook->Worksheets[1];

MyRange=MySheet->Range["A1",System::Reflection::Missing::Value];

MyQueryTables=MySheet->QueryTables;

MyQueryTable=(Excel::_QueryTable^)MyQueryTables->Add("OLEDB;Provider= Microsoft.Jet.OLEDB.4.0;Data Source="+MyAccessFile+";",MyRange, MySQL);

MyQueryTable->RefreshStyle=Excel::XlCellInsertionMode::xlInsertEntireRows;

MyQueryTable->Refresh(false);

//儲存結果

MyBook->SaveAs(MyExcelFile, System::Reflection::Missing::Value, System::Reflection::Missing::Value,System::Reflection::Missing::Value, System::Reflection::Missing::Value, System::Reflection::Missing::Value, Excel::XlSaveAsAccessMode::xlNoChange, System::Reflection::Missing::Value, System::Reflection::Missing::Value,System::Reflection::Missing::Value, System::Reflection::Missing::Value, System::Reflection::Missing::Value);

MyBook->Close(false, System::Reflection::Missing::Value, System::Reflection::Missing::Value);

MyExcel->Quit();

MessageBox::Show("成功輸出Excel查詢表:"+this->textBox2->Text, "資訊提示", MessageBoxButtons::OK, MessageBoxIcon::Information);

}

catch (Exception^ MyEx)

{

MessageBox::Show(MyEx->Message, "資訊提示", MessageBoxButtons::OK, MessageBoxIcon::Information);

}

}

};

}

5.啟動調試運作如下,在相關設定中選擇檔案路徑和查詢資料表的語句:

基于Visual C++2010 與office2010開發辦公自動化(26)-如何使用自動化輸出Excel查詢表

點選”輸出查詢表到Excel檔案“随後手動啟動上述Excel你會發現其中完好的現實查詢表中的資料,顯示如下:

基于Visual C++2010 與office2010開發辦公自動化(26)-如何使用自動化輸出Excel查詢表

成功的示範了如何輸出查詢表到Excel檔案的方法,希望讀者加以鞏固舉一反三,實際應用還需讀者自行研究揣摩,以實作更為複雜高效的功能。

繼續閱讀