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 Word Document (doc) file using AngularJS in ASP.Net<p>Hi&nbsp;<a class="username" rel="rani">rani,</a></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;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, $window) { $http.post(&#34;Default.aspx/BindEmployees&#34;, { headers: { &#39;Content-Type&#39;: &#39;application/json&#39;} }) .then(function (response) { $scope.Employees = eval(response.data.d); }); $scope.Export = function () { var html = angular.element(document.querySelector(&#39;#tblCustomer&#39;))[0].innerHTML; var header = &#34;&lt;html xmlns:o=&#39;urn:schemas-microsoft-com:office:office&#39; &#34; + &#34;xmlns:w=&#39;urn:schemas-microsoft-com:office:word&#39; &#34; + &#34;xmlns=&#39;http://www.w3.org/TR/REC-html40&#39;&gt;&#34; + &#34;&lt;head&gt;&lt;meta charset=&#39;utf-8&#39;&gt;&lt;title&gt;Export Table to Word&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&#34;; var footer = &#34;&lt;/body&gt;&lt;/html&gt;&#34;; var sourceHTML = header + &#34;&lt;table border=&#39;1&#39; cellpadding=&#39;1&#39; cellspacing=&#39;1&#39;&gt;&#34; + html + &#34;&lt;/table&gt;&#34; + footer; if (navigator.msSaveBlob) { // IE 10+ navigator.msSaveBlob(new Blob([sourceHTML], { type: &#39;application/vnd.ms-word&#39; }), &#34;Employee.doc&#34;); } else { var source = &#39;data:application/vnd.ms-word;charset=utf-8,&#39; + encodeURIComponent(sourceHTML); var fileDownload = document.createElement(&#34;a&#34;); document.body.appendChild(fileDownload); fileDownload.href = source; fileDownload.download = &#39;Employee.doc&#39;; fileDownload.click(); document.body.removeChild(fileDownload); } } }); &lt;/script&gt; &lt;div ng-app=&#34;MyApp&#34; ng-controller=&#34;MyController&#34;&gt; &lt;table id=&#34;tblCustomer&#34;&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Id&lt;/th&gt; &lt;th&gt;First Name&lt;/th&gt; &lt;th&gt;Last 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.EmployeeId }}&lt;/td&gt; &lt;td&gt;{{ employee.FirstName }}&lt;/td&gt; &lt;td&gt;{{ employee.LastName }}&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;</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 BindEmployees() { 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 { EmployeeId = sdr[&#34;EmployeeID&#34;], FirstName = sdr[&#34;FirstName&#34;], LastName = 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 BindEmployees() 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 { .EmployeeId = sdr(&#34;EmployeeID&#34;), .FirstName = sdr(&#34;FirstName&#34;), .LastName = 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/BCiDFq5.gif" alt="" width="381" height="387" /></span></strong></p>https://www.aspforums.net:443/Threads/730564/Export-HTML-Table-to-Word-Document-doc-file-using-AngularJS-in-ASPNet/https://www.aspforums.net:443/Threads/730564/Export-HTML-Table-to-Word-Document-doc-file-using-AngularJS-in-ASPNet/Fri, 12 Jul 2019 08:20:01 GMT