Your fiddle contains everything needed except one important thing. You're not generating content that excel can understood.
Your problem is here:
var blob = new Blob([document.getElementById('exportable').innerHTML], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
});
More specifically, here:
[document.getElementById('exportable').innerHTML]
This returns HTML, which is not Excel file format. There is no auto-magic that converts HTML into Excel.
This is usually done on server side, not AngularJS. But if you're forced, you'll need another library to handle conversion between your data and Excel. One of popular library is ExcelJS.
Simpler solution - generate CSV
I would propose to skip Excel and generate CSV, which is the simplest possible format to generate - understood by Excel. You only have to modify your export function:
$scope.exportData = function () {
var blob = new Blob([convertToCsv($scope.items)], {
type: "text/csv"
});
saveAs(blob, "Report.csv");
function convertToCsv(items) {
var headers = "Name; Email; DoB \n";
return headers + items.map(function(item) {
return item.name + ";" + item.email + ";" + item.dob;
}).join("\n");
}
};
Function convertToCsv
organize your items into format:
Name; Email; DoB
John Smith;j.smith@example.com;1985-10-10
Jane Smith;jane.smith@example.com;1988-12-22
Jan Smith;jan.smith@example.com;2010-01-02
Jake Smith;jake.smith@exmaple.com;2009-03-21
Josh Smith;josh@example.com;2011-12-12
Jessie Smith;jess@example.com;2004-10-12
Your fiddle updated: DEMO
Downloaded file Reports.csv can be opened and edited in Excel.
Notes
- You won't be able to use function specific to excel as setting column width, colors etc.
- My Solution with CSV is not quite what you're asked, but I believe it is still good enough
- Related question: how to generate Excel through Javascript which points out other solutions
function myCtrl($scope) {
$scope.exportData = function() {
var blob = new Blob([convertToCsv($scope.items)], {
type: "text/csv"
});
saveAs(blob, "Report.csv");
function convertToCsv(items) {
var headers = "Name; Email; DoB \n";
return headers + items.map(function(item) {
return item.name + ";" + item.email + ";" + item.dob;
}).join("\n");
}
};
$scope.items = [{
name: "John Smith",
email: "j.smith@example.com",
dob: "1985-10-10"
}, {
name: "Jane Smith",
email: "jane.smith@example.com",
dob: "1988-12-22"
}, {
name: "Jan Smith",
email: "jan.smith@example.com",
dob: "2010-01-02"
}, {
name: "Jake Smith",
email: "jake.smith@exmaple.com",
dob: "2009-03-21"
}, {
name: "Josh Smith",
email: "josh@example.com",
dob: "2011-12-12"
}, {
name: "Jessie Smith",
email: "jess@example.com",
dob: "2004-10-12"
}]
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="https://rawgithub.com/eligrey/FileSaver.js/master/FileSaver.js" type="text/javascript"></script>
<body ng-app>
<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>
</body>