{"id":947,"date":"2012-05-29T09:27:56","date_gmt":"2012-05-29T07:27:56","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=947"},"modified":"2012-05-29T09:27:56","modified_gmt":"2012-05-29T07:27:56","slug":"provide-export-to-csv-functionality-on-an-asp-net-page","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2012\/05\/provide-export-to-csv-functionality-on-an-asp-net-page\/","title":{"rendered":"Provide &#8220;Export to CSV&#8221; functionality on an ASP.Net page"},"content":{"rendered":"<p>I don&#8217;t do (or particularly like) ASP.Net development often but recently had to quickly add some &#8216;Export to CSV&#8217; functionality of a displayed table to a page. Personally I would have just used &#8216;copy and paste&#8217; to do it (the export) but since the user isn&#8217;t in the habit of thinking for themselves I had to create new functionality which turn out to be very nice &#8211; and even opens the door for some future more advanced functionality.<\/p>\n<p>Anyway, to achieve this is really easy if you use the\u00a0HttpContext.Current.Response class and add an attachment &#8216;header&#8217; to your page. The header must be named &#8216;<strong>content-disposition<\/strong>&#8216; and ContentType &#8216;<strong>text\/csv<\/strong>&#8216;. See the following methods I created to simply take a DataSet and convert it to &#8216;CSV file&#8217;:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class CSVExportUtils\r\n{\r\n    public void ExportDataSetToCSV(string title, DataSet ds, string columnNamesCSV = &quot;&quot;)\r\n    {\r\n        string attachment = &quot;attachment; filename=&quot; + title.Replace(&quot;,&quot;, &quot;&quot;).Replace(&quot;.&quot;, &quot;&quot;) + &quot;.csv&quot;;\r\n        HttpContext.Current.Response.Clear();\r\n        HttpContext.Current.Response.ClearHeaders();\r\n        HttpContext.Current.Response.ClearContent();\r\n        HttpContext.Current.Response.AddHeader(&quot;content-disposition&quot;, attachment);\r\n        HttpContext.Current.Response.ContentType = &quot;text\/csv&quot;;\r\n        HttpContext.Current.Response.AddHeader(&quot;Pragma&quot;, &quot;public&quot;);\r\n        if (columnNamesCSV.Length == 0)\r\n        {\r\n            WriteColumnName(ds);\r\n            foreach (DataRow r in ds.Tables&#x5B;0].Rows)\r\n            {\r\n                if (ds.Tables&#x5B;0].Columns.Count &gt; 0)\r\n                {\r\n                    string value = r&#x5B;0].ToString();\r\n                    if (value.Contains(&quot;,&quot;))\r\n                        value = &quot;\\&quot;&quot; + value + &quot;\\&quot;&quot;;\r\n                    HttpContext.Current.Response.Write(value);\r\n                    for (int i = 1; i &lt; ds.Tables&#x5B;0].Columns.Count; i++)\r\n                    {\r\n                        value = r&#x5B;i].ToString();\r\n                        if (value.Contains(&quot;,&quot;))\r\n                            value = &quot;\\&quot;&quot; + value + &quot;\\&quot;&quot;;\r\n                        HttpContext.Current.Response.Write(&quot;,&quot; + value);\r\n                    }\r\n                }\r\n                HttpContext.Current.Response.Write(Environment.NewLine);\r\n            }\r\n        }\r\n        else\r\n        {\r\n            HttpContext.Current.Response.Write(columnNamesCSV);\r\n            HttpContext.Current.Response.Write(Environment.NewLine);\r\n            string&#x5B;] columns = columnNamesCSV.Split(',');\r\n            foreach (DataRow r in ds.Tables&#x5B;0].Rows)\r\n            {\r\n                string value = r&#x5B;columns&#x5B;0]].ToString();\r\n                if (value.Contains(&quot;,&quot;))\r\n                    value = &quot;\\&quot;&quot; + value + &quot;\\&quot;&quot;;\r\n                HttpContext.Current.Response.Write(value);\r\n                for (int i = 1; i &lt; columns.Length; i++)\r\n                {\r\n                    value = r&#x5B;columns&#x5B;i]].ToString();\r\n                    if (value.Contains(&quot;,&quot;))\r\n                        value = &quot;\\&quot;&quot; + value + &quot;\\&quot;&quot;;\r\n                    HttpContext.Current.Response.Write(&quot;,&quot; + value);\r\n                }\r\n                HttpContext.Current.Response.Write(Environment.NewLine);\r\n            }\r\n        }\r\n\r\n        HttpContext.Current.Response.End();\r\n    }\r\n    private static void WriteColumnName(DataSet ds)\r\n    {\r\n        StringBuilder sb = new StringBuilder();\r\n        if (ds.Tables&#x5B;0].Columns.Count &gt; 0)\r\n        {\r\n            string value = ds.Tables&#x5B;0].Columns&#x5B;0].Caption;\r\n            if (value.Contains(&quot;,&quot;))\r\n                value = &quot;\\&quot;&quot; + value + &quot;\\&quot;&quot;;\r\n            HttpContext.Current.Response.Write(value);\r\n            for (int i = 1; i &lt; ds.Tables&#x5B;0].Columns.Count; i++)\r\n            {\r\n                value = ds.Tables&#x5B;0].Columns&#x5B;i].Caption;\r\n                if (value.Contains(&quot;,&quot;))\r\n                    value = &quot;\\&quot;&quot; + value + &quot;\\&quot;&quot;;\r\n                HttpContext.Current.Response.Write(&quot;,&quot; + value);\r\n            }\r\n        }\r\n        HttpContext.Current.Response.Write(Environment.NewLine);\r\n    }\r\n}\r\n<\/pre>\n<p>This method also allows you to specify the fields you want to export &#8211; or alternatively it just use the ones in the DataSet. For example, you can use it like this:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nif (Request.QueryString&#x5B;&quot;csvexport&quot;] != null)\r\n{\r\n    CSVExportUtils csvExportUtils = new CSVExportUtils();\r\n    csvExportUtils.ExportDataSetToCSV(fileName, dataSet, &quot;Field1,Field2,...&quot;);\r\n}\r\n<\/pre>\n<p>The calling page can then simply have a hyperlink like this:<\/p>\n<p>SomePage.aspx?csvexport=Yes<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t do (or particularly like) ASP.Net development often but recently had to quickly add some &#8216;Export to CSV&#8217; functionality of a displayed table to a page. Personally I would have just used &#8216;copy and paste&#8217; to do it (the &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2012\/05\/provide-export-to-csv-functionality-on-an-asp-net-page\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,194,8,196,303,195],"class_list":["post-947","post","type-post","status-publish","format-standard","hentry","category-development","tag-net","tag-asp-net","tag-c","tag-csv","tag-development","tag-export"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=947"}],"version-history":[{"count":3,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/947\/revisions"}],"predecessor-version":[{"id":950,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/947\/revisions\/950"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}