Thursday, December 10, 2009

Convert DataSet to Excel document

Below is a quick description on how to convert a DataSet to an Excel sheet. It applies XslTransform to a DataSet and writes the results to the Response.OutputStream of a web request. This will allow for a user to save or open the data as Excel.

Populate a DataSet:
using (DataSet ds = Service.GetSummary(UserId, Filter1, Filter2))
{
  ...
}

Configure Response object:
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";

Configure XslTransform:
XmlDataDocument xmlDoc = new XmlDataDocument(ds);
XslTransform xslTran = new XslTransform();
xslTran.Load(Server.MapPath("ExcelTransformFormat.xslt"));
Note: "ExcelTransformFormat.xslt" will need to be adjusted. Below is an example, but it will need to be modified to reflect the DataSet it is being used to transform.

Write results to Response:
xslTran.Transform(xmlDoc, null, Response.OutputStream, null);
Response.AppendHeader("Content-Disposition", "attachment; filename=Name.xls");
Response.End();

Example .xslt Transform File
<xsl:stylesheet version="1.0"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" >

<xsl:template match="NewDataSet">

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">

<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s21">
<Font ss:Bold="1"/>
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
</Style>
<Style ss:ID="s22">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
<Font ss:Bold="1"/>
<Interior ss:Color="#99CCFF" ss:Pattern="Solid"/>
</Style>
</Styles>
<Worksheet>
<xsl:attribute name="ss:Name">
<xsl:value-of select='concat("RGN", Table/region, "DST", Table/district)'/>
</xsl:attribute>
<Table ss:ExpandedColumnCount="17">
<xsl:attribute name="ss:ExpandedRowCount" >
<xsl:value-of select="count(Table)+10"/>
</xsl:attribute>
<Column ss:AutoFitWidth="0" ss:Width="30"/>
<Column ss:AutoFitWidth="0" ss:Width="30"/>
<Column ss:AutoFitWidth="0" ss:Width="50"/>
<Row>
<Cell ss:StyleID="s21"><Data ss:Type="String">RGN</Data></Cell>
<Cell ss:StyleID="s21"><Data ss:Type="String">DST</Data></Cell>
<Cell ss:StyleID="s21"><Data ss:Type="String">Location</Data></Cell>
</Row>
<xsl:apply-templates select="Table"/>
</Table>
</Worksheet>
</Workbook>
</xsl:template>
<xsl:template match="Table">
<Row>
<Cell><Data ss:Type="String"><xsl:value-of select="region"/></Data></Cell>
<Cell><Data ss:Type="String"><xsl:value-of select="district"/></Data></Cell>
<Cell><Data ss:Type="String"><xsl:value-of select="location"/></Data></Cell>
</Row>
</xsl:template>
</xsl:stylesheet>

No comments: