js实现数据的excel下载

js实现数据的excel下载
由于目前html+ajax的后台数据传递的使用很高,那么如果对后台传入的json数据进行excel格式的下载呢?
本人也搜索了网络的各种办法,自认为下面这个方法经过改进后应该是最简单的:

1、方法提取:可以直接引用

//把table导出Excel表,excel是拼接后的table字符串,fileName是需要起的文件名称
	tableToExcel:function(excel, fileName) {  
		var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";  
		excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';  
		excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';  
		excelFile += '; charset=UTF-8">';  
		excelFile += "<head>";  
		excelFile += "<!--[if gte mso 9]>";  
		excelFile += "<xml>";  
		excelFile += "<x:ExcelWorkbook>";  
		excelFile += "<x:ExcelWorksheets>";  
		excelFile += "<x:ExcelWorksheet>";  
		excelFile += "<x:Name>";  
		excelFile += "{worksheet}";  
		excelFile += "</x:Name>";  
		excelFile += "<x:WorksheetOptions>";  
		excelFile += "<x:DisplayGridlines/>";  
		excelFile += "</x:WorksheetOptions>";  
		excelFile += "</x:ExcelWorksheet>";  
		excelFile += "</x:ExcelWorksheets>";  
		excelFile += "</x:ExcelWorkbook>";  
		excelFile += "</xml>";  
		excelFile += "<![endif]-->";  
		excelFile += "</head>";  
		excelFile += "<body>";  
		excelFile += excel;  
		excelFile += "</body>";  
		excelFile += "</html>";  
		var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);  
		var link = document.createElement("a");      
		link.href = uri;  
		link.style = "visibility:hidden";  
		link.download = fileName + ".xls";  
		document.body.appendChild(link);  
		link.click();  
		document.body.removeChild(link);  
	}, 

2、tabel拼接:
例如:
                var excel = '<table>';      
		//设置表头  
		var row = "<tr>"+
					"<td>申请时间</td>"+
					"<td>申请人</td>"+
					"<td>申请人手机号</td>"+
					"<td>申请提现金额</td>"+
					"<td>当前总金额</td>"+
					"<td>提现银行</td>"+
					"<td>开户行名</td>"+
					"<td>账户名称</td>"+
					"<td>银行卡号</td>"+
					"<td>状态</td>"+
					"<td>操作</td>"+
				  "</tr>";
		//换行  
		excel += row;
		excel += html;
		excel += "</table>";
		//利用时间起文件名称
		var date=new Date();
		var fileName="提现"+ date.getFullYear()+(date.getMonth()+1)+date.getDate()+date.getHours()+
					date.getMinutes()+date.getSeconds();
		//应用1的方法
		withdrawalsManagement.tableToExcel(excel,fileName)

其中的html变量就是table的body部分。不同人有不同的拼凑方法,这里不做阐述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值