当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET应用XML技术实现Web报表打印

ASP.NET
在图片上加入图片版权信息
Peer-to-Peer (P2P) communication across middleboxes(翻译4)
今天完成了.net compact framework 加 web service的演练.
Cordbg, Dumpbin, Ildasm, 的一些教程。
asp和asp.net的session共用
VB连接SQL数据库的模块
消除图片在ie中缓存而无法更新的问题
说说使用static和const关键字
怎样解决thephile中的数据库由于排序造成的问题:对 text 数据类型不支持代码页转...
.net分布式事务例子
Internet Explorer 编程简述(二)
使用SqlParameter参数返回值时遇到的问题
vb可不可以实现虚拟中断
C#下Socket对象的BeginReceive方法,执行后竟然不调用AsyncCallback里的回调函数
坚持学asp.net:(十一)
[C#][正则表达式]寻找匹配的Groups的几种方法
面向服务的体系结构概述
Windows Form 和 UserControl
VB中類模塊實現與C++中類實現的比較(1)
下载Oracle数据库中的Blob二进制文件,实例!

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 260 ::
收藏到网摘: n/a

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文件夹下即可。如下代码添加到通用打印程序中。