{"id":4521,"date":"2015-07-24T04:35:52","date_gmt":"2015-07-24T04:35:52","guid":{"rendered":"http:\/\/ngenioussolutions.com\/blog\/?p=60"},"modified":"2023-08-10T11:12:19","modified_gmt":"2023-08-10T11:12:19","slug":"export-data-table-to-excel-sheetcsv-on-button-click","status":"publish","type":"post","link":"https:\/\/ngenioussolutions.com\/blog\/export-data-table-to-excel-sheetcsv-on-button-click\/","title":{"rendered":"Export data table to Excel Sheet(CSV) on Button Click"},"content":{"rendered":"<h2 style=\"margin: 0px 0px 10px; text-align: left; color: #444444; text-indent: 0px; letter-spacing: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 11pt !important; font-style: normal; font-variant: normal; background-color: white;\"><strong>First You have to Create a grid view that will show details which you want to Export<\/strong><\/h2>\n<div>\n<div>&lt;asp:GridView ID=&#8221;GridView1&#8243; CssClass=&#8221;footable&#8221; runat=&#8221;server&#8221; AutoGenerateColumns=&#8221;false&#8221;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 Style=&#8221;max-width: 500px&#8221; &gt;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 &lt;Columns&gt;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;asp:BoundField DataField=&#8221;Id&#8221; HeaderText=&#8221;Id&#8221; \/&gt;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;asp:BoundField DataField=&#8221;Name&#8221; HeaderText=&#8221;Name&#8221; \/&gt;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;asp:BoundField DataField=&#8221;Company&#8221; HeaderText=&#8221;Company&#8221; \/&gt;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;asp:BoundField DataField=&#8221;Age&#8221; HeaderText=&#8221;Age&#8221; \/&gt;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 &lt;\/Columns&gt;<\/div>\n<div>\u00a0 \u00a0 &lt;\/asp:GridView&gt;<\/div>\n<\/div>\n<div><\/div>\n<p><b>Bind you details to GridView<\/b><br \/>\n<b>C# Code Behind:-<\/b><br \/>\npublic static DataTable dt; \/\/<u>Declare your dt globle to paas it in another function<\/u><br \/>\nprotected void Page_Load(object sender, EventArgs e)<br \/>\n{<br \/>\nif (!this.IsPostBack)<br \/>\n{<br \/>\ndt = new DataTable();<br \/>\ndt.Columns.AddRange(new DataColumn[4] { new DataColumn(&#8220;Id&#8221;), new DataColumn(&#8220;Name&#8221;), new DataColumn(&#8220;Company&#8221;), new DataColumn(&#8220;Age&#8221;) });<br \/>\ndt.Rows.Add(1, &#8220;Savan&#8221;, &#8220;MSOSL&#8221;, 23);<br \/>\ndt.Rows.Add(2, &#8220;Rupesh&#8221;, &#8220;KLOSJ&#8221;, 25);<br \/>\ndt.Rows.Add(3, &#8220;Rahul&#8221;, &#8220;JSKL&#8221;, 26);<br \/>\nGridView1.DataSource = dt;<br \/>\nGridView1.DataBind();<br \/>\n}<\/p>\n<p>}<\/p>\n<p><b>Create a button that will export you details to Excel<\/b><\/p>\n<p>&lt;asp:Button ID=&#8221;Button1&#8243; runat=&#8221;server&#8221; Text=&#8221;Export&#8221; OnClick=&#8221;Button1_Click&#8221; \u00a0\/&gt;<br \/>\n<b><br \/>\n<\/b><b>Call the function on click event<\/b><br \/>\n<b>C# Code Behind:-<\/b><b><br \/>\n<\/b><b><br \/>\n<\/b>protected void Button1_Click(object sender, EventArgs e)<br \/>\n{<br \/>\nExportDataTableToCSV(dt);<\/p>\n<p>}<\/p>\n<p>protected void ExportDataTableToCSV(DataTable dt)<br \/>\n{<\/p>\n<p>Response.Clear();<br \/>\nResponse.ContentType = &#8220;text\/csv&#8221;;<br \/>\nResponse.AddHeader(&#8220;content-disposition&#8221;, &#8220;attachment;filename=Customers.csv&#8221;);<\/p>\n<p>StringBuilder sb = new StringBuilder();<\/p>\n<p>for (int i = 0; i &lt; dt.Columns.Count; i++)<br \/>\n{<br \/>\nsb.Append(dt.Columns[i].ColumnName + &#8216;,&#8217;);<br \/>\n}<br \/>\nsb.Append(Environment.NewLine);<\/p>\n<p>for (int j = 0; j &lt; dt.Rows.Count; j++)<br \/>\n{<br \/>\nfor (int k = 0; k &lt; dt.Columns.Count; k++)<br \/>\n{<br \/>\nsb.Append(dt.Rows[j][k].ToString() + &#8216;,&#8217;);<br \/>\n}<br \/>\nsb.Append(Environment.NewLine);<br \/>\n}<br \/>\nResponse.Write(sb.ToString());<br \/>\nResponse.End();<br \/>\n}<\/p>\n<p><b>Screenshot<\/b><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-9453\" src=\"https:\/\/ngenioussolutions.com\/blog\/wp-content\/uploads\/2015\/07\/CSV-300x269.png\" loading=\"lazy\" alt=\"\" width=\"300\" height=\"269\" srcset=\"https:\/\/ngenioussolutions.com\/blog\/wp-content\/uploads\/2015\/07\/CSV-300x269.png 300w, https:\/\/ngenioussolutions.com\/blog\/wp-content\/uploads\/2015\/07\/CSV.png 609w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>First You have to Create a grid view that will show details which you want to Export &lt;asp:GridView ID=&#8221;GridView1&#8243; CssClass=&#8221;footable&#8221; runat=&#8221;server&#8221; AutoGenerateColumns=&#8221;false&#8221; \u00a0 \u00a0 \u00a0&#8230;<\/p>\n","protected":false},"author":1,"featured_media":8610,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4521","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-others"],"menu_order":0,"_links":{"self":[{"href":"https:\/\/ngenioussolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ngenioussolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ngenioussolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ngenioussolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ngenioussolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4521"}],"version-history":[{"count":4,"href":"https:\/\/ngenioussolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4521\/revisions"}],"predecessor-version":[{"id":9454,"href":"https:\/\/ngenioussolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4521\/revisions\/9454"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ngenioussolutions.com\/blog\/wp-json\/wp\/v2\/media\/8610"}],"wp:attachment":[{"href":"https:\/\/ngenioussolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ngenioussolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ngenioussolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}