Currently, I have an object stored in my controller that I need to export as either a .xls or .csv file. I have tried various methods, including the following:
HTML
<script src="https://rawgithub.com/eligrey/FileSaver.js/master/FileSaver.js" type="text/javascript" />
<div ng-controller="myCtrl">
<button ng-click="exportData()">Export</button>
<br />
<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>
</div>
Javascript
function myCtrl($scope) {
$scope.exportData = function () {
var blob = new Blob([document.getElementById('exportable').innerHTML], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
});
saveAs(blob, "Report.xls");
};
$scope.items = [{
name: "John Smith",
email: "[email protected]",
dob: "1985-10-10"
}, {
name: "Jane Smith",
email: "[email protected]",
dob: "1988-12-22"
}, {
name: "Jan Smith",
email: "[email protected]",
dob: "2010-01-02"
}, {
name: "Jake Smith",
email: "[email protected]",
dob: "2009-03-21"
}, {
name: "Josh Smith",
email: "[email protected]",
dob: "2011-12-12"
}, {
name: "Jessie Smith",
email: "[email protected]",
dob: "2004-10-12"
}]
}
However, this method does not work with paginated tables. Is there a direct way to export objects (like $scope.item in this example) to a file (xls, csv)?