Export JSON TO CSV Using AlaSQL.js

jsontocsv

AlaSQL is a client side in memory SQL database. It is very light weight and fast.It is fully functional and work in all modern browsers.

Below program show to we can export json data to CSV format, and download it.

STEPS:

1. ADD alasql libray  (you can dowload js file or use cdn ) 
     https://cdn.jsdelivr.net/alasql/0.3/alasql.min.js
2. WRITE QUERY FROM YOUR JSON.
      EX: (SELECT * INTO FILENAME.XLSX FROM $scope.data )
3. RUN THE FILE
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <!--angular js-->
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

        <!--alasql library-->
        <script src="https://cdn.jsdelivr.net/alasql/0.3/alasql.min.js"></script>

        <script>
            angular.module('app', []).controller('downloader', function($scope) {
                // json data to be exported
                $scope.data = [
                    {
                        id: 1,
                        name: "avis",

                    },
                    {
                        id: 2,
                        name: "tasis"
                    },
                    {
                        id: 3,
                        name: "ticus"
                    }
                ]

                $scope.downloadReports = function() {   // we can download our json data in many formats. ex: csv, excel
                   // var filename = "someFileName.xlsx"
                    var filename = "someFileName.csv"
                    //alasql('SELECT id as ID,name as Name INTO XLSX("' + filename + '",{headers:true}) FROM ?', [$scope.OrganizationUsersList]);
                    alasql('SELECT id as ID,name as Name INTO CSV("' + filename + '",{headers:true}) FROM ?', [$scope.data]);

                }

            });
        </script>
    </head>
    <body ng-app="app" ng-controller="downloader">
<table border="1px">
<th>Id</th>
<th>Name</th>
<tr ng-repeat="user in data">
<td>{{user.id}}</td>
<td>{{user.name}}</td>
</tr>
</table>
<h3> Hello , click the below button to export your data to CSV</h1>
<button class="btn  btn-xs btn-white" ng-click="downloadReports()">Download Report</button>
    </body>
</html>
References:
http://alasql.org/
jsfiddle
Advertisements

About sivateja

I am a professional graduate..I am passionate about Latest Technology.
This entry was posted in angularjs and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s