asp.net利用OWC生成分析報表

本例是在本人匯率案子中使用OWC生成報表的實例代碼﹐參照了几篇文章﹐共享一下﹐希望對大家有點幫助。
不過在本例中有個地方一直不能實現﹐就是能多曲線顯示的功能﹐如知道的朋友請告知一下。源代碼如下﹕
頁面文件:ExchRateChart.aspx

 1 <% @ Page language="c#" Codebehind="ExchangeRateChart.aspx.cs" AutoEventWireup="false" Inherits="ExchRate.ExchRateWeb.ExchangeRateChart"  %>
 2 <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
 3 < HTML >
 4    < HEAD >
 5          < title > 匯率分析 </ title >
 6          < meta  content ="Microsoft Visual Studio .NET 7.1"  name ="GENERATOR" >
 7          < meta  content ="C#"  name ="CODE_LANGUAGE" >
 8          < meta  content ="JavaScript"  name ="vs_defaultClientScript" >
 9          < meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
10          < LINK  href ="css/ExchRate.css"  rel ="stylesheet" >
11          < script  language ="javascript"  src ="css/buttons.js" ></ script >
12 </ HEAD >
13      < body  class ="mainBody"  MS_POSITIONING ="GridLayout" >
14          < form  id ="Form1"  method ="post"  runat ="server" >
15              < div  align ="center" >
16                  < table  width ="90%"  height ="90%" >
17                      < tr >
18                          < td  align ="center" >< asp:label  id ="lblTitle"  Runat ="server"  Font-Bold ="True"  Font-Size ="Large" > 匯率分析 </ asp:label ></ td >
19                      </ tr >
20                      < tr >
21                          < td  class ="tbDivider"  noWrap align ="center" >< asp:label  id ="lblExchangeRateCategory"  Runat ="server" > 匯率類別 </ asp:label >< asp:dropdownlist  id =drpExchangeRateCategory  DataTextField ="ErcName"  DataValueField ="ErcNo"  DataSource ="<%#dtExchangeRateCategory%>"  Runat ="server" >
22                              </ asp:dropdownlist >< asp:label  id ="lblMoneyCategory"  Runat ="server" > 幣別 </ asp:label >< asp:dropdownlist  id =drpMoneyCategory  DataTextField ="McName"  DataValueField ="McNo"  DataSource ="<%#dtMoneyCategory%>"  Runat ="server" >
23                              </ asp:dropdownlist >< asp:label  id ="lblStartErDate"  Runat ="server" > 起止日期 </ asp:label >< asp:textbox  id ="txtStartErDate"  Runat ="server"  MaxLength ="50"  ReadOnly ="True" ></ asp:textbox >< asp:button  id ="btnStartErDate"  Runat ="server"  Text ="" ></ asp:button >< asp:label  id ="lblEndErDate"  Runat ="server" > 結束日期 </ asp:label >< asp:textbox  id ="txtEndErDate"  Runat ="server"  MaxLength ="50"  ReadOnly ="True" ></ asp:textbox >< asp:button  id ="btnEndErDate"  Runat ="server"  Text ="" ></ asp:button >< asp:button  id ="btnQuery"  Runat ="server"  Text ="查詢" ></ asp:button ></ td >
24                      </ tr >
25                      < tr >
26                          < td >< hr  size ="1" >
27                          </ td >
28                      </ tr >
29                      < tr >
30                          < td  bgcolor ="inactivecaptiontext"  align ="center"  width ="100%"  height ="100%" >
31                              < asp:Image  id ="imgChart"  runat ="server"   ></ asp:Image >
32                          </ td >
33                      </ tr >
34                  </ table >
35              </ div >
36          </ form >
37      </ body >
38 </ HTML >
39

代碼文件﹕ExchRateChart..aspx.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  System.Data.SqlClient;
using  System.IO;
using  System.Xml;
using  ExchRate.Common;
using  ExchRate.Common.Data;
using  ExchRate.DataAccess;
using  OWC11;

namespace  ExchRate.ExchRateWeb
{
    
/// 
    
/// 
    
/// 

    
    
public class ExchangeRateChart : System.Web.UI.Page
    
{
        

        
private ExchangeRateBodies exchangeRateBodies;
        
protected System.Web.UI.WebControls.DropDownList drpExchangeRateCategory;
        
protected System.Web.UI.WebControls.DropDownList drpMoneyCategory;
        
protected System.Web.UI.WebControls.Label lblStartErDate;
        
protected System.Web.UI.WebControls.TextBox txtStartErDate;
        
protected System.Web.UI.WebControls.Label lblEndErDate;
        
protected System.Web.UI.WebControls.TextBox txtEndErDate;
        
protected System.Web.UI.WebControls.Button btnStartErDate;
        
protected System.Web.UI.WebControls.Button btnEndErDate;
        
protected System.Web.UI.WebControls.Label lblExchangeRateCategory;
        
protected System.Web.UI.WebControls.Label lblMoneyCategory;
        
protected System.Web.UI.WebControls.Button btnQuery;
        
private ExchangeRateBodyData exchangeRateBodyData;

        
        
private ExchangeRateCategories exchangeRateCategories;
        
protected System.Web.UI.WebControls.Image imgChart;
        
protected System.Web.UI.WebControls.Label lblTitle;

        
        
private MoneyCategories moneyCategories;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
if(!IsPostBack)
            
{
                
this.drpExchangeRateCategory.DataBind();
                
this.drpMoneyCategory.DataBind();
                
this.txtStartErDate.Text = DateTime.Now.AddDays(-6).ToString("yyyy/MM/dd");
                
this.txtEndErDate.Text = DateTime.Now.ToString("yyyy/MM/dd");
                
this.imgChart.Visible = false;

                
//設置時間選擇按鈕
                this.btnStartErDate.Attributes.Add("OnClick","OpenModalDialog(txtStartErDate,'date.aspx','','330px','250px')");
                
this.btnEndErDate.Attributes.Add("OnClick","OpenModalDialog(txtEndErDate,'date.aspx','','330px','250px')");;
                
            }



        }


        
取得匯率類型

        
取得所有貨幣幣種

        
private void MakeLineChart(string chartTitle, string seriesTitle ,string ercNo, string mcNo, string startErDate, string endErDate)
        
{
            exchangeRateBodies 
= new ExchangeRateBodies();
            exchangeRateBodyData 
= exchangeRateBodies.GetExchangeRateBodyToChart(ercNo, mcNo, startErDate, endErDate);
            StringWriter sw 
= new StringWriter();
            XmlDocument xDoc 
= new XmlDocument();
            exchangeRateBodyData.WriteXml(sw);
            xDoc.LoadXml(sw.ToString());
            sw.Close();   

            System.Xml.XmlNodeList nodes;
            nodes 
= xDoc.ChildNodes.Item(0).ChildNodes;
            
int nCount = nodes.Count;

            
//string[] aNames = new string[nCount];
            
            
            
string[] aCategories = new string[nCount];
            
string[] aValues = new string[nCount];

            
string names=String.Empty;
            
string values =String.Empty;
            
string categories = string.Empty;

            
for(int i=0;i<nCount;i++)
            
{

                
//aNames[i]= nodes.Item(i).ChildNodes.Item(0).InnerText;        
                aCategories[i] = nodes.Item(i).ChildNodes.Item(0).InnerText;
                aValues[i]
= nodes.Item(i).ChildNodes.Item(1).InnerText;
            }


          

            
//names= String.Join("\t", aNames);  
            categories =String.Join(" \t",aCategories);
            values
= String.Join("\t", aValues);
            
           
            
//創建ChartSpace對象來放置圖表
            ChartSpace chartSpace = new ChartSpaceClass();
            
//在ChartSpace對象中添加圖表,Add方法返回Chart對象
            ChChart chart = chartSpace.Charts.Add(0);
            
            
確定曲線類型﹐并確定區別不同的曲線名稱
            
            
確定分類(X)軸標簽與數值(Y)軸標簽所對應的字段
            
            
            
對坐標軸的屬性進行設置

            
存儲為文件

            # region 不存儲為文件﹐一種更好的方式
            
byte[] byteArr = (byte[])chartSpace.GetPicture("png"800600);
            HttpContext ctx 
= HttpContext.Current;
            
string chartID = Guid.NewGuid().ToString();
            ctx.Session[chartID] 
= byteArr;
            imgChart.ImageUrl 
= string.Concat("chart.ashx?", chartID);
            imgChart.Visible 
= true;
            
            
#endregion

        }



        
Web Form 設計工具產生的程式碼

        
private void btnQuery_Click(object sender, System.EventArgs e)
        
{
            
string chartTitle = this.drpExchangeRateCategory.SelectedItem.Text.Trim() + "分析圖";
            
string seriesTitle = this.drpMoneyCategory.SelectedItem.Text.Trim();
            
string ercNo = this.drpExchangeRateCategory.SelectedItem.Value.Trim();
            
string mcNo = this.drpMoneyCategory.SelectedItem.Value.Trim();
            
string startErDate = this.txtStartErDate.Text.Trim();
            
string endErDate = this.txtEndErDate.Text.Trim();

            MakeLineChart(chartTitle, seriesTitle, ercNo, mcNo, startErDate, endErDate );

        }



    }

}


關鍵還用到了一個繼承IHttpHandler接口的類文件﹐用來處理圖片的生成
頁面文件:Chart.ashx    就一行
1 <% @ WebHandler language="c#"  class="ExchRate.ExchRateWeb.ChartHandler"Codebehind="Chart.ashx.cs"  %>
2
代碼文件﹕Chart.ashx.cs
 1 using  System;
 2 using  System.Web;
 3 using  System.Web.SessionState;
 4 using  System.IO;
 5
 6 namespace  ExchRate.ExchRateWeb
 7 {
 8    /// <summary>
 9    /// chart 的摘要描述。
10    /// </summary>

11    public class ChartHandler : IHttpHandler, IReadOnlySessionState
12    {
13        
14
15        public void ProcessRequest(HttpContext context)
16        {
17            string chartID = context.Request.QueryString[0];
18            Array arr = (Array)context.Session[chartID];
19            context.ClearError();
20            context.Response.Expires = 0;
21            context.Response.Buffer = true;
22            context.Response.Clear();
23
24            MemoryStream memStream = new MemoryStream((byte[])arr);
25            memStream.WriteTo(context.Response.OutputStream);
26            memStream.Close();
27
28            context.Response.ContentType = "image/png";
29            context.Response.StatusCode = 200;
30            context.Response.End();
31
32            
33        }

34
35        public bool IsReusable
36        {
37            get
38            {
39                return true;
40            }

41        }

42
43        
44
45    }

46
47    
48}

49




生成的分析圖如下﹕

你可能感兴趣的:(asp.net)