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开发办公自动化(25)-如何使用自动化生成Excel图表

时间:2010-08-05 23:41来源:blog.csdn.net/yincheng01 作者: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::Button^  button1;  
    protected:   
 
    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->button1 = (gcnew System::Windows::Forms::Button());  
            this->SuspendLayout();  
            //   
            // button1  
            //   
            this->button1->Location = System::Drawing::Point(220, 206);  
            this->button1->Name = L"button1";  
            this->button1->Size = System::Drawing::Size(146, 44);  
            this->button1->TabIndex = 0;  
            this->button1->Text = L"生成Excel图表";  
            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::SystemColors::ActiveCaptionText;  
            this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"$this.BackgroundImage")));  
            this->ClientSize = System::Drawing::Size(578, 431);  
            this->Controls->Add(this->button1);  
            this->ForeColor = System::Drawing::SystemColors::ControlText;  
            this->Name = L"Form1";  
            this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;  
            this->Text = L"CSDN著名技术专家尹成-演示使用自动化生成Excel图表";  
            this->ResumeLayout(false);  
 
        } 
#pragma endregion  
    //随机产生数据生成Excel图表  
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {  
             Excel::ApplicationClass^ MyExcel;  
             Excel::_Workbook^ MyBook;  
             Excel::_Worksheet^ MySheet;  
             Excel::Range^ MyRange2;  
             Excel::Range^ MyRange1;  
             Excel::Series^ MySeries;  
             int MyNum, i;  
             String^ MyString;  
             Excel::_Chart^ MyChart;  
            try 
            {  
                MyExcel=gcnew Excel::ApplicationClass();  
                MyExcel->Visible=true;  
                MyBook=(Excel::_Workbook^)MyExcel->Workbooks->Add(System::Reflection::Missing::Value);  
                MySheet=(Excel::_Worksheet^)MyBook->ActiveSheet;  
                MySheet->Cells[1, 1]="姓名";  
                MySheet->Cells[1, 2]="年薪(元)";  
                MySheet->Cells[8, 2]="收入合计";  
                MySheet->Range["A1", "B1"]->Font->Bold=true;  
                MySheet->Range["A1", "B1"]->VerticalAlignment=Excel::XlVAlign::xlVAlignCenter;  
                MySheet->Range["A1","B1"]->Interior->ColorIndex= 36;  
                MySheet->Range["A2","B6"]->Interior->ColorIndex= 38;  
                MySheet->Cells[2,1]="由大卫";  
                MySheet->Cells[3,1]="张楠";  
                MySheet->Cells[4,1]="王啸坤";  
                MySheet->Cells[5,1]="刘莹莹";  
                MySheet->Cells[6,1]="张琼";  
                MyRange1=MySheet->Range["B2","B6"];  
                MyRange1->Formula="=RAND()*100000";  
                MyRange1->NumberFormat="0.00";  
                MyRange1=MySheet->Range["A1", "B1"];  
                MyRange1->EntireColumn->AutoFit();  
                MyNum=4;  
                MyRange2=MySheet->Range["C1", "C1"]->Resize[System::Reflection::Missing::Value, MyNum];  
                MyRange2->Formula="=\"主营业务\"& COLUMN()-2 &\"收入(元)\"";  
                MyRange2->EntireColumn->AutoFit();  
                MyRange2->Orientation= 0;  
                MyRange2->WrapText=true;  
                MyRange2->Interior->ColorIndex= 36;  
                MyRange2=MySheet->Range["C2","C6"]->Resize[System::Reflection::Missing::Value, MyNum];  
                MyRange2->Formula="=RAND()*100000000000";  
                MyRange2->NumberFormat="0.00";  
                MyRange2=MySheet->Range["C1","C6"]->Resize[System::Reflection::Missing::Value, MyNum];  
                MyRange2->Borders->Weight=Excel::XlBorderWeight::xlThin;  
                MyRange2=MySheet->Range["C8","C8"]->Resize[System::Reflection::Missing::Value, MyNum];  
                MyRange2->Formula="=SUM(C2:C6)";  
                MyRange2->Borders[Excel::XlBordersIndex::xlEdgeBottom]->LineStyle=Excel::XlLineStyle::xlDouble;   
                MyRange2->Borders[Excel::XlBordersIndex::xlEdgeBottom]->Weight=Excel::XlBorderWeight::xlThick;  
                MyBook=(Excel::_Workbook^)MySheet->Parent;  
                MyChart=(Excel::_Chart^)MyBook->Charts->Add(System::Reflection::Missing::Value,System::Reflection::Missing::Value,System::Reflection::Missing::Value, System::Reflection::Missing::Value);  
                MyRange2=MySheet->Range["C2:C6",System::Reflection::Missing::Value]->Resize[System::Reflection::Missing::Value,MyNum];  
                MyChart->ChartWizard(MyRange2, Excel::XlChartType::xl3DColumn, System::Reflection::Missing::Value, Excel::XlRowCol::xlColumns, System::Reflection::Missing::Value, System::Reflection::Missing::Value, System::Reflection::Missing::Value,System::Reflection::Missing::Value, System::Reflection::Missing::Value, System::Reflection::Missing::Value, System::Reflection::Missing::Value );  
                MySeries=(Excel::Series^)MyChart->SeriesCollection(1);  
                MySeries->XValues=MySheet->Range["A2","A6"];  
                for (i = 1; i < 5; i++)  
                {  
                    MySeries=(Excel::Series^)MyChart->SeriesCollection(i);  
                    MyString = "=\"主营业务";  
                    MyString = MyString +Convert::ToString(i);  
                    MyString = MyString + "\"";  
                    MySeries->Name = MyString;  
                }  
                MyChart->Location(Excel::XlChartLocation::xlLocationAsObject, MySheet->Name);  
                MyRange2 = (Excel::Range^)MySheet->Rows->Item[10, System::Reflection::Missing::Value];  
                MySheet->Shapes->Item("Chart 1")->Top =(float)Convert::ToDouble(MyRange2->Top);  
                MyRange2 = (Excel::Range^)MySheet->Columns[2, System::Reflection::Missing::Value];  
                MySheet->Shapes->Item("Chart 1")->Left =(float)Convert::ToDouble(MyRange2->Left);  
            }  
            catch(Exception^ MyEx)  
            {  
                MessageBox::Show(MyEx->Message,"信息提示",MessageBoxButtons::OK, MessageBoxIcon::Information);  
            }  
        }  
    };  

5.启动调试,点击”导出Excel图表“按钮启动Excel,显示如下:

 


成功的演示了如何使用自动化生成Excel图表的方法,希望读者加以巩固举一反三,实际应用还需读者自行研究揣摩,以实现更为复杂高效的功能。

 

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