Link to home
Start Free TrialLog in
Avatar of karanba
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(HttpContext.Current.Server.MapPath("/include/TransformXSLs/Excel.xsl"));
xslCompiledTransform.Transform(xdd, null, sw);


// Excel.xsl

<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="/">
  <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">
    <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>
Avatar of bungHoc
bungHoc
Flag of Viet Nam image

You can try this:
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.GetManifestResourceStream(typeof (WorkbookEngine), "Excel.xsl"));
         XmlTextReader xRdr = new XmlTextReader(reader);

         xt.Load(xRdr, null, null);
         StringWriter sw = new StringWriter();
         xt.Transform(xmlDataDoc, null, sw, null);
         
      return sw.ToString();
}

Avatar of karanba
karanba

ASKER

what is WorkbookEngine, what to include for this?
ASKER CERTIFIED SOLUTION
Avatar of bungHoc
bungHoc
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of karanba

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.