We help IT Professionals succeed at work.

Transform dataset to excel with schema information

2,989 Views
Last Modified: 2013-11-18
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>
Comment
Watch Question

Commented:
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();
}

Author

Commented:
what is WorkbookEngine, what to include for this?
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.