应用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。