biz*_*zr3 6 javascript csv xls export angularjs
实际上,我在我的控制器中有一个对象,我只想将该对象导出为.xls或.csv file.i使用了很多这样的方法:
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>
Run Code Online (Sandbox Code Playgroud)
使用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: "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"
}]
}
Run Code Online (Sandbox Code Playgroud)
但这不适用于分页table.is有没有办法直接导出对象(在这个例子中$ scope.item)到文件(xls,csv)?
age*_*hun 14
是的,您可以使用带有XLSX.js库的Alasql JavaScript库来保存数据.这是一个例子:
首先:在页面中包含两个JavaScript库:
第二步:用你的代码替换exportData()函数:
$scope.exportData = function () {
alasql('SELECT * INTO XLSX("john.xlsx",{headers:true}) FROM ?',[$scope.items]);
};
Run Code Online (Sandbox Code Playgroud)
第三:对于CSV文件 - 只需使用CSV()函数:
alasql('SELECT * INTO CSV("john.csv",{headers:true, separator:";"}) FROM ?',[$scope.items]);
Run Code Online (Sandbox Code Playgroud)
您可以在jsFiddle中使用此示例.
| 归档时间: |
|
| 查看次数: |
21176 次 |
| 最近记录: |