子风.NET 进阶中......

路途多艱,唯勤是岸

 

Owc11封装类

 

microsoft office web 组件(owc11)的编程(.net

owc控件是microsoft office 图表控件(owc10ofiiceXP的组件、owc11office2003的组件,组件的路径为C:\Program Files\Common Files\Microsoft Shared\Web Components\11\owc11,帮助文件的路径为C:\Program Files\Common Files\Microsoft Shared\Web Components\11\2052),它可以生成三维图、柱状图、饼状图、趋势图和误差图,下面以生成三维图web应用程序为例:

添加引用:在“com选项卡中选择“misrosoft office 11.0 object library”

using System;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.OWC;
using System.Data.SqlClient;
using System.Data;
using System.Text;

namespace Demo.owc
{
    
/// <summary>
    
/// Pic 的摘要说明。
    
/// 利用OWC11进行作统计图的封装类。
    
/// </summary>

    public class Owc11
    

}


使用 aspx页面
<%@ Page language="c#" Codebehind="PicTest.aspx.cs" AutoEventWireup="false" Inherits="Demo.owc.PicTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>PicTest</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    
</HEAD>
    
<body>
        
<form id="Form1" method="post" runat="server">
            
<FONT face="宋体">
                
<P>
                    
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder></P>
                
<P>
                    
<asp:Label id="列名" runat="server">列名</asp:Label>&nbsp;
                    
<asp:DropDownList id="ddlClm" runat="server" Width="105px"></asp:DropDownList>&nbsp;
                    
<asp:Label id="Label1" runat="server" Width="48px">图形</asp:Label>
                    
<asp:DropDownList id="ddlPic" runat="server" Width="105">
                        
<asp:ListItem Value="0">平滑曲线图</asp:ListItem>
                        
<asp:ListItem Value="1">饼图</asp:ListItem>
                        
<asp:ListItem Value="2">柱状图</asp:ListItem>
                    
</asp:DropDownList>&nbsp;
                    
<asp:Button id="Button1" runat="server" Width="56px" Text="确定"></asp:Button></P>
            
</FONT></FONT></FONT>
        
</form>
    
</body>
</HTML>

.cs文件
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.OWC;
using System.Data.SqlClient;

namespace Demo.owc
{
    
/// <summary>
    
/// PicTest 的摘要说明。
    
/// </summary>

    public class PicTest : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Label 列名;
        
protected System.Web.UI.WebControls.Label Label1;
        
protected System.Web.UI.WebControls.Button Button1;
        
protected System.Web.UI.WebControls.DropDownList ddlClm;
        
protected System.Web.UI.WebControls.DropDownList ddlPic;
        
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            if(!this.Page.IsPostBack)
            
{
                
this.sb_InitDll();
                
this.sb_Init(0);
            }

        }



        
private void sb_Init(int i)
        
{

            Owc11 chart 
= new Owc11();
            chart.SeriesName 
= "图例";
            
string strTitle = this.ddlClm.SelectedItem.Text; //选择列的值    
            string FilePath = this.Server.MapPath(".");
            chart.Title 
= strTitle + " " + this.ddlPic.SelectedItem.Text; //标题

            chart.PhaysicalImagePath 
= FilePath;
            chart.PicHight 
= 400;
            chart.PicWidth 
= 600;

            
string strSql = "Select 年份, " + strTitle  +" From Sheet "//这里可以改你的Sql语句
            chart.DataSource = this.dt(strSql); //这个是你的数据源

            
switch(i)
            
{
                
case 0:
                    FilePath 
+= "\\" +chart.CreateSmoothLine(); //图片得到路径
                    break;
                
case 1:
                    FilePath 
+= "\\" + chart.CreatePie();
                    
break;
                
case 2:
                    FilePath 
+= "\\" + chart.CreateColumn();
                    
break;
            }

    
        

            
//把图片添加到placeholder.
            string strImageTag = "<IMG SRC='" + FilePath + "'/>";
            PlaceHolder1.Controls.Add(
new LiteralControl(strImageTag));

        }


        
/// <summary>
        
/// 得到你要的数据,返回一个DataTable
        
/// </summary>
        
/// <param name="strSql">Sql语句</param>
        
/// <returns></returns>

        private DataTable dt(string strSql)
        
{
            
string strConn = "Server = .;database = aa;uid = sa;pwd = 135246"//数据库的连接放在这里,aa是数据库的名称 要修改成你自己的
            SqlConnection cn = new SqlConnection(strConn);
            SqlDataAdapter da 
= new SqlDataAdapter(strSql,cn);
            DataSet ds 
= new DataSet();

            da.Fill(ds);
            
return ds.Tables[0];
        }


        
/// <summary>
        
/// 初始化列
        
/// </summary>

        private void sb_InitDll()
        
{
            
string strSql = "Select * From Sheet";
            DataTable dt 
= this.dt(strSql);

            ListItem  lItem ;
            
for(int i= 1 ;i< dt.Columns.Count-1 ;i++)
            
{
                lItem 
= new ListItem();
                lItem.Text 
= dt.Columns[i].Caption.ToString();
                lItem.Value 
=i.ToString();

                
this.ddlClm.Items.Add(lItem);
            }

        }


        
Web 窗体设计器生成的代码

        
private void Button1_Click(object sender, System.EventArgs e)
        
{
            
int i = Convert.ToInt16(this.ddlPic.SelectedValue);
            
this.sb_Init(i);
        }

    }

}


  枚举类型枚举类型

posted on 2007-05-25 11:05  子风  阅读(741)  评论(2编辑  收藏  举报

导航