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。