ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Export HTML Table to CSV file using AngularJS in ASP.Net<p>Hi&nbsp;rani,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>For this example I have used of Northwind database that you can download using the link given below.</p> <p><a href="https://www.aspsnippets.com/Articles/Install-Microsoft-Northwind-and-Pubs-Sample-databases-in-SQL-Server-Management-Studio.aspx" target="_blank">Download Northwind Database</a></p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; var app = angular.module(&#39;MyApp&#39;, []); app.controller(&#39;MyController&#39;, function ($scope, $http) { $http.post(&#34;Default.aspx/GetEmployees&#34;, { headers: { &#39;Content-Type&#39;: &#39;application/json&#39;} }) .then(function (response) { $scope.Employees = eval(response.data.d); }, function error(response) { alert(response.responseText); }); $scope.Export = function () { var rows = document.getElementById(&#34;tblEmployees&#34;).rows; var data = &#34;&#34;; for (var row = 0; row &lt; rows.length; row++) { for (var column = 0; column &lt; rows[row].cells.length; column++) { data += rows[row].cells[column].innerHTML.trim() + &#34;,&#34;; } data += &#34;\n&#34;; } if (navigator.msSaveBlob) { navigator.msSaveBlob(new Blob([data], { type: &#39;text/csv;charset=utf-8;&#39; }), &#34;Employee.csv&#34;); } else { var a = document.createElement(&#34;a&#34;); a.href = &#39;data:attachment/csv;charset=utf-8,&#39; + encodeURI(data); a.target = &#39;_blank&#39;; a.download = &#39;sample.csv&#39;; document.body.appendChild(a); a.click(); } } }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;div ng-app=&#34;MyApp&#34; ng-controller=&#34;MyController&#34;&gt; &lt;table id=&#34;tblEmployees&#34;&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Id&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;City&lt;/th&gt; &lt;th&gt;Country&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr ng-repeat=&#34;employee in Employees&#34;&gt; &lt;td&gt;{{employee.Id}}&lt;/td&gt; &lt;td&gt;{{employee.Name}}&lt;/td&gt; &lt;td&gt;{{employee.City}}&lt;/td&gt; &lt;td&gt;{{employee.Country}}&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;br /&gt; &lt;input type=&#34;button&#34; value=&#34;Export&#34; ng-click=&#34;Export()&#34; /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Web.Script.Serialization; using System.Web.Services;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Collections.Generic Imports System.Configuration Imports System.Data.SqlClient Imports System.Web.Script.Serialization Imports System.Web.Services</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">[WebMethod] public static string GetEmployees() { List&lt;object&gt; employees = new List&lt;object&gt;(); string sql = &#34;SELECT EmployeeID,FirstName,LastName,City,Country FROM Employees&#34;; using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlCommand cmd = new SqlCommand(sql)) { cmd.Connection = conn; conn.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { employees.Add(new { Id = sdr[&#34;EmployeeID&#34;], Name = sdr[&#34;FirstName&#34;] + &#34; &#34; + sdr[&#34;LastName&#34;], City = sdr[&#34;City&#34;], Country = sdr[&#34;Country&#34;] }); } } conn.Close(); } return (new JavaScriptSerializer().Serialize(employees)); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">&lt;WebMethod()&gt; Public Shared Function GetEmployees() As String Dim employees As List(Of Object) = New List(Of Object)() Dim sql As String = &#34;SELECT EmployeeID,FirstName,LastName,City,Country FROM Employees&#34; Using conn As SqlConnection = New SqlConnection() conn.ConnectionString = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using cmd As SqlCommand = New SqlCommand(sql) cmd.Connection = conn conn.Open() Using sdr As SqlDataReader = cmd.ExecuteReader() While sdr.Read() employees.Add(New With { _ .Id = sdr(&#34;EmployeeID&#34;), .Name = sdr(&#34;FirstName&#34;) &amp; &#34; &#34; &amp; sdr(&#34;LastName&#34;), .City = sdr(&#34;City&#34;), .Country = sdr(&#34;Country&#34;) }) End While End Using conn.Close() End Using Return (New JavaScriptSerializer().Serialize(employees)) End Using End Function</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/qUBPyDJ.gif" alt="" width="322" height="386" /></span></strong></p>https://www.aspforums.net:443/Threads/105154/Export-HTML-Table-to-CSV-file-using-AngularJS-in-ASPNet/https://www.aspforums.net:443/Threads/105154/Export-HTML-Table-to-CSV-file-using-AngularJS-in-ASPNet/Mon, 15 Jul 2019 05:55:31 GMT