In this code snippet you can find complete solution to export JSON as XLS format using AngularJS.
Step 1 Include alasql.js service in your application from this url http://alasql.org/console/alasql.js
Step 2 Create HTML Page with app and controller according your application. Add button for export.
- <button type="submit" ng-click="exportData()" data-ng-disabled="!searchCaseResults.length" class="btn btn-primary pull-right">Export</button>
Step 3 Write code in your angular controller.
-
-
- var mystyle = {
- headers:true,
- columns: [
- { columnid: 'CaseId', title: 'Case Id'},
- { columnid: 'ClientName', title: 'Client Name'},
- { columnid: 'PatientName', title: 'Patient Name'},
- { columnid: 'BillCharge', title: 'Billed Amount'},
- { columnid: 'DocCreatedDate', title: 'Doc Created On' },
- { columnid: 'CaseStatusValue', title: 'Status'},
- { columnid: 'CaseCategory', title: 'Category'},
- { columnid: 'ClientType', title: 'ClientType'},
- ],
- };
-
-
-
- $scope.exportData = function () {
-
-
- var date = new Date();
- $scope.CurrentDateTime = $filter('date')(new Date().getTime(), 'MM/dd/yyyy HH:mm:ss');
-
- for(var i=0; i<$scope.searchCaseResult.length;i++)
- {
- $scope.searchCaseResult[i].DocCreatedDate;
- var dateString = $scope.searchCaseResult[i].DocCreatedDate.substr(6);
- var currentTime = new Date(parseInt(dateString));
- var month = currentTime.getMonth() + 1;
- var day = currentTime.getDate();
- var year = currentTime.getFullYear();
- var date = month + "/" + day + "/" + year;
- $scope.searchCaseResult[i].DocCreatedDate = date;
- }
-
- alasql('SELECT * INTO XLS("SearchResult' + $scope.CurrentDateTime + '.xls",?) FROM ?', [mystyle, $scope.searchCaseResult]);
- };
-
According your requirement you can change column name in XLS template.