笔者为了能够快速实现简单的报表打印,设计了下面的方案。编写一个通用打印页面,要打印报表将参数写入一个XML文档,调用页面XMLReport.aspx?report=报表名,即可完成报表的制作
3.1 创建自定义的XML报表文件
XML主要用来描述打印报表的名称,数据来源,格式,分页打印等信息,具体标签可自己定义,下面是笔者针对会员管理系统报表打印所制定的XML文档:
MemberList.xml
Member Information
Member List
MemberList
Data Source=localhost;User ID=sa;password=;Initial Catalog=XMLReport;
Select MemberID, prefix+'. '+ firstname+' '+ surname AS [Member Name],CityOrTown,State,PostCode, '$'+convert(varchar(12),FeesPaid) As Fees From Members;
10
Member Summary
MemberSummary
Data Source=localhost;User ID=sa;password=;Initial Catalog=XMLReport;
Select Count(MemberID) As [Member Count], '$'+convert(varchar(12),Sum(FeesPaid)) As [Fees Total] From Members;
标签说明:
Page Title:报表标题
ApplyXSLT:应用样式表定制报表
SQLdaraConnection: 数据源连接字串,数据提供者为SQL server
OleDbConnection:数据源连接字串,数据提供者为OleDb
Sql:选取报表数据的sql语句
PageSize:分页打印,每页显示的记录条数
读者还可以自定义一些更精确的标签来控制报表。
3.2 创建通用打印页面
page_load时读取要打印的报表名
xmlFile = Request.QueryString["report"].ToString() + ".xml";
private void BindReports()
{
DataSet dsXml = new DataSet();
try
{
dsXml.ReadXml(Server.MapPath("Reports\\" + xmlFile));
DataTable dtPage = dsXml.Tables["Page"];
DataTable dtReport = dsXml.Tables["Report"];
|