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开发办公自动化(18)- 如何使用自动化导出Word文件

时间:2010-08-05 21:32来源:未知 作者:admin 点击:

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

1.启动VS2010

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


 

3.添加下列引用


 

4.在Form1.h中插入以下代码,详细见代码分析与注释

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.启动调试运行如下:


查询显示数据如下:

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

 

 

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