欢迎光临
我们一直在努力

ASP.NET中利用Crystal Report创建图表-.NET教程,.NET Framework

建站超值云服务器,限时71元/月

在很多的应用程序中,报表是不可缺少的,一张好的报表能直观地让人把握数据的情况,方便决策。在这篇文章中,我们将以一个三层结构的asp.net程序为例,介绍如何使用crystal report ,来制作一份报表,其中介绍了不少asp.net和水晶报表的技巧。

  在这个例子中,我们设想的应用要为一个销售部门制作一份报表,管理者可以查看某段时间之内的销售情况,以列表或者折线图的形式反映出销售的趋势。我们将使用sql server 2000做为数据库,使用vb.net编写中间层逻辑层,而前端的表示层使用c#。我们先来看下数据库的结构。

  其中,tbitem表中存放的是每张订单中所订购的货品,tbsales存放的是每张订单,tblsalesperson是销售员表,存放该出版社的每位销售员。

  接下来,使用sql server 2000来创建这些表。表的结构如下所示:

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]

  并且用以下的代码创建表之间的约束关系。

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

  创建中间逻辑层

  在中间逻辑层组件中,我们为每一张表都创建两个类。比如,对于tblitems表,创建item 和items类。item类中记录每件销售的货品的详细情况,而items表则记录所有销售的货品并且有增加货品的方法。这样,就存在下面的六个类:

  item and items

  salesperson and salespersons

  sale and sales

  接下来,看一下每个类中的属性:

  item类

   包括如下属性

    itemid (货品id编号)

    description (货品的描述)

    items

  有一个方法,将根据item的编号返回一个item对象

public function getallitems () as collections.arraylist

  salesperson

  该类有以下三个属性:

   salespersonid (销售员编号)

   name (姓名)

   password (密码)

  salespersons

  有一个方法,根据销售员登陆时输入的用户名和密码,在数据库中,验证销售员的登陆是否正确,如果正确,则返回零。

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

  sale

  有如下5个属性

  · saleid

  · salespersonid

  · itemid

  · saledate

  · amount

  sales

  有两个方法,其中getsales根据输入的参数返回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)

  创建表现层页面

  接下来,开始创建表现层页面。首先,要创建一个可以给销售员登陆的页面,如下图所示。

  在销售员成功登陆后,可以给销售员输入某样货品销售的数量,如下图:

  此外,为了给业务主管看到某个时期的销售情况,创建如下页面。

  其中,业务主管可以选择查看某样货品在某个时期(选择开始日期,结束日期)的销售情况,以图表方式显示。

  这里特别值得一提的是,日历控件的使用。在页面中放置日历控件后,设置如下代码:

<input type="image" onclick="page_validationactive=false;" src="datepicker.gif" alt="show calender" runat="server" onserverclick="showcal1" id="imgcal1" name="imgcal1">

  这里设置page_validationactive参数为false,所以不需要重新提交页面,并且,在onserverclick事件中,设置处理的代码,如下所示:

public void showcal1(object sender, system.web.ui.imageclickeventargs e)

{ //显示日历控件

dtpicker1.visible = true;

}

  当用户选择了相关的日期后,在文本框中可以获得相关的日期:

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);}

  在提交页面后,将跳转到浏览报表的页面viewreport.aspx,传入相关的参数,如货品id,开始和结束日期。

  用水晶报表创建报表

  首先,往窗体中添加水晶报表控件,之后,就可以用水晶报表的报表设计器设计一份报表了。新建一个水晶报表文件类型,命名为itemreport.rpt,接着使用报表设计专家,并选择设计标准型的报表,点下一步,出现如下画面:

  我们选择使用ado类型的数据,在弹出的窗口中,设置好sql server的登陆名,登陆密码,选择好数据库后,选择在报表中使用tblsales表,如下图:

  接着选next,在要在报表中显示的字段中,选择saledate和amount。之后,一直选next,忽略其他相关设置,最后,在图表类型中选择折线图,如下图:

  最后点击finish,可以看到如下的报表:

  再次选择报表专家,在弹出的窗口中选择数据页,在数据可用字段中选择salesdate,并且可以在文本页中,设置适当的报表标题。

  由于要根据输入的日期,货品参数来动态显示报表,因此我们要设置参数字段。在报表设计器中,在字段资源管理器中,选择参数字段,鼠标右击选择“新建”,新建如下三个参数字段。

名称: 类型:

itemid number

startdate date

enddate date

  

  最后,要设置相关的查询公式,在除报表页眉的区域,鼠标右键点击,在弹出菜单中选择“报表|编辑选择公式|记录”,输入如下的公式:

  在上面的公式编辑器中,分为左中右三部分,左边的是报表的字段,中间的是相关的功能函数,最右边的是运算符,双击其中选中的部分,则可以添加到下部的公式显示区域中。最后,保存建立的公式。

  在程序中使用报表

  接下来,我们可以在程序中用代码处理和报表的连接过程。首先,在工程项目中,增加如下的两个命名空间的引用(注意,在代码中也必须用using引入它们):

crystaldecisions.crystalreports.engine

crystaldecisions.shared

在viewreport.aspx的page_load事件中,加入以下代码

//接收传递的参数

nitemid = int.parse(request.querystring.get("itemid"));

strstartdate = request.querystring.get("startdate");

strenddate = request.querystring.get("enddate");

//声明报表的数据对象

crystaldecisions.crystalreports.engine.database crdatabase; crystaldecisions.crystalreports.engine.table crtable;

tablelogoninfo dbconn = new tablelogoninfo();

// 创建报表对象opt

reportdocument orpt = new reportdocument();

// 加载已经做好的报表

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

//连接数据库,获得相关的登陆信息

crdatabase = orpt.database;

//定义一个arrtables对象数组

object[] arrtables = new object[1];

crdatabase.tables.copyto(arrtables, 0);

crtable = (crystaldecisions.crystalreports.engine.table)arrtables[0]; dbconn = crtable.logoninfo;

//设置相关的登陆数据库的信息

dbconn.connectioninfo.databasename = "wroxsellers"; dbconn.connectioninfo.servername = "localhost";

dbconn.connectioninfo.userid = "sa";

dbconn.connectioninfo.password = "test";

//将登陆的信息应用于crtable表对象

crtable.applylogoninfo(dbconn);

//将报表和报表浏览控件绑定

crviewer.reportsource = orpt;

//传递参数

setreportparameters();

  在上面的代码中,首先接收了日期,货品编号等参数,并实例化了database, table和 tablelogoninfo 三个类,它们是用来在运行时,建立报表和数据库的连接必需的。再使用

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

  来装载已经做好了的报表。

  在装载完报表后,将数据库中要使用的表复制到一个对象数组中去,并选择对象数组中的第一个表元素,将其转换为水晶报表的报表对象。接着,再设置logoninfo中的登陆数据库的信息。最后,将报表源与报表浏览控件绑定。

  传递参数到水晶报表

  定义一个新的过程,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();

// 设置在报表中,将要接受的参数字段的名称

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

// 将参数集合绑定到报表浏览控件

crviewer.parameterfieldinfo = paramfields;

}

  现在来解释一下上面的代码。在水晶报表中,浏览器控件有一个属性parameterfieldsinfo,该属性可以通过绑定parameterfields类型的集合,将相关的参数的实际数值传递到报表。parameterfields类型集合通过add的方法,接收parameterfield类型的对象。因此,我们先为itemid,startdate,enddate三个参数创建parameterfield类型的对象,并且设置它们对应报表中接受参数的名称:

parameterfields paramfields = new parameterfields();

parameterfield pfitemid = new parameterfield();

parameterfield pfstartdate = new parameterfield();

parameterfield pfenddate = new parameterfield();

// 设置在报表中,将要接受的参数字段的名称

pfitemid.parameterfieldname = "itemid";

pfstartdate.parameterfieldname = "startdate";

pfenddate.parameterfieldname = "enddate";

  接着,可以为这些参数字段设置具体的值了,但由于parameterfield必须接受p arameterdiscretevalue类型的对象,所以,再创建相关的实例:

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中设置它们的值,并往paramfields集合中加进这三个parameterfield对象。

  运行的结果如下。

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP.NET中利用Crystal Report创建图表-.NET教程,.NET Framework
分享到: 更多 (0)

相关推荐

  • 暂无文章