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开发办公自动化(26)-如何使用自动化输出Excel查询表

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

1.启动VS2010。

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


3.添加下列引用

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.启动调试运行如下,在相关设置中选择文件路径和查询数据表的语句:

点击”输出查询表到Excel文件“随后手动启动上述Excel你会发现其中完好的现实查询表中的数据,显示如下:

成功的演示了如何输出查询表到Excel文件的方法,希望读者加以巩固举一反三,实际应用还需读者自行研究揣摩,以实现更为复杂高效的功能。

 

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