ASP.NET RDLC 报表开发详解

原来写过的,当时不会上传图片,对不住各位。现在重整理了一下,有图,比较详细。

RDLC 报表开发

打开Visual Studio 2005

clip_image002

新建ASP.NET 网站

clip_image004

添加数据集

clip_image006

会自动调出数据集配置窗口TableAdapter

clip_image008

如果上面的窗口没有自动调出,可以如下图

clip_image010

可以调出上面的TableAdapter 窗口

新建立数据库连接

clip_image012

下面的这一步会将数据库连接保存到config 文件中

clip_image014

下面的这一步可以,选择生成SQL的方式

clip_image016

让我们先回到SQL Server Query Analyzer

打开SQL Server 查询分析器

clip_image018

创建如下图的存储过程

clip_image020

然后回到 Visual Studio 2005

clip_image022

选择使用现在的存储过程,下一步

clip_image024

这里,我们只需要选择所需要的存储过程,如刚才建立的EmployeeReport 即可

点击 完成。完成TableAdapter向导的配置.

添加报表项

clip_image026

拖动一个Table 到报表设计器中

clip_image028

选择网站数据集中的字段,把它拖动到表格的列中

clip_image030

新建立一个ASPX页面,拖动一个Report Viewer控件到页面中

clip_image032

在ReportViewer任务窗口中,选择刚才建立的rdlc文件

clip_image034

生成解决方案,然后执行

clip_image036

看到结果,报表执行完成

clip_image038

至此报表开发成功。

注意

1. 刚才的SQL 脚本

CREATE PROC EmployeeReport

AS

SELECT * FROM Employee

GO

实际的报表开发中,一定不要用SELECT * ,只取报表中需要查看的字段。

2. 有时候,可能需要用户选择一些条件,有选择性的查看报表。而不是全部绑定数据

clip_image040

如上图,用户可能只需要查看2008-9-29至2008-9-30时间段之间的数据

则作法如下

先建立好如上图的ASPX页面,在View Report 事件中写如下的程序

ReportViewer1.LocalReport.ReportPath = AppDomain.CurrentDomain.BaseDirectory + "/Report/Request.rdlc";

DateTime dtFrom =Convert.ToDateTime(txtDateFrom.Text);

DateTime dtTo =Convert.ToDateTime(txtDateTo.Text);

string requester = txtRequester.Text;

string dept = txtRequestDept.Text;

string material = ddlMaterial.SelectedValue;

string iprstatus = ddlStatus.SelectedValue;

DataTable reqrpt = ReportDB.RequestReport(dtFrom, dtTo, material, dept,requester, iprstatus);

if (reqrpt != null)

{

ReportViewer1.LocalReport.DataSources.Clear();

ReportViewer1.LocalReport.DataSources.Add(

new Microsoft.Reporting.WebForms.ReportDataSource("Request_RequestReport", reqrpt));

ReportViewer1.LocalReport.Refresh();

}

ReportViewer1.LocalReport.Refresh();

根据用户所选的参数,把数据值传到SQL语句中即可.下面是RequestReport方法的源码

DataTable RequestReport(DateTime dtFrom, DateTime dtTo, string pMaterial, string pDept, string pRequester, string pIPRStatus) {

string MySQL = Purchase;

string whDate = " RequestDate BETWEEN '{0}' AND '{1}' ";

MySQL = MySQL + string.Format(whDate, dtFrom, dtTo);

string whMaterial = " AND MaterialCode='{0}' ";

if (pMaterial != "ALL")

{

MySQL = MySQL + string.Format(whMaterial, pMaterial);

}

string whDept = " AND RequestDepartment='{0}' ";

MySQL = MySQL + string.Format(whDept, pDept);

string whRequester=" AND Requester='{0}' ";

if(pRequester!="ALL")

MySQL = MySQL + string.Format(whRequester, pRequester);

string whIPRStatus = " AND IPRStatus={0} ";

if (pIPRStatus != "ALL")

{

MySQL = MySQL + string.Format(whIPRStatus, pIPRStatus);

}

IDataProvider privider = DataProvider.CreateDataProvider();

DataSet ds = privider.RetriveDataSet(MySQL);

if (ds != null && ds.Tables.Count > 0)

return ds.Tables[0];

else

return null;

}

const string Purchase="SELECT SerialNO,LedgerAcc,CostCenter,Requester,"+

" RequestDate,RequestDepartment,MaterialCode, " +

" Brand,Specifications,Unit,Quantity,Usage, "+

" ExpectedDeliveryDate,Currency "+

" ,Quotation1Supplier,Quotation1UnitPrice,Quotation1Amount, "+

" Quotation2Supplier,Quotation2UnitPrice,Quotation2Amount, "+

" Quotation3Supplier, Quotation3UnitPrice, Quotation3Amount, "+

" ProposedQuotationSupplier, ProposedQuotationUnitPrice, "+

" ProposedQuotationAmount,QuotationRemarks ,IPRStatus,QtyTo, UnitPriceTo FROM IPR WHERE ";

3. 设计报表时,可以用上述的方法,实际运行时,可以替换成SQL 语句,传到ReportDataSource中即可,只要相当的表结构字段是存在的。

4. 报表的定义是XML结构的,如果熟悉报表的定义格式规范,可以用文本编辑器打开直接修改。

clip_image042

clip_image044

5. 如果采用SQL Server 2005的服务器端报表,可能还会有进一步的方便设计和开发的地方.这里采用的是.net framework的组件,客户端只需要安装.net framework2.0即可,无需安装额外的组件.

原文连接如下

RDLC 报表开发

原文地址:https://www.cnblogs.com/JamesLi2015/p/1660086.html