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.
//Start*To Export SearchTable data in excel
// create XLS template with your field.
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'},
],
};
// function to use on ng-click.
//$scope.searchCaseResult is your json data which cominmg from database or Controller.
$scope.exportData = function () {
//get current system date.
var date = new Date();
$scope.CurrentDateTime = $filter('date')(new Date().getTime(), 'MM/dd/yyyy HH:mm:ss');
//To convert Date[mm/dd/yyyy] from jsonDate "/Date(1355287447277)/"
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;
}
//Create XLS format using alasql.js file.
alasql('SELECT * INTO XLS("SearchResult' + $scope.CurrentDateTime + '.xls",?) FROM ?', [mystyle, $scope.searchCaseResult]);
};
//End*To Export SearchTable data in excelAccording your requirement you can change column name in XLS template.