【 使用自动化生成Word图表】

1.启动VS20109462618_201007032155471.jpg

 

2.创建一个CLR项目如下,在窗体中插入一个Button,插入背景,如下图所示:9462618_201007032155472.jpg

3.添加下列引用9462618_201007032155473.jpg

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

 

 

view plaincopy to clipboardprint?

#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;  

    ///   

    /// Form1 摘要  

    ///  

    /// 警告: 如果更改此类的名称,则需要更改  

    ///          与此类所依赖的所有 .resx 文件关联的托管资源编译器工具的  

    ///          “资源文件名属性。否则,  

    ///          设计器将不能与此窗体的关联  

    ///          本地化资源正确交互。  

    ///   

    public ref class Form1 : public System::Windows::Forms::Form  

    {  

    public:  

        Form1(void)  

        {  

            InitializeComponent();  

            //  

            //TODO: 在此处添加构造函数代码  

            //  

        }  

    protected:  

        ///   

        /// 清理所有正在使用的资源。  

        ///   

        ~Form1()  

        {  

            if (components)  

            {  

                delete components;  

            }  

        }  

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

    protected:   

    private:  

        ///   

        /// 必需的设计器变量。  

        ///   

        System::ComponentModel::Container ^components; 

#pragma region Windows Form. Designer generated code  

        ///   

        /// 设计器支持所需的方法 - 不要  

        /// 使用代码编辑器修改此方法的内容。  

        ///   

        void InitializeComponent(void)  

        {  

            System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));  

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

            this->SuspendLayout();  

            //   

            // button1  

            //   

            this->button1->Font = (gcnew System::Drawing::Font(L"SimSun", 12, System::Drawing::FontStyle.:Bold, System::Drawing::GraphicsUnit::Point,   

                static_cast<:byte>(134)));  

            this->button1->Location = System::Drawing::Point(138, 207);  

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

            this->button1->Size = System::Drawing::Size(288, 59);  

            this->button1->TabIndex = 0;  

            this->button1->Text = L"演示使用自动化插入Word图表";  

            this->button1->UseVisualStyleBackColor = true;  

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

            //   

            // Form1  

            //   

            this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);  

            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;  

            this->BackColor = System::Drawing::Color::Black;  

            this->BackgroundImage = (cli::safe_cast<:drawing::image style="mso-spacerun: yes">  >(resources->GetObject(L"$this.BackgroundImage")));  

            this->ClientSize = System::Drawing::Size(567, 433);  

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

            this->Name = L"Form1";  

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

            this->Text = L"CSDN著名技术专家尹成-演示使用自动化生成Word图表";  

            this->ResumeLayout(false);  

        } 

#pragma endregion  

    //演示使用自动化插入Word图表  

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

            Microsoft::Office::Interop::Word::ApplicationClass^ MyWord;  

            Object^ MyMissing;  

            Object^ MyEndOfDoc;  

            Microsoft::Office::Interop::Word::Range^ MyRange;  

            Microsoft::Office::Interop::Word::_Document^ MyDoc;  

            Microsoft::Office::Interop::Word::InlineShape^ MyShape;  

            Object^ MyClassType;  

            Object^ MyChart;  

            Object^ MyChartApp;  

            array<:object>^ MyArray;  

            try 

            {  

                MyMissing=System::Reflection::Missing::Value;  

                // endofdoc 是一个预定义的书签  

                MyEndOfDoc="\\endofdoc";  

                MyWord=gcnew Microsoft::Office::Interop::Word::ApplicationClass();  

                MyWord->Visible= true;  

                MyDoc=MyWord->Documents->Add(MyMissing,MyMissing,MyMissing, MyMissing);  

                //插入图表  

                MyClassType="MSGraph.Chart.8";  

                MyRange=MyDoc->Bookmarks[MyEndOfDoc]->Range;  

                System::Reflection::Binder^ MyBinder;  

                MyShape=MyRange->InlineShapes->AddOLEObject(MyClassType,MyMissing, MyMissing, MyMissing,MyMissing,MyMissing, MyMissing, MyMissing);  

                MyChart=MyShape->OLEFormat->Object;  

                MyChartApp=MyChart->GetType()->InvokeMember("Application",System::Reflection::BindingFlags::GetProperty,MyBinder, MyChart,MyArray);  

                MyShape->Width=MyWord->InchesToPoints((float)5.50);  

                MyShape->Height=MyWord->InchesToPoints((float)2.57);  

                MyRange=MyDoc->Bookmarks[MyEndOfDoc]->Range;  

                MyRange->InsertParagraphAfter();  

                this->Close();  

            }  

            catch(Exception^ MyEx)  

            {  

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

            }  

        }  

    };  

#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;

      ///

      /// Form1 摘要

      ///

      /// 警告: 如果更改此类的名称,则需要更改

      ///          与此类所依赖的所有 .resx 文件关联的托管资源编译器工具的

      ///          “资源文件名属性。否则,

      ///          设计器将不能与此窗体的关联

      ///          本地化资源正确交互。

      ///

      public ref class Form1 : public System::Windows::Forms::Form

      {

      public:

           Form1(void)

           {

                 InitializeComponent();

                 //

                 //TODO: 在此处添加构造函数代码

                 //

           }

      protected:

           ///

           /// 清理所有正在使用的资源。

           ///

           ~Form1()

           {

                 if (components)

                 {

                      delete components;

                 }

           }

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

      protected:

      private:

           ///

           /// 必需的设计器变量。

           ///

           System::ComponentModel::Container ^components;

#pragma region Windows Form. Designer generated code

           ///

           /// 设计器支持所需的方法 - 不要

           /// 使用代码编辑器修改此方法的内容。

           ///

           void InitializeComponent(void)

           {

                 System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));

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

                 this->SuspendLayout();

                 //

                 // button1

                 //

                 this->button1->Font = (gcnew System::Drawing::Font(L"SimSun", 12, System::Drawing::FontStyle.:Bold, System::Drawing::GraphicsUnit::Point,

                      static_cast<:byte>(134)));

                 this->button1->Location = System::Drawing::Point(138, 207);

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

                 this->button1->Size = System::Drawing::Size(288, 59);

                 this->button1->TabIndex = 0;

                 this->button1->Text = L"演示使用自动化插入Word图表";

                 this->button1->UseVisualStyleBackColor = true;

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

                 //

                 // Form1

                 //

                 this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);

                 this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

                 this->BackColor = System::Drawing::Color::Black;

                 this->BackgroundImage = (cli::safe_cast<:drawing::image style="mso-spacerun: yes">  >(resources->GetObject(L"$this.BackgroundImage")));

                 this->ClientSize = System::Drawing::Size(567, 433);

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

                 this->Name = L"Form1";

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

                 this->Text = L"CSDN著名技术专家尹成-演示使用自动化生成Word图表";

                 this->ResumeLayout(false);

           }

#pragma endregion

      //演示使用自动化插入Word图表

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

                 Microsoft::Office::Interop::Word::ApplicationClass^ MyWord;

                 Object^ MyMissing;

                 Object^ MyEndOfDoc;

                 Microsoft::Office::Interop::Word::Range^ MyRange;

                 Microsoft::Office::Interop::Word::_Document^ MyDoc;

                 Microsoft::Office::Interop::Word::InlineShape^ MyShape;

                 Object^ MyClassType;

                 Object^ MyChart;

                 Object^ MyChartApp;

                 array<:object>^ MyArray;

                 try

                 {

                      MyMissing=System::Reflection::Missing::Value;

                      // endofdoc 是一个预定义的书签

                      MyEndOfDoc="\\endofdoc";

                      MyWord=gcnew Microsoft::Office::Interop::Word::ApplicationClass();

                      MyWord->Visible= true;

                      MyDoc=MyWord->Documents->Add(MyMissing,MyMissing,MyMissing, MyMissing);

                      //插入图表

                      MyClassType="MSGraph.Chart.8";

                      MyRange=MyDoc->Bookmarks[MyEndOfDoc]->Range;

                      System::Reflection::Binder^ MyBinder;

                       MyShape=MyRange->InlineShapes->AddOLEObject(MyClassType,MyMissing, MyMissing, MyMissing,MyMissing,MyMissing, MyMissing, MyMissing);

                      MyChart=MyShape->OLEFormat->Object;

                       MyChartApp=MyChart->GetType()->InvokeMember("Application",System::Reflection::BindingFlags::GetProperty,MyBinder, MyChart,MyArray);

                      MyShape->Width=MyWord->InchesToPoints((float)5.50);

                      MyShape->Height=MyWord->InchesToPoints((float)2.57);

                      MyRange=MyDoc->Bookmarks[MyEndOfDoc]->Range;

                      MyRange->InsertParagraphAfter();

                      this->Close();

                 }

                 catch(Exception^ MyEx)

                 {

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

                 }

           }

      };

}

 

5.启动调试运行如下:9462618_201007032155474.jpg

点击演示使用自动化插入Word图表按钮启动word,显示如下,word中自动插入了一个图表:9462618_201007032155475.jpg

成功的演示了在word中插入图表的方法,实际应用还需读者自行研究揣摩,以实现更为复杂高效的功能。

 

fj.png1.JPG

fj.png2.JPG

fj.png3.JPG

fj.png4.JPG

fj.png5.JPG

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9462618/viewspace-667111/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9462618/viewspace-667111/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值