XML file:
<?
xml version="1.0" encoding="utf-8"
?>
< ric >
< catalog >
< book price ="75" >
< author > Kalen Delaney </ author >
< name > Inside SQL Server 2000 </ name >
</ book >
< book price ="200" >
< author > Ken Henderson </ author >
< name > The Guru's Guide to SQL Server Architecture </ name >
</ book >
</ catalog >
</ ric >
< ric >
< catalog >
< book price ="75" >
< author > Kalen Delaney </ author >
< name > Inside SQL Server 2000 </ name >
</ book >
< book price ="200" >
< author > Ken Henderson </ author >
< name > The Guru's Guide to SQL Server Architecture </ name >
</ book >
</ catalog >
</ ric >
XSLT file:
<?
xml version="1.0" encoding="utf-8"
?>
< xsl:stylesheet version ="1.0"
xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >
< xsl:output method ="html" encoding ="utf-8" />
< xsl:template match ="/" >
< html >
< body >
< table cellpadding ="0" cellspacing ="0" border ="1" style ="border-collapse:collapse;font-size:14px;" >
< tr >
< th > Book Name </ th >
< th > Author </ th >
< th > Price </ th >
</ tr >
< xsl:for-each select ="//ric/catalog/book" >
< tr >
< td >
< xsl:value-of select ="name" ></ xsl:value-of >
</ td >
< td >
< xsl:value-of select ="author" ></ xsl:value-of >
</ td >
< td >
< xsl:value-of select ="@price" />
</ td >
</ tr >
</ xsl:for-each >
</ table >
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
< xsl:stylesheet version ="1.0"
xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >
< xsl:output method ="html" encoding ="utf-8" />
< xsl:template match ="/" >
< html >
< body >
< table cellpadding ="0" cellspacing ="0" border ="1" style ="border-collapse:collapse;font-size:14px;" >
< tr >
< th > Book Name </ th >
< th > Author </ th >
< th > Price </ th >
</ tr >
< xsl:for-each select ="//ric/catalog/book" >
< tr >
< td >
< xsl:value-of select ="name" ></ xsl:value-of >
</ td >
< td >
< xsl:value-of select ="author" ></ xsl:value-of >
</ td >
< td >
< xsl:value-of select ="@price" />
</ td >
</ tr >
</ xsl:for-each >
</ table >
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
default.aspx如下,只是个简单的示例,服务器端没有任何代码。
< input type ="button" value ="Download" onclick ="window.open('download.aspx','download');" />
< iframe id ="download" name ="download" src ="" style ="display:none;" ></ iframe >download.aspx:
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " download.aspx.cs " Inherits = " download " %>download.aspx.cs:
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Text;
using System.IO;
public partial class download : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
string fileName = " aaa_ " + DateTime.Now.ToString( " yyyyMMddHHmmss " ) + " .xls " ;
string fullPath = Server.MapPath(fileName);
XmlTextWriter writer = new XmlTextWriter(fullPath, Encoding.UTF8);
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(Server.MapPath( " a.xslt " ));
transform.Transform(Server.MapPath( " a.xml " ), null , writer);
writer.Close();
Response.Clear();
Response.AddHeader( " Content-Disposition " , " attachment; filename= " + fileName);
Response.WriteFile(fullPath);
}
}