We help IT Professionals succeed at work.

Transform dataset to excel with schema information

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();

XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
xslCompiledTransform.Transform(xdd, null, sw);

// Excel.xsl

<xsl:stylesheet version="1.0"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" > 
<xsl:template match="/">
  <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"

<xsl:template match="/*">
  <xsl:attribute name="ss:Name">
  <xsl:value-of select="local-name(/*/*)"/>
    <Table x:FullColumns="1" x:FullRows="1">
        <xsl:for-each select="*[position() = 1]/*">
          <Cell><Data ss:Type="String">
          <xsl:value-of select="local-name()"/>

<xsl:template match="/*/*">

<xsl:template match="/*/*/*">
    <xsl:when test=".='-999999'">
        <Data ss:Type="String">
        <Data ss:Type="String">
          <xsl:value-of select="."/>

Watch Question

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();


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


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.


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.