karanba
asked on
Transform dataset to excel with schema information
Hi, I use xml xsl transformation to export datasets as excel file. Here is the code, The only thing I need is my Excel.xsl do not look schema information. So it transform all the data as ss:Type="String". I need an xsl that take cares of orjinal data types and convert according to that. So do you have or know an excel.xsl that convert with schema, my current xsl is below.
// Dataset ds is ready
StringWriter sw = new StringWriter();
StringWriter stw = new StringWriter();
ds.WriteXml(stw, XmlWriteMode.WriteSchema);
XmlDataDocument xdd = new XmlDataDocument();
xdd.LoadXml(stw.ToString() );
XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
xslCompiledTransform.Load( HttpContex t.Current. Server.Map Path("/inc lude/Trans formXSLs/E xcel.xsl") );
xslCompiledTransform.Trans form(xdd, null, sw);
// Excel.xsl
<xsl:stylesheet version="1.0"
xmlns="urn:schemas-microso ft-com:off ice:spread sheet"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-m icrosoft-c om:xslt"
xmlns:user="urn:my-scripts "
xmlns:o="urn:schemas-micro soft-com:o ffice:offi ce"
xmlns:x="urn:schemas-micro soft-com:o ffice:exce l"
xmlns:ss="urn:schemas-micr osoft-com: office:spr eadsheet" >
<xsl:template match="/">
<Workbook xmlns="urn:schemas-microso ft-com:off ice:spread sheet"
xmlns:o="urn:schemas-micro soft-com:o ffice:offi ce"
xmlns:x="urn:schemas-micro soft-com:o ffice:exce l"
xmlns:ss="urn:schemas-micr osoft-com: office:spr eadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<xsl:apply-templates/>
</Workbook>
</xsl:template>
<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>
<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>
<xsl:template match="/*/*">
<Row>
<xsl:apply-templates/>
</Row>
</xsl:template>
<xsl:template match="/*/*/*">
<xsl:choose>
<xsl:when test=".='-999999'">
<Cell>
<Data ss:Type="String">
</Data>
</Cell>
</xsl:when>
<xsl:otherwise>
<Cell>
<Data ss:Type="String">
<xsl:value-of select="."/>
</Data>
</Cell>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
// Dataset ds is ready
StringWriter sw = new StringWriter();
StringWriter stw = new StringWriter();
ds.WriteXml(stw, XmlWriteMode.WriteSchema);
XmlDataDocument xdd = new XmlDataDocument();
xdd.LoadXml(stw.ToString()
XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
xslCompiledTransform.Load(
xslCompiledTransform.Trans
// Excel.xsl
<xsl:stylesheet version="1.0"
xmlns="urn:schemas-microso
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-m
xmlns:user="urn:my-scripts
xmlns:o="urn:schemas-micro
xmlns:x="urn:schemas-micro
xmlns:ss="urn:schemas-micr
<xsl:template match="/">
<Workbook xmlns="urn:schemas-microso
xmlns:o="urn:schemas-micro
xmlns:x="urn:schemas-micro
xmlns:ss="urn:schemas-micr
xmlns:html="http://www.w3.org/TR/REC-html40">
<xsl:apply-templates/>
</Workbook>
</xsl:template>
<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>
<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>
<xsl:template match="/*/*">
<Row>
<xsl:apply-templates/>
</Row>
</xsl:template>
<xsl:template match="/*/*/*">
<xsl:choose>
<xsl:when test=".='-999999'">
<Cell>
<Data ss:Type="String">
</Data>
</Cell>
</xsl:when>
<xsl:otherwise>
<Cell>
<Data ss:Type="String">
<xsl:value-of select="."/>
</Data>
</Cell>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
ASKER
what is WorkbookEngine, what to include for this?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Ok, I think I could not tell want I want excatly, but I get solution by writing a simple method for converting dataset to xml and it works, The point that I need is to set types with xsl. thanks for your trys.
Convert DataSet to XML using XmlDataDocument, then perform XSL Transform and last return as Excel
Here is the method that does the trick. I'm not sure how it would handle dataset with a couple of tables though :D
public static string WriteToExcel(DataSet ds)
{
XmlDataDocument xmlDataDoc = new XmlDataDocument(ds);
XslTransform xt = new XslTransform();
StreamReader reader = new StreamReader(typeof (WorkbookEngine).Assembly.
XmlTextReader xRdr = new XmlTextReader(reader);
xt.Load(xRdr, null, null);
StringWriter sw = new StringWriter();
xt.Transform(xmlDataDoc, null, sw, null);
return sw.ToString();
}