国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET中利用Crystal Report創(chuàng)建圖表

2024-07-10 12:59:35
字體:
供稿:網(wǎng)友
在很多的應(yīng)用程序中,報表是不可缺少的,一張好的報表能直觀地讓人把握數(shù)據(jù)的情況,方便決策。在這篇文章中,我們將以一個三層結(jié)構(gòu)的asp.net程序為例,介紹如何使用crystal report ,來制作一份報表,其中介紹了不少asp.net和水晶報表的技巧。

  在這個例子中,我們設(shè)想的應(yīng)用要為一個銷售部門制作一份報表,管理者可以查看某段時間之內(nèi)的銷售情況,以列表或者折線圖的形式反映出銷售的趨勢。我們將使用sql server 2000做為數(shù)據(jù)庫,使用vb.net編寫中間層邏輯層,而前端的表示層使用c#。我們先來看下數(shù)據(jù)庫的結(jié)構(gòu)。




  其中,tbitem表中存放的是每張訂單中所訂購的貨品,tbsales存放的是每張訂單,tblsalesperson是銷售員表,存放該出版社的每位銷售員。

  接下來,使用sql server 2000來創(chuàng)建這些表。表的結(jié)構(gòu)如下所示:

create table [dbo].[tblitem] (
[itemid] [int] not null ,
[description] [varchar] (50) not null
) on [primary]
create table [dbo].[tblsalesperson] (
[salespersonid] [int] not null ,
[username] [varchar] (50) not null ,
[password] [varchar] (30) not null
) on [primary]
create table [dbo].[tblsales] (
[saleid] [int] identity (1, 1) not null ,
[salespersonid] [int] not null ,
[itemid] [int] not null ,
[saledate] [datetime] not null ,
[amount] [int] not null
) on [primary]

  并且用以下的代碼創(chuàng)建表之間的約束關(guān)系。

alter table tblitem
add constraint pk_itemid
primary key (itemid)
go
alter table tblsalesperson
add constraint pk_salespersonid
primary key (salespersonid)
go
alter table tblsales
add constraint fk_itemid
foreign key (itemid) references tblitem(itemid)
go
alter table tblsales
add constraint fk_salespersonid
foreign key (salespersonid) references tblsalesperson(salespersonid)
go


  創(chuàng)建中間邏輯層

  在中間邏輯層組件中,我們?yōu)槊恳粡埍矶紕?chuàng)建兩個類。比如,對于tblitems表,創(chuàng)建item 和items類。item類中記錄每件銷售的貨品的詳細(xì)情況,而items表則記錄所有銷售的貨品并且有增加貨品的方法。這樣,就存在下面的六個類:

  item and items
  salesperson and salespersons
  sale and sales

  接下來,看一下每個類中的屬性:

  item類

   包括如下屬性
    itemid (貨品id編號)
    description (貨品的描述)
    items

  有一個方法,將根據(jù)item的編號返回一個item對象


public function getallitems () as collections.arraylist


  salesperson

  該類有以下三個屬性:

   salespersonid (銷售員編號)
   name (姓名)
   password (密碼)

  salespersons

  有一個方法,根據(jù)銷售員登陸時輸入的用戶名和密碼,在數(shù)據(jù)庫中,驗證銷售員的登陸是否正確,如果正確,則返回零。


public function validateuser (strusername as string, strpassword as string) as integer


  sale

  有如下5個屬性

  · saleid
  · salespersonid
  · itemid
  · saledate
  · amount

  sales

  有兩個方法,其中g(shù)etsales根據(jù)輸入的參數(shù)返回sales對象的集合


public function getsales (optional nsaleid as integer = 0, optional nsalespersonid as integer = 0,optional nitemid as integer = 0) as collections.arraylist


  還有一個addsales方法,用于增加一張訂單


public function addsale (objsale as sale)

  創(chuàng)建表現(xiàn)層頁面

  接下來,開始創(chuàng)建表現(xiàn)層頁面。首先,要創(chuàng)建一個可以給銷售員登陸的頁面,如下圖所示。



  在銷售員成功登陸后,可以給銷售員輸入某樣貨品銷售的數(shù)量,如下圖:



  此外,為了給業(yè)務(wù)主管看到某個時期的銷售情況,創(chuàng)建如下頁面。



  其中,業(yè)務(wù)主管可以選擇查看某樣貨品在某個時期(選擇開始日期,結(jié)束日期)的銷售情況,以圖表方式顯示。

  這里特別值得一提的是,日歷控件的使用。在頁面中放置日歷控件后,設(shè)置如下代碼:


<input type="image" onclick="page_validationactive=false;" src=http://www.163design.net/n/b/"datepicker.gif" alt="show calender" runat="server" onserverclick="showcal1" id="imgcal1" name="imgcal1">


  這里設(shè)置page_validationactive參數(shù)為false,所以不需要重新提交頁面,并且,在onserverclick事件中,設(shè)置處理的代碼,如下所示:


public void showcal1(object sender, system.web.ui.imageclickeventargs e)
{ //顯示日歷控件
dtpicker1.visible = true;
}


  當(dāng)用戶選擇了相關(guān)的日期后,在文本框中可以獲得相關(guān)的日期:


private void dtpicker1_selectionchanged(object sender, system.eventargs e)
{
txtstartdate.text = dtpicker1.selecteddate.toshortdatestring();
dtpicker1.visible = false;
}


  在提交的頁面中,處理的代碼如下:


private void bsubmit_serverclick(object sender, system.eventargs e)
{
response.redirect("viewreport.aspx?itemid=" + cboitemtype.selecteditem.value + "&startdate=" + txtstartdate.text + "&enddate=" + txtenddate.text);}


  在提交頁面后,將跳轉(zhuǎn)到瀏覽報表的頁面viewreport.aspx,傳入相關(guān)的參數(shù),如貨品id,開始和結(jié)束日期。

  用水晶報表創(chuàng)建報表

  首先,往窗體中添加水晶報表控件,之后,就可以用水晶報表的報表設(shè)計器設(shè)計一份報表了。新建一個水晶報表文件類型,命名為itemreport.rpt,接著使用報表設(shè)計專家,并選擇設(shè)計標(biāo)準(zhǔn)型的報表,點下一步,出現(xiàn)如下畫面:





  我們選擇使用ado類型的數(shù)據(jù),在彈出的窗口中,設(shè)置好sql server的登陸名,登陸密碼,選擇好數(shù)據(jù)庫后,選擇在報表中使用tblsales表,如下圖:





  接著選next,在要在報表中顯示的字段中,選擇saledate和amount。之后,一直選next,忽略其他相關(guān)設(shè)置,最后,在圖表類型中選擇折線圖,如下圖:





  最后點擊finish,可以看到如下的報表:





  再次選擇報表專家,在彈出的窗口中選擇數(shù)據(jù)頁,在數(shù)據(jù)可用字段中選擇salesdate,并且可以在文本頁中,設(shè)置適當(dāng)?shù)膱蟊順?biāo)題。





  由于要根據(jù)輸入的日期,貨品參數(shù)來動態(tài)顯示報表,因此我們要設(shè)置參數(shù)字段。在報表設(shè)計器中,在字段資源管理器中,選擇參數(shù)字段,鼠標(biāo)右擊選擇“新建”,新建如下三個參數(shù)字段。


名稱: 類型:
itemid number
startdate date
enddate date

  
  最后,要設(shè)置相關(guān)的查詢公式,在除報表頁眉的區(qū)域,鼠標(biāo)右鍵點擊,在彈出菜單中選擇“報表|編輯選擇公式|記錄”,輸入如下的公式:





  在上面的公式編輯器中,分為左中右三部分,左邊的是報表的字段,中間的是相關(guān)的功能函數(shù),最右邊的是運算符,雙擊其中選中的部分,則可以添加到下部的公式顯示區(qū)域中。最后,保存建立的公式。

  在程序中使用報表

  接下來,我們可以在程序中用代碼處理和報表的連接過程。首先,在工程項目中,增加如下的兩個命名空間的引用(注意,在代碼中也必須用using引入它們):

crystaldecisions.crystalreports.engine
crystaldecisions.shared
在viewreport.aspx的page_load事件中,加入以下代碼

//接收傳遞的參數(shù)
nitemid = int.parse(request.querystring.get("itemid"));
strstartdate = request.querystring.get("startdate");
strenddate = request.querystring.get("enddate");
//聲明報表的數(shù)據(jù)對象
crystaldecisions.crystalreports.engine.database crdatabase; crystaldecisions.crystalreports.engine.table crtable;
tablelogoninfo dbconn = new tablelogoninfo();
// 創(chuàng)建報表對象opt
reportdocument orpt = new reportdocument();
// 加載已經(jīng)做好的報表
orpt.load("f://aspnet//wroxweb//itemreport.rpt");
//連接數(shù)據(jù)庫,獲得相關(guān)的登陸信息
crdatabase = orpt.database;
//定義一個arrtables對象數(shù)組
object[] arrtables = new object[1];
crdatabase.tables.copyto(arrtables, 0);
crtable = (crystaldecisions.crystalreports.engine.table)arrtables[0]; dbconn = crtable.logoninfo;
//設(shè)置相關(guān)的登陸數(shù)據(jù)庫的信息
dbconn.connectioninfo.databasename = "wroxsellers"; dbconn.connectioninfo.servername = "localhost";
dbconn.connectioninfo.userid = "sa";
dbconn.connectioninfo.password = "test";
//將登陸的信息應(yīng)用于crtable表對象
crtable.applylogoninfo(dbconn);
//將報表和報表瀏覽控件綁定
crviewer.reportsource = orpt;
//傳遞參數(shù)
setreportparameters();

  在上面的代碼中,首先接收了日期,貨品編號等參數(shù),并實例化了database, table和 tablelogoninfo 三個類,它們是用來在運行時,建立報表和數(shù)據(jù)庫的連接必需的。再使用

orpt.load("f://aspnet//wroxweb//itemreport.rpt");

  來裝載已經(jīng)做好了的報表。

  在裝載完報表后,將數(shù)據(jù)庫中要使用的表復(fù)制到一個對象數(shù)組中去,并選擇對象數(shù)組中的第一個表元素,將其轉(zhuǎn)換為水晶報表的報表對象。接著,再設(shè)置logoninfo中的登陸數(shù)據(jù)庫的信息。最后,將報表源與報表瀏覽控件綁定。

  傳遞參數(shù)到水晶報表

  定義一個新的過程,setreportparameters(),代碼如下:

private void setreportparameters()
{
// all the parameter fields will be added to this collection
parameterfields paramfields = new parameterfields();
// the parameter fields to be sent to the report
parameterfield pfitemid = new parameterfield();
parameterfield pfstartdate = new parameterfield();
parameterfield pfenddate = new parameterfield();
// 設(shè)置在報表中,將要接受的參數(shù)字段的名稱
pfitemid.parameterfieldname = "itemid";
pfstartdate.parameterfieldname = "startdate";
pfenddate.parameterfieldname = "enddate";
parameterdiscretevalue dcitemid = new parameterdiscretevalue();
parameterdiscretevalue dcstartdate = new parameterdiscretevalue(); parameterdiscretevalue dcenddate = new parameterdiscretevalue();

dcitemid.value = nitemid;
dcstartdate.value = datetime.parse(strstartdate);
dcenddate.value = datetime.parse(strenddate);

pfitemid.currentvalues.add(dcitemid);
pfstartdate.currentvalues.add(dcstartdate); pfenddate.currentvalues.add(dcenddate);
paramfields.add(pfitemid);
paramfields.add(pfstartdate);
paramfields.add(pfenddate);

// 將參數(shù)集合綁定到報表瀏覽控件
crviewer.parameterfieldinfo = paramfields;
}

  現(xiàn)在來解釋一下上面的代碼。在水晶報表中,瀏覽器控件有一個屬性parameterfieldsinfo,該屬性可以通過綁定parameterfields類型的集合,將相關(guān)的參數(shù)的實際數(shù)值傳遞到報表。parameterfields類型集合通過add的方法,接收parameterfield類型的對象。因此,我們先為itemid,startdate,enddate三個參數(shù)創(chuàng)建parameterfield類型的對象,并且設(shè)置它們對應(yīng)報表中接受參數(shù)的名稱:

parameterfields paramfields = new parameterfields();
parameterfield pfitemid = new parameterfield();
parameterfield pfstartdate = new parameterfield();
parameterfield pfenddate = new parameterfield();
// 設(shè)置在報表中,將要接受的參數(shù)字段的名稱
pfitemid.parameterfieldname = "itemid";
pfstartdate.parameterfieldname = "startdate";
pfenddate.parameterfieldname = "enddate";

  接著,可以為這些參數(shù)字段設(shè)置具體的值了,但由于parameterfield必須接受p arameterdiscretevalue類型的對象,所以,再創(chuàng)建相關(guān)的實例:

parameterdiscretevalue dcitemid = new parameterdiscretevalue();
parameterdiscretevalue dcstartdate = new parameterdiscretevalue();
parameterdiscretevalue dcenddate = new parameterdiscretevalue();
dcitemid.value = nitemid;
dcstartdate.value = datetime.parse(strstartdate);
dcenddate.value = datetime.parse(strenddate);

  最后,就可以在三個parameterfield的對象中的currentvalues中設(shè)置它們的值,并往paramfields集合中加進(jìn)這三個parameterfield對象。

  運行的結(jié)果如下。



  本程序在vs.net 2003,crystal report 9以上版本可以正確運行。

國內(nèi)最大的酷站演示中心!
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 东方市| 高安市| 鄂温| 廊坊市| 措勤县| 赤峰市| 东海县| 灵寿县| 镇原县| 河源市| 东平县| 新丰县| 天气| 石棉县| 桐城市| 洪江市| 宣武区| 安图县| 依安县| 临颍县| 宜昌市| 枝江市| 化德县| 平塘县| 永州市| 汤阴县| 罗城| 宜丰县| 彰化市| 施秉县| 古交市| 海阳市| 剑阁县| 涞水县| 吉水县| 商丘市| 洪洞县| 万安县| 阳春市| 南木林县| 宁晋县|