在很多的应用程序中,报表是不可缺少的,一张好的报表能直观地让人把握数据的情况,方便决策。在这篇文章中,我们将以一个三层结构的asp.net程序为例,介绍如何使用crystal report ,来制作一份报表,其中介绍了不少asp.net和水晶报表的技巧。
在这个例子中,我们设想的应用要为一个销售部门制作一份报表,管理者可以查看某段时间之内的销售情况,以列表或者折线图的形式反映出销售的趋势。我们将使用SQL Server 2000做为数据库,使用VB.NET编写中间层逻辑层,而前端的表示层使用C#。我们先来看下数据库的结构。
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 |
Public Function GetAllItems () As Collections.ArrayList |
Public Function ValidateUser (strUserName as String, strPassword as String) As Integer |
Public Function GetSales (Optional nSaleId As Integer = 0, Optional nSalesPersonId As Integer = 0,Optional nItemId As Integer = 0) As Collections.ArrayList |
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"> |
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);} |
名称: | 类型: |
ItemId | Number |
StartDate | Date |
EndDate | Date |
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(); |
oRpt.Load("F:\\aspnet\\WroxWeb\\ItemReport.rpt"); |
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; } |
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"; |
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); |
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |