AngularJs導(dǎo)出數(shù)據(jù)到Excel的示例代碼
公司一個新的需求導(dǎo)出Exce表格,研究了一下,最后終于實(shí)現(xiàn),分享給大家。
1 使用FileSaver
第一次采用FileSaver.js 由于剛開始導(dǎo)致導(dǎo)出一片空白,還只能抓取網(wǎng)頁里面的表格地址: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('暫無數(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)出成功!') }) } }) } }
以上就是本文的全部內(nèi)容,希望對大家的學(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ù)庫導(dǎo)出數(shù)據(jù)到Excel下載的方法
- js導(dǎo)出table數(shù)據(jù)到excel即導(dǎo)出為EXCEL文檔的方法
- 通過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雙向綁定的方法。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01AngularJs入門教程之環(huán)境搭建+創(chuàng)建應(yīng)用示例
這篇文章主要介紹了AngularJs入門教程之環(huán)境搭建+創(chuàng)建應(yīng)用的方法,較為詳細(xì)的分析了AngularJS的功能、下載及應(yīng)用創(chuàng)建方法,需要的朋友可以參考下2016-11-11Angular將填入表單的數(shù)據(jù)渲染到表格的方法
這篇文章主要介紹了Angular將填入表單的數(shù)據(jù)渲染到表格的方法,非常具有實(shí)用價值,需要的朋友可以參考下2017-09-09詳解Angular中通過$location獲取地址欄的參數(shù)
這篇文章主要介紹了詳解 Angular中通過$location獲取地址欄的參數(shù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08對Angular.js Controller如何進(jìn)行單元測試
這篇文章主要給大家介紹了如何對Angular Controller進(jìn)行單頁測試。如果你不太了解angular也沒關(guān)系,本文也會提及關(guān)于Angular的一些知識。文中通過示例代碼介紹的很詳細(xì),詳細(xì)對大家的理解和學(xué)習(xí)很有幫助,下面來一起看看吧。2016-10-10angular和BootStrap3實(shí)現(xiàn)購物車功能
這篇文章主要為大家詳細(xì)介紹了angular和BootStrap3實(shí)現(xiàn)購物車功能的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01