Exporting HTML Table To CSV File Using Angularjs

Today, We want to share with you Exporting HTML Table To CSV File Using Angularjs.In this post we will show you how to export html table data to excel using angularjs, hear for Export to CSV using Angularjs and html we will give you demo and example for implement.In this post, we will learn about Export HTML table as CSV file using Angularjs with an example.

Exporting HTML Table To CSV File Using Angularjs

There are the Following The simple About Exporting HTML Table To CSV File Using Angularjs Full Information With Example and source code.

As I will cover this Post with live Working example to develop Angularjs Plugin To Export Table Data To CSV File, so the some major files and Directory structures for this example is following below.

  • index.html
  • index.js
Also Read :  AngularJS populate Dynamic Dependent dropdown in PHP

Export table data to csv in Angularjs

index.html

This is where I will make a simple HTML form and PHP server side source code for our web application. To make the forms simply all souce code copy and write it into your any text editor Like Notepad++, then save file it as index.html.



  
  <title>Export table data to csv in angularjs</title>



<div>
	<button>Download</button>

	<table>
		<tr>
			<th>No</th>
			<th>Name</th>
			<th>Website</th>
			<th>Age</th>
		</tr>
		<tr>
			<td>11</td>
			<td>JAydeep</td>
			<td>pakainfo</td>
			<td>14</td>
		</tr>
		<tr>
			<td>21</td>
			<td>Ankit</td>
			<td>infinityknow.com</td>
			<td>24</td>
		</tr>
		<tr>
			<td>31</td>
			<td>krunal</td>
			<td>pakainfo.com</td>
			<td>34</td>
		</tr>
		<tr>
			<td>41</td>
			<td>chirag</td>
			<td>www.infinityknow.com</td>
			<td>44</td>
		</tr>
	</table>
</div>	

  

    



Angular Part

index.js

var app = angular.module('csvdownloadModule',[]);
	app.directive('exportToCsv',function(){
  	return {
    	restrict: 'A',
    	link: function (scope, element, attrs) {
    		var el = element[0];
	        element.bind('click', function(e){
	        	var table = e.target.nextElementSibling;
	        	var dataStr = '';
	        	for(var i=0; i<table.rows.length;i++){
	        		var rowData = table.rows[i].cells;
	        		for(var j=0; j<rowData.length;j++){
	        			dataStr = dataStr + rowData[j].innerHTML + ",";
	        		}
	        		dataStr = dataStr.substring(0,dataStr.length - 1);
	        		dataStr = dataStr + "\n";
			    }
	         	dataStr = dataStr.substring(0, dataStr.length - 1);
	         	var a = $('<a />', {
		            style:'display:none',
		            href:'data:application/octet-stream;base64,'+btoa(dataStr),
		            download:'devloperStatistics.csv'
		        }).appendTo('body')
		        a[0].click()
		        a.remove();
	        });
    	}
  	}
	});

	app.controller('csvCtrl',function($scope){
		$scope.message = "";
	});

Export HTML table as CSV file using Angularjs
Angular 6 CRUD Operations Application Tutorials

Read :

Also Read :  Check all checkboxes in HTML table using jQuery

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Exporting HTML Table To CSV File Using Angularjs.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Reply

avatar
  Subscribe  
Notify of