ASP.NET中应用XML技术实现Web报表打印

发表于:2007-06-30来源:作者:点击数: 标签:
应用XML技术实现报表打印方案的设计与实现 笔者为了能够快速实现简单的报表打印,设计了下面的方案。编写一个通用打印页面,要打印报表将参数写入一个XML文档,调用页面XM LR eport.aspx?report=报表名,即可完成报表的制作 3.1 创建自定义的XML报表文件 XML
应用XML技术实现报表打印方案的设计与实现







笔者为了能够快速实现简单的报表打印,设计了下面的方案。编写一个通用打印页面,要打印报表将参数写入一个XML文档,调用页面XMLReport.aspx?report=报表名,即可完成报表的制作




3.1 创建自定义的XML报表文件







XML主要用来描述打印报表的名称,数据来源,格式,分页打印等信息,具体标签可自己定义,下面是笔者针对会员管理系统报表打印所制定的XML文档:

MemberList.xml

<?xml version="1.0" encoding="utf-8" ?>







<XmlReport>







<Page>







<Title>Member Information</Title>







<ApplyXSLT></ApplyXSLT>







</Page>







<Report>







<Title>Member List</Title>







<TableName>MemberList</TableName>







<SQLdataConnection>Data Source=localhost;User ID=sa;password=;Initial Catalog=XMLReport;</SQLdataConnection>







<OleDbConnection></OleDbConnection>







<Sql>Select MemberID, prefix+@#. @#+ firstname+@# @#+ surname AS [Member Name],CityOrTown,State,PostCode, @#$@#+convert(varchar(12),FeesPaid) As Fees From Members;</Sql>







<PageSize>10</PageSize>







</Report>







<Report>







<Title>Member Summary</Title>







<TableName>MemberSummary</TableName>







<SQLdataConnection>Data Source=localhost;User ID=sa;password=;Initial Catalog=XMLReport;</SQLdataConnection>







<OleDbConnection></OleDbConnection>







<Sql>Select Count(MemberID) As [Member Count], @#$@#+convert(varchar(12),Sum(FeesPaid)) As [Fees Total] From Members;</Sql>







<PageSize></PageSize>







</Report>







</XmlReport>















标签说明:

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"];







labelPageTitle.Text = dtPage.Rows[0]["Title"].ToString();







for(int i = 0;i < dtReport.Rows.Count; i++)







{







GetLabel(i).Text = dtReport.Rows[i]["Title"].ToString();







if(dtReport.Rows[i]["SQLdataConnection"].ToString() != String.Empty || dtReport.Rows[i]["Sql"].ToString() != String.Empty || dtReport.Rows[i]["OleDbConnection"].ToString() != String.Empty)







{







DataGrid dg = GetDataGrid(i);







if(dtReport.Rows[i]["PageSize"].ToString() != String.Empty)







{







//分页打印







dg.AllowPaging = true;







dg.PagerStyle.Mode = PagerMode.NumericPages;







dg.PagerStyle.PageButtonCount = 10;







dg.PageSize = Convert.ToInt32(dtReport.Rows[i]["PageSize"].ToString());







}







DataSet ds = new DataSet();







//从Report.xml读取数据源信息







if(dtReport.Rows[i]["SQLdataConnection"].ToString() != String.Empty)







{







//数据提供者为SQL Server







SqlConnection Conn = new SqlConnection(dtReport.Rows[i]["SQLdataConnection"].ToString());







SqlDataAdapter myDataAdapt = new SqlDataAdapter(dtReport.Rows[i]["Sql"].ToString(),Conn);







myDataAdapt.Fill(ds,dtReport.Rows[i]["TableName"].ToString());







}







else if(dtReport.Rows[i]["OleDbConnection"].ToString() != String.Empty)







{







//数据提供者为OLE DB







OleDbConnection Conn = new OleDbConnection(dtReport.Rows[i]["OleDbConnection"].ToString());







OleDbDataAdapter myDataAdapt = new OleDbDataAdapter(dtReport.Rows[i]["Sql"].ToString(),Conn);







myDataAdapt.Fill(ds,dtReport.Rows[i]["TableName"].ToString());







}







//用通用页面显示报表







dg.DataSource = ds;







dg.DataBind();







}







}







}







catch







{







labelPageTitle.Text = "The requested report could not be found";







}







}

3.3 创建定制打印页面







需要在XML文档ApplyXSLT标签内添加对应的xslt文件名,并制作相应的样式表放在项目的xslt文件夹下即可。如下代码添加到通用打印程序中。







if(dtPage.Rows[0]["ApplyXSLT"].ToString() != string.Empty )







{







//用xsl显示报表







XmlDataDocument xmlDoc = new XmlDataDocument(ds);







XslTransform xslTran = new XslTransform();







xslTran.Load(Server.MapPath("xslt\\"+dtPage.Rows[0]["ApplyXSLT"].ToString()));







XmlTextWriter writer = new XmlTextWriter(Server.MapPath("XSLTReports.aspx"), System.Text.Encoding.UTF8);







xslTran.Transform(xmlDoc, null, writer);







writer.Close();







Response.Redirect("XSLTReports.aspx");

}

应用xslt样式表可以制作出专业的报表样式,为方便制作,在此推荐使用XMLSpy带的Stylesheet。

原文转自:http://www.ltesting.net