I'm working on a project that requires exporting html table into text file. Below is a simplified version of the HTML code :
<table>
<thead>
<tr>
<th>Code</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Date1</th>
<th>Date2</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee in employees">
<td>{{employee.code}}</td>
<td>{{employee.firstName}}</td>
<td>{{employee.lastName}}</td>
<td>{{employee.age}}</td>
<td><input type="text" class="datepicker" ng-model="employee.date1" datepicker /></td>
<td><input type="text" class="datepicker" ng-model="employee.date2" datepicker/></td>
</tr>
</tbody>
</table>
The expected outcome should look like this in a text file (with columns aligned nicely):
Code firstName lastName Age Date1 Date2
001 x y 25 2016/01/01 2016/04/04
...
I tried the "classical" way with jquery but it doesnt interpret values inside my td's. So im looking for an angular directive or something like that to overcome such issue. Thanks..
A pretty straight forward way to do it would be using the FileSaver.js
Assuming your table is like this,
<table id="demo-table">
<thead>
<tr>
<th>Code</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Date1</th>
<th>Date2</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee in employees">
<td>{{employee.code}}</td>
<td>{{employee.firstName}}</td>
<td>{{employee.lastName}}</td>
<td>{{employee.age}}</td>
<td><input type="text" class="datepicker" ng-model="employee.date1" datepicker /></td>
<td><input type="text" class="datepicker" ng-model="employee.date2" datepicker/></td>
</tr>
</tbody>
</table>
After this table have been generated, simply invoke the code below,
var ele = document.getElementById('demo-table');
var blob = new Blob([ele.innerText], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
});
saveAs(blob, "demo-table.csv");
};
For other configurations, check the document of FileSaver.js.
Hope this would solve your problem.