How to Add a Title to an Excel Spreadsheet From XSL
The article Convert XML To an Excel Spreadsheet Using XSL explains how to transform an XML file into an Excel spreadsheet using XSL. The question is, how can you add a title to the Excel spreadsheet from XSL? To view the original XSL file visit the article link above.
First add an XSL parameter to the stylesheet immediately after the xsl:stylesheet declaration:
<xsl:param name="title" />
Second change this template in the XSL file:
<xsl:template match="/*">
<Worksheet>
<xsl:attribute name="ss:Name">
<xsl:value-of select="local-name(/*/*)"/>
</xsl:attribute>
<Table x:FullColumns="1" x:FullRows="1">
<Row>
<Cell><Data ss:Type="String">
<xsl:value-of select="$title"/>
</Data></Cell>
</Row>
<Row>
<xsl:for-each select="*[position() = 1]/*">
<Cell><Data ss:Type="String">
<xsl:value-of select="local-name()"/>
</Data></Cell>
</xsl:for-each>
</Row>
<xsl:apply-templates/>
</Table>
</Worksheet>
</xsl:template>
Third use this web form:
<%@ Page Language="c#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<script language="C#" runat="server">
public void Page_Load(Object sender, EventArgs E) {
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("Authors.xml"));
XsltArgumentList xal = new XsltArgumentList();
xal.AddParam("title", "", "Author List");
XmlDataDocument xdd = new XmlDataDocument(ds);
XslTransform xt = new XslTransform();
xt.Load(Server.MapPath("Excel.xsl"));
xt.Transform(xdd, xal, Response.OutputStream);
Response.End();
}
</script>