AngularJs導(dǎo)出數(shù)據(jù)到Excel的示例代碼
公司一個(gè)新的需求導(dǎo)出Exce表格,研究了一下,最后終于實(shí)現(xiàn),分享給大家。
1 使用FileSaver
第一次采用FileSaver.js 由于剛開(kāi)始導(dǎo)致導(dǎo)出一片空白,還只能抓取網(wǎng)頁(yè)里面的表格地址:https://github.com/eligrey/FileSaver.js
HTML
<div id="exportable">
<table width="100%">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>小明</td>
<td>dsds@163.com</td>
</tr>
</tbody>
</table>
</div>
js部分
var blob = new Blob([document.getElementById('exportable').innerHTML], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
});
saveAs(blob, "日記賬.xls");
};
2 使用 alasql
使用alasql的好處就是可以在數(shù)據(jù)層面去組織結(jié)構(gòu)
官網(wǎng)地址:http://alasql.org/
js部分
\\ angular 需要導(dǎo)入xlsx.core.min.js alasql.min.js
\\ 文件結(jié)構(gòu)
var arr = [
{
'收入':1,
'支出':2,
'結(jié)存':3
},
{
'收入':4,
'支出':5,
'結(jié)存':6
}
]
\\ 生成 excel 文件
alasql('SELECT * INTO XLSX("日記賬.xlsx",{headers:true}) FROM ?',[arr]);
我優(yōu)化的版本
// 導(dǎo)出excel
$scope.exportToExcel=function(){
var data = angular.copy($scope.pageData.list)
var arr = [];
var type = null;
var amountIN = 0;
var amountOUT = 0;
angular.forEach(data,function (item) {
// 兌付情況
if(item.isHappened){
type = '未兌付'
}else{
type = '已兌付'
}
// 收入
if(item.itemModel=='INCOME'){
amountIN = item.amount
}
// 支出
if(item.itemModel=='OUTCOME'){
amountOUT = item.amount
}
arr.push({
'兌付情況':type,
'合同':item.keyId,
'收付日期':$filter('date')(item.updateTime,'yyyy-MM-dd'),
'科目':item.itemType.value,
'收入':$filter('number')(amountIN,2),
'支出':$filter('number')(amountOUT,2),
'結(jié)存':$filter('number')(item.balance,2)
})
})
if(arr.length < 1){
ToasterTool.error('暫無(wú)數(shù)據(jù),導(dǎo)出失??!');
}else{
// alasql('SELECT * INTO XLSX("日記賬.xlsx",{headers:true}) FROM ?',[arr]);
alasql.promise('SELECT * INTO XLSX("日記賬-'+ DateTool.format(new Date(),'yyyy-MM-dd HH:mm:ss') + "-"+ $scope.loginUser.userName +'.xlsx",{headers:true}) FROM ?',[arr])
.then(function (data) {
if(data == 1){
$timeout(function(){
ToasterTool.success('數(shù)據(jù)導(dǎo)出成功!')
})
}
})
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到Excel的方法詳解
- js實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出為EXCEL(支持大量數(shù)據(jù)導(dǎo)出)
- Vue導(dǎo)出json數(shù)據(jù)到Excel電子表格的示例
- JSP實(shí)現(xiàn)從數(shù)據(jù)庫(kù)導(dǎo)出數(shù)據(jù)到Excel下載的方法
- js導(dǎo)出table數(shù)據(jù)到excel即導(dǎo)出為EXCEL文檔的方法
- 通過(guò)Javascript將數(shù)據(jù)導(dǎo)出到外部Excel文檔的函數(shù)代碼
- javascript 導(dǎo)出數(shù)據(jù)到Excel(處理table中的元素)
- javascript?實(shí)現(xiàn)純前端將數(shù)據(jù)導(dǎo)出excel兩種方式
相關(guān)文章
div實(shí)現(xiàn)自適應(yīng)高度的textarea實(shí)現(xiàn)angular雙向綁定
本文主要介紹了div實(shí)現(xiàn)自適應(yīng)高度的textarea,實(shí)現(xiàn)angular雙向綁定的方法。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01
AngularJs入門(mén)教程之環(huán)境搭建+創(chuàng)建應(yīng)用示例
這篇文章主要介紹了AngularJs入門(mén)教程之環(huán)境搭建+創(chuàng)建應(yīng)用的方法,較為詳細(xì)的分析了AngularJS的功能、下載及應(yīng)用創(chuàng)建方法,需要的朋友可以參考下2016-11-11
AngularJS入門(mén)教程之ng-class 指令用法
本文主要介紹AngularJS ng-class 指令,這里幫大家整理了ng-class資料和示例代碼,學(xué)習(xí)AngularJS指令的同學(xué)可以參考下2016-08-08
Angular將填入表單的數(shù)據(jù)渲染到表格的方法
這篇文章主要介紹了Angular將填入表單的數(shù)據(jù)渲染到表格的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09
詳解Angular中通過(guò)$location獲取地址欄的參數(shù)
這篇文章主要介紹了詳解 Angular中通過(guò)$location獲取地址欄的參數(shù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
對(duì)Angular.js Controller如何進(jìn)行單元測(cè)試
這篇文章主要給大家介紹了如何對(duì)Angular Controller進(jìn)行單頁(yè)測(cè)試。如果你不太了解angular也沒(méi)關(guān)系,本文也會(huì)提及關(guān)于Angular的一些知識(shí)。文中通過(guò)示例代碼介紹的很詳細(xì),詳細(xì)對(duì)大家的理解和學(xué)習(xí)很有幫助,下面來(lái)一起看看吧。2016-10-10
angular和BootStrap3實(shí)現(xiàn)購(gòu)物車功能
這篇文章主要為大家詳細(xì)介紹了angular和BootStrap3實(shí)現(xiàn)購(gòu)物車功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Angularjs 滾動(dòng)加載更多數(shù)據(jù)
AngularJS 通過(guò)新的屬性和表達(dá)式擴(kuò)展了 HTML。本文主要給大家介紹Angularjs 滾動(dòng)加載更多數(shù)據(jù)的相關(guān)知識(shí),需要的朋友參考下吧2016-03-03

