Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export to xls using angularjs

I am working on angular js app and I stuck in a situation in which I have to export data to Xls using angular js. I have searched a lot on the internet for export functionality or any library for angular js so I can do that or at least I can get the idea how to export. I don't have any code or work to show here.

I need suggestions. Please help me on this.

Thanks in advance.

Update:

I have a data which is an array of objects and I am iterating that on UI in a table. My backend is node.js and frontend are angular js.

My problem is if we have the data from the server and I am using on UI, how can I use the same data to export to Xls using angular js. I don't want to give a call again on the backend to extract the data and export that.

In the existing table, the user can select the checkbox (Any number of rows or all rows) to extract the data to Xls.

In node.js I have used node module whose name is: Excel and it is available on nodemodules site.

My data is like that:

"data": [     {         "Name": "ANC101",         "Date": "10/02/2014",         "Terms": ["samsung", "nokia": "apple"]     },{         "Name": "ABC102",         "Date": "10/02/2014",         "Terms": ["motrolla", "nokia": "iPhone"]     } ] 

I want the solution using angularjs or any angularjs library.

like image 480
w3uiguru Avatar asked Feb 10 '14 15:02

w3uiguru


People also ask

How to export data to Excel in AngularJS?

When the Export Button is clicked, the jQuery table2excel plugin is applied to the HTML Table. The jQuery table2excel plugin accepts filename parameter which sets the name of the Excel file. Note: You will get a Warning message from Microsoft Excel application when you try to open the generated Excel file.


2 Answers

A cheap way to do this is to use Angular to generate a <table> and use FileSaver.js to output the table as an .xls file for the user to download. Excel will be able to open the HTML table as a spreadsheet.

<div id="exportable">     <table width="100%">         <thead>             <tr>                 <th>Name</th>                 <th>Email</th>                 <th>DoB</th>             </tr>         </thead>         <tbody>             <tr ng-repeat="item in items">                 <td>{{item.name}}</td>                 <td>{{item.email}}</td>                 <td>{{item.dob | date:'MM/dd/yy'}}</td>             </tr>         </tbody>     </table> </div> 

Export call:

var blob = new Blob([document.getElementById('exportable').innerHTML], {         type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"     });     saveAs(blob, "Report.xls"); }; 

Demo: http://jsfiddle.net/TheSharpieOne/XNVj3/1/

Updated demo with checkbox functionality and question's data. Demo: http://jsfiddle.net/TheSharpieOne/XNVj3/3/

like image 98
TheSharpieOne Avatar answered Sep 25 '22 13:09

TheSharpieOne


You can try Alasql JavaScript library which can work together with XLSX.js library for easy export of Angular.js data. This is an example of controller with exportData() function:

function myCtrl($scope) {   $scope.exportData = function () {     alasql('SELECT * INTO XLSX("john.xlsx",{headers:true}) FROM ?',[$scope.items]);   };    $scope.items = [{     name: "John Smith",     email: "[email protected]",     dob: "1985-10-10"   }, {     name: "Jane Smith",     email: "[email protected]",     dob: "1988-12-22"   }]; } 

See full HTML and JavaScript code for this example in jsFiddle.

UPDATED Another example with coloring cells.

Also you need to include two libraries:

  • alasql.min.js
  • xlsx.core.min.js
like image 25
agershun Avatar answered Sep 23 '22 13:09

agershun