Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stop the leading 0's from being stripped off when exporting to excel from a datatable?
    text
    copied!<p>I am running into the same problem as in this question:</p> <p><a href="https://stackoverflow.com/questions/22879/how-do-you-prevent-leading-zeros-from-being-stripped-when-importing-an-excel-doc">How do you prevent leading zeros from being stripped when importing an excel doc using c#</a></p> <p>But I am not sure if that is the best solution for my scenario. Here is the code I am using to do the export. Does anyone know what I can change to prevent the leading 0's from being stripped off?</p> <pre><code>private static void Export_with_XSLT_Web(DataSet dsExport, string[] sHeaders, string[] sFileds, ExportFormat FormatType, string FileName) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + FileName); } // XSLT to use for transforming this dataset. MemoryStream stream = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8); CreateStylesheet(writer, sHeaders, sFileds, FormatType); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); XmlDataDocument xmlDoc = new XmlDataDocument(dsExport); XslTransform xslTran = new XslTransform(); xslTran.Load(new XmlTextReader(stream), null, null); using(StringWriter sw = new StringWriter()) { xslTran.Transform(xmlDoc, null, sw, null); HttpContext.Current.Response.Write(sw.ToString()); writer.Close(); stream.Close(); HttpContext.Current.Response.End(); } } } </code></pre> <p>Here is the method that creates the stylesheet, is there anything in here that I can change to bring in some or all fields as text.</p> <pre><code>private static void CreateStylesheet(XmlTextWriter writer, string[] sHeaders, string[] sFileds, ExportFormat FormatType) { try { // xsl:stylesheet string ns = "http://www.w3.org/1999/XSL/Transform"; writer.Formatting = Formatting.Indented; writer.WriteStartDocument(); writer.WriteStartElement("xsl", "stylesheet", ns); writer.WriteAttributeString("version", "1.0"); writer.WriteStartElement("xsl:output"); writer.WriteAttributeString("method", "text"); writer.WriteAttributeString("version", "4.0"); writer.WriteEndElement(); // xsl-template writer.WriteStartElement("xsl:template"); writer.WriteAttributeString("match", "/"); // xsl:value-of for headers for(int i = 0; i &lt; sHeaders.Length; i++) { writer.WriteString("\""); writer.WriteStartElement("xsl:value-of"); writer.WriteAttributeString("select", "'" + sHeaders[i] + "'"); writer.WriteEndElement(); // xsl:value-of writer.WriteString("\""); } // xsl:for-each writer.WriteStartElement("xsl:for-each"); writer.WriteAttributeString("select", "Export/Values"); writer.WriteString("\r\n"); // xsl:value-of for data fields for(int i = 0; i &lt; sFileds.Length; i++) { writer.WriteString("\""); writer.WriteStartElement("xsl:value-of"); writer.WriteAttributeString("select", sFileds[i]); writer.WriteEndElement(); // xsl:value-of writer.WriteString("\""); } writer.WriteEndElement(); // xsl:for-each writer.WriteEndElement(); // xsl-template writer.WriteEndElement(); // xsl:stylesheet writer.WriteEndDocument(); } catch(Exception Ex) { throw Ex; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload