{"id":664,"date":"2014-02-27T00:02:28","date_gmt":"2014-02-27T00:02:28","guid":{"rendered":"http:\/\/elbsolutions.com\/projects\/?p=664"},"modified":"2022-02-03T11:25:02","modified_gmt":"2022-02-03T17:25:02","slug":"simple-download-gridview-well-try","status":"publish","type":"post","link":"https:\/\/elbsolutions.com\/projects\/simple-download-gridview-well-try\/","title":{"rendered":"Simple download from GridView? Well, no &#8211; it should be &#8211; try this"},"content":{"rendered":"<p>So the code in theory is super slick &#8211; just use a StringWriter and HtmlWriter and in 2-5 commands it is downloaded to the client (see this link and the caveats that people use to get things working!). NO SUCH LUCK HERE. The main reason is that I have 2 columns with buttons in them at the front &#8211; the whole grid is DYNAMICALLY created by myself in the code (that is not included below). The c# code is included below.<\/p>\n<p>So using some other researching I found a couple of concepts that worked together. I am sure there are some other optimizations but this worked.<\/p>\n<p>First, I converted the GridView contents to a DataTable and for all columns 3 or more columns over I filled the datatable. <a href=\"http:\/\/www.dotnetgallery.com\/kb\/resource19-How-to-export-data-to-excel-from-gridview-and-DataTable-using-aspnet.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">Thanks to this fellow<\/a>. Because the Gridview has all the source info including column headers etc. it works out great. <!--more-->My version even takes care of duplicate column heading names.<\/p>\n<p>Since it downloads CSV, I already knew the <strong>format that Excel likes<\/strong> &#8211; it is surrounded by quotes and comma separated. I also took care of escaping double quotes with two double quotes and also ensuring that a \\r\\n (vbCrLf) was included at the end. Then Excel will not complain about anything.<\/p>\n<p>One oddity was that empty Gridview cells export &amp;nbsp; so I replaced this with an empty string. One irritant remains and I don&#8217;t think I need to fix it for my client but 001 turns into 1 but that is what Excel does to the data. Once I figure out how to ELEGANTLY know the datatype (cause the sql command that gets the data knows what datatype it is &#8211; perhaps I should just save it as I retrieve it)<\/p>\n<p>Importing? That is another article!<\/p>\n<p>Just call the routine from a button or linked text asp item.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\nprotected void ExportToExcel()\r\n {\r\n int ignoreXcols = 3;\r\n DataTable dt = new DataTable();\r\n List&lt;string&gt; cols = new List&lt;string&gt;();\r\n for (int i = 0; i &lt; GridView1.Columns.Count; i++)\r\n {\r\n\r\n if (i &gt;= ignoreXcols)\r\n {\r\n string colName = GridView1.Columns&#x5B;i].HeaderText;\r\n\r\n if (dt.Columns.Contains(colName))\r\n {\r\n colName += i.ToString(); \/\/add i to the end so there will be no conflicts\r\n }\r\n cols.Add(colName);\r\n dt.Columns.Add(colName);\r\n\r\n }\r\n }\r\n for (int i = 0; i &lt; GridView1.Rows.Count; i++)\r\n {\r\n DataRow rw = dt.NewRow();\r\n\r\nfor (int j = 0; j &lt; GridView1.Columns.Count; j++)\r\n {\r\n\r\n if (j &gt;= ignoreXcols)\r\n {\r\n\r\n string temp = GridView1.Rows&#x5B;i].Cells&#x5B;j].Text; \/\/improvement - this should be dependant on data type if we include the ' or not\r\n if (&quot;&amp;nbsp;&quot; == temp)\r\n {\r\n temp = &quot;&quot;; \/\/null value\r\n }\r\n rw&#x5B;cols&#x5B;j - ignoreXcols]] = temp;\r\n }\r\n\r\n\/\/ xlWorkSheet.Cells&#x5B;i + 2, j + 1] = cell.Value;\r\n }\r\n dt.Rows.Add(rw);\r\n }\r\n\r\nDateTime dtime = DateTime.Now;\r\n string filename = &quot;SourceData &quot; + _setName + _moduleType + &quot; &quot; + dtime.ToString(&quot;yyyyMMdd HHmmss&quot;) + &quot;.csv&quot;;\r\n UploadDataTableToExcel(dt, filename);\r\n }\r\n protected void UploadDataTableToExcel(DataTable dtEmp, string filename)\r\n {\r\n string attachment = &quot;attachment; filename=&quot; + filename;\r\n Response.ClearContent();\r\n Response.AddHeader(&quot;content-disposition&quot;, attachment);\r\n Response.ContentType = &quot;application\/vnd.ms-excel&quot;;\r\n string tab = string.Empty;\r\n foreach (DataColumn dtcol in dtEmp.Columns)\r\n {\r\n Response.Write(tab + &quot;\\&quot;&quot; + dtcol.ColumnName + &quot;\\&quot;&quot;);\r\n tab = &quot;,&quot;;\r\n }\r\n Response.Write(&quot;\\r\\n&quot;);\r\n foreach (DataRow dr in dtEmp.Rows)\r\n {\r\n tab = &quot;&quot;;\r\n for (int j = 0; j &lt; dtEmp.Columns.Count; j++)\r\n {\r\n\r\nResponse.Write(tab + &quot;\\&quot;&quot; + Convert.ToString(dr&#x5B;j]).Replace(&quot;\\&quot;&quot;, &quot;\\&quot;\\&quot;&quot;) + &quot;\\&quot;&quot;);\r\n tab = &quot;,&quot;;\r\n }\r\n Response.Write(&quot;\\r\\n&quot;);\r\n }\r\n Response.End();\r\n }\r\n\r\n<\/pre>\n<p>Other links &#8211; thanks to everyone<\/p>\n<ul>\n<li>t<a href=\"http:\/\/blogs.msdn.com\/b\/erikaehrli\/archive\/2009\/01\/30\/how-to-export-data-to-excel-from-an-asp-net-application-avoid-the-file-format-differ-prompt.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">he start of slickness<\/a> &#8211; but it dind&#8217;t work in my situation. Don&#8217;t forget to turn off sorting and ??? to use it just before the renderControl statement. That didn&#8217;t fix my issue unfortunately<\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/643643\/how-to-create-and-download-excel-document-using-asp-net\" target=\"_blank\" rel=\"noopener noreferrer\">using an Excel file as a template and filling it in cell by cell from your Gridview<\/a><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><a href=\"http:\/\/www.dotnetgallery.com\/kb\/resource19-How-to-export-data-to-excel-from-gridview-and-DataTable-using-aspnet.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">the person who helped be get over the GridView to Datatable and download to Excel is this article.<\/a><\/li>\n<li><a href=\"http:\/\/www.aspsnippets.com\/Articles\/How-to-Import-Excel-Sheet-data-into-SQL-Server-using-ASPNet.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">importing<\/a>\u00a0and <a href=\"http:\/\/www.codeproject.com\/Tips\/300639\/Import-Data-from-excel-to-SQL-server-using-Csharp\" target=\"_blank\" rel=\"noopener noreferrer\">more<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>So the code in theory is super slick &#8211; just use a StringWriter and HtmlWriter and in 2-5 commands it is downloaded to the client (see this link and the caveats that people use to get things working!). NO SUCH LUCK HERE. The main reason is that I have 2 columns with buttons in them [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-664","post","type-post","status-publish","format-standard","hentry","category-general"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Simple download from GridView? Well, no - it should be - try this - ELB Solutions.com Inc.<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/elbsolutions.com\/projects\/simple-download-gridview-well-try\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Simple download from GridView? Well, no - it should be - try this - ELB Solutions.com Inc.\" \/>\n<meta property=\"og:description\" content=\"So the code in theory is super slick &#8211; just use a StringWriter and HtmlWriter and in 2-5 commands it is downloaded to the client (see this link and the caveats that people use to get things working!). NO SUCH LUCK HERE. The main reason is that I have 2 columns with buttons in them [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/elbsolutions.com\/projects\/simple-download-gridview-well-try\/\" \/>\n<meta property=\"og:site_name\" content=\"ELB Solutions.com Inc.\" \/>\n<meta property=\"article:published_time\" content=\"2014-02-27T00:02:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-02-03T17:25:02+00:00\" \/>\n<meta name=\"author\" content=\"Etienne Bley\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Etienne Bley\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/simple-download-gridview-well-try\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/simple-download-gridview-well-try\\\/\"},\"author\":{\"name\":\"Etienne Bley\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#\\\/schema\\\/person\\\/51e717c68f4f5917c63baf88f0896c39\"},\"headline\":\"Simple download from GridView? Well, no &#8211; it should be &#8211; try this\",\"datePublished\":\"2014-02-27T00:02:28+00:00\",\"dateModified\":\"2022-02-03T17:25:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/simple-download-gridview-well-try\\\/\"},\"wordCount\":675,\"articleSection\":[\"General\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/simple-download-gridview-well-try\\\/\",\"url\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/simple-download-gridview-well-try\\\/\",\"name\":\"Simple download from GridView? Well, no - it should be - try this - ELB Solutions.com Inc.\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#website\"},\"datePublished\":\"2014-02-27T00:02:28+00:00\",\"dateModified\":\"2022-02-03T17:25:02+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#\\\/schema\\\/person\\\/51e717c68f4f5917c63baf88f0896c39\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/simple-download-gridview-well-try\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/simple-download-gridview-well-try\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/simple-download-gridview-well-try\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Simple download from GridView? Well, no &#8211; it should be &#8211; try this\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#website\",\"url\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/\",\"name\":\"ELB Solutions.com Inc.\",\"description\":\"Bringing all your IT Pieces together\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/#\\\/schema\\\/person\\\/51e717c68f4f5917c63baf88f0896c39\",\"name\":\"Etienne Bley\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g\",\"caption\":\"Etienne Bley\"},\"url\":\"https:\\\/\\\/elbsolutions.com\\\/projects\\\/author\\\/etienne-bley\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Simple download from GridView? Well, no - it should be - try this - ELB Solutions.com Inc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/elbsolutions.com\/projects\/simple-download-gridview-well-try\/","og_locale":"en_US","og_type":"article","og_title":"Simple download from GridView? Well, no - it should be - try this - ELB Solutions.com Inc.","og_description":"So the code in theory is super slick &#8211; just use a StringWriter and HtmlWriter and in 2-5 commands it is downloaded to the client (see this link and the caveats that people use to get things working!). NO SUCH LUCK HERE. The main reason is that I have 2 columns with buttons in them [&hellip;]","og_url":"https:\/\/elbsolutions.com\/projects\/simple-download-gridview-well-try\/","og_site_name":"ELB Solutions.com Inc.","article_published_time":"2014-02-27T00:02:28+00:00","article_modified_time":"2022-02-03T17:25:02+00:00","author":"Etienne Bley","twitter_misc":{"Written by":"Etienne Bley","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/elbsolutions.com\/projects\/simple-download-gridview-well-try\/#article","isPartOf":{"@id":"https:\/\/elbsolutions.com\/projects\/simple-download-gridview-well-try\/"},"author":{"name":"Etienne Bley","@id":"https:\/\/elbsolutions.com\/projects\/#\/schema\/person\/51e717c68f4f5917c63baf88f0896c39"},"headline":"Simple download from GridView? Well, no &#8211; it should be &#8211; try this","datePublished":"2014-02-27T00:02:28+00:00","dateModified":"2022-02-03T17:25:02+00:00","mainEntityOfPage":{"@id":"https:\/\/elbsolutions.com\/projects\/simple-download-gridview-well-try\/"},"wordCount":675,"articleSection":["General"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/elbsolutions.com\/projects\/simple-download-gridview-well-try\/","url":"https:\/\/elbsolutions.com\/projects\/simple-download-gridview-well-try\/","name":"Simple download from GridView? Well, no - it should be - try this - ELB Solutions.com Inc.","isPartOf":{"@id":"https:\/\/elbsolutions.com\/projects\/#website"},"datePublished":"2014-02-27T00:02:28+00:00","dateModified":"2022-02-03T17:25:02+00:00","author":{"@id":"https:\/\/elbsolutions.com\/projects\/#\/schema\/person\/51e717c68f4f5917c63baf88f0896c39"},"breadcrumb":{"@id":"https:\/\/elbsolutions.com\/projects\/simple-download-gridview-well-try\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/elbsolutions.com\/projects\/simple-download-gridview-well-try\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/elbsolutions.com\/projects\/simple-download-gridview-well-try\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/elbsolutions.com\/projects\/"},{"@type":"ListItem","position":2,"name":"Simple download from GridView? Well, no &#8211; it should be &#8211; try this"}]},{"@type":"WebSite","@id":"https:\/\/elbsolutions.com\/projects\/#website","url":"https:\/\/elbsolutions.com\/projects\/","name":"ELB Solutions.com Inc.","description":"Bringing all your IT Pieces together","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/elbsolutions.com\/projects\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/elbsolutions.com\/projects\/#\/schema\/person\/51e717c68f4f5917c63baf88f0896c39","name":"Etienne Bley","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f8971dfb65b25b768415568f83247df4057f15d037137e386928a804e2c997b9?s=96&d=mm&r=g","caption":"Etienne Bley"},"url":"https:\/\/elbsolutions.com\/projects\/author\/etienne-bley\/"}]}},"_links":{"self":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts\/664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/comments?post=664"}],"version-history":[{"count":6,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts\/664\/revisions"}],"predecessor-version":[{"id":2819,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/posts\/664\/revisions\/2819"}],"wp:attachment":[{"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/media?parent=664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/categories?post=664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elbsolutions.com\/projects\/wp-json\/wp\/v2\/tags?post=664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}