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.Import Excel data to Database using AngularJS in ASP.Net MVC<p>Hi <a class="username" rel="mahesh213"> mahesh213</a>,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <pre class="brush: csharp">using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web.Mvc; using System.Web.Script.Serialization;</pre> <p><strong><span style="text-decoration: underline;">Controller</span></strong></p> <pre class="brush: csharp">public class HomeController : Controller { // GET: /Home/ public ActionResult Index() { return View(); } [HttpPost] public JsonResult Save(List&lt;object&gt; customers) { DataTable dt = new DataTable(); JavaScriptSerializer serializer = new JavaScriptSerializer(); var columnNames = ((Dictionary&lt;string, object&gt;)serializer.DeserializeObject(customers[0].ToString())).Select(x =&gt; x.Key).ToList(); for (int i = 0; i &lt; columnNames.Count; i++) { dt.Columns.Add(columnNames[i]); } foreach (string customer in customers) { DataRow dr = dt.NewRow(); for (int i = 0; i &lt; columnNames.Count(); i++) { Dictionary&lt;string, object&gt; keyValue = ((Dictionary&lt;string, object&gt;)serializer.DeserializeObject(customer)); foreach (KeyValuePair&lt;string, object&gt; item in keyValue) { if (item.Key == columnNames[i].ToString()) { dr[columnNames[i]] = item.Value; } } } dt.Rows.Add(dr); } if (dt.Rows.Count &gt; 0) { string consString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(consString)) { using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con)) { //Set the database table name. sqlBulkCopy.DestinationTableName = &#34;dbo.Customers&#34;; con.Open(); sqlBulkCopy.WriteToServer(dt); con.Close(); } } } return Json(customers.Count().ToString()); } }</pre> <p><strong><span style="text-decoration: underline;">View</span></strong></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head&gt; &lt;title&gt;Index&lt;/title&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css&#34; /&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.8/angular.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/danialfarid-angular-file-upload/12.2.13/ng-file-upload.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.5/xlsx.full.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.5/jszip.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; var app = angular.module(&#39;MyApp&#39;, [&#39;ngFileUpload&#39;]); app.controller(&#39;MyController&#39;, [&#39;$scope&#39;, &#39;$http&#39;, &#39;$window&#39;, function ($scope, $http, $window) { $scope.SelectFile = function (file) { $scope.SelectedFile = file; }; $scope.ProcessExcel = function (data) { //Read the Excel File data. var workbook = XLSX.read(data, { type: &#39;binary&#39; }); //Fetch the name of First Sheet. var firstSheet = workbook.SheetNames[0]; //Read all rows from First Sheet into an JSON array. var excelRows = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[firstSheet]); if (excelRows.length &gt; 0) { // Save excel data to database. $http({ method: &#34;POST&#34;, url: &#34;Home/Save/&#34;, params: { customers: excelRows } }).then(function (response) { if (response.data &gt; 0) { $scope.Message = response.data + &#34; record inserted.&#34;; } else { $scope.Message = &#34;Record Insert failed.&#34;; } }, function (response) { $scope.Message = response.responceText; }) } }; $scope.Import = function () { var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xls|.xlsx)$/; if (regex.test($scope.SelectedFile.name.toLowerCase())) { if (typeof (FileReader) != &#34;undefined&#34;) { var reader = new FileReader(); //For Browsers other than IE. if (reader.readAsBinaryString) { reader.onload = function (e) { $scope.ProcessExcel(e.target.result); }; reader.readAsBinaryString($scope.SelectedFile); } else { //For IE Browser. reader.onload = function (e) { var data = &#34;&#34;; var bytes = new Uint8Array(e.target.result); for (var i = 0; i &lt; bytes.byteLength; i++) { data += String.fromCharCode(bytes[i]); } $scope.ProcessExcel(data); }; reader.readAsArrayBuffer($scope.SelectedFile); } } else { $window.alert(&#34;This browser does not support HTML5.&#34;); } } else { $window.alert(&#34;Please upload a valid Excel file.&#34;); } } } ]); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div ng-app=&#34;MyApp&#34; ng-controller=&#34;MyController&#34;&gt; &lt;input type=&#34;file&#34; name=&#34;file&#34; ngf-select=&#34;SelectFile($file)&#34; /&gt; &lt;input type=&#34;button&#34; value=&#34;Import&#34; class=&#34;btn btn-success&#34; ng-click=&#34;Import()&#34; /&gt; &lt;br /&gt; &lt;span style=&#34;color: green&#34;&gt;{{Message}} &lt;/span&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><strong><span style="text-decoration: underline;">Screenshots</span></strong></p> <p><span style="text-decoration: underline;">The Excel File</span></p> <p><strong><img src="https://i.imgur.com/70HXFnQ.jpg" alt="" width="387" height="220" /></strong></p> <p><span style="text-decoration: underline;">The Form</span></p> <p><strong><img src="https://i.imgur.com/yQcYSK6.gif" alt="" width="262" height="169" /></strong></p> <p><span style="text-decoration: underline;">Record After Insert in Database</span></p> <p><strong><img src="https://i.imgur.com/WdCo8cI.jpg" alt="" width="300" height="148" /></strong></p>https://www.aspforums.net:443/Threads/102915/Import-Excel-data-to-Database-using-AngularJS-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/102915/Import-Excel-data-to-Database-using-AngularJS-in-ASPNet-MVC/Tue, 03 Sep 2019 03:58:57 GMT