超级好用的网页table 导出excel

最近发现一个封装的js插件导出网页中的table 为excel 超级好用,基本不用写代码

 

(function ($) {
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}

$.fn.ExportExcel = function (tab_id, options) {
var defaults = {
height: '24px',
'line-height': '24px',
margin: '0 5px',
padding: '0 11px',
color: '#000',
background: '#02bafa',
border: '1px #26bbdb solid',
'border-radius': '3px',
/*color: #fff;*/
display: 'inline-block',
'text-decoration': 'none',
'font-size': '12px',
outline: 'none',
cursor: 'pointer'
}
var options = $.extend(defaults, options);
return this.each(function () {
var currentObject = $(this); //获取当前对象
currentObject.css(defaults);
currentObject.onmouseover = function () {
$(this).css('cursor', 'hand');
};

currentObject.click(function () {
//From:jsfiddle.net/h42y4ke2/16/
var tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">';
tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';

tab_text = tab_text + '<x:Name>Test Sheet</x:Name>';

tab_text = tab_text + '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>';
tab_text = tab_text + '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';

tab_text = tab_text + "<table border='1px'>";
tab_text = tab_text + $('#' + tab_id).html();
tab_text = tab_text + '</table></body></html>';

var data_type = 'data:application/vnd.ms-excel';

var timeStr = new Date().Format('yyyyMMddhhmmss');
$(this).attr('href', data_type + ', ' + encodeURIComponent(tab_text));
$(this).attr('download', '数据报表' + timeStr + '.xls');
});
})
}
})(jQuery);

把此代码封装起来。

命名为ExcelTable.js

 

 

注意:需用a标签 <a href="javascript:;" id="downloadExcel">导出Excel</a>

 

例如:

<table class="table table-bordered" id="table_a">
<thead>
<tr>
<th rowspan="3" style="width:30px;">序号</th>
<th rowspan="3">所属银行</th>
<th rowspan="3">时间</th>
<th rowspan="3">调剂类型</th>
<th colspan="8">硬币</th>
<th colspan="12">零钞</th>
</tr>
<tr>
<th colspan="2">1元</th>
<th colspan="2">5角</th>
<th colspan="2">1角</th>
<th colspan="2">小计</th>
<th colspan="2">10元</th>
<th colspan="2">5元</th>
<th colspan="2">1元</th>
<th colspan="2">5角</th>
<th colspan="2">1角</th>
<th colspan="2">小计</th>
</tr>
<tr>
<th>金额</th>
<th>枚数</th>
<th>金额</th>
<th>枚数</th>
<th>金额</th>
<th>枚数</th>
<th>金额</th>
<th>枚数</th>
<th>金额</th>
<th>张</th>
<th>金额</th>
<th>张</th>
<th>金额</th>
<th>张</th>
<th>金额</th>
<th>张</th>
<th>金额</th>
<th>张</th>
<th>金额</th>
<th>张</th>

</tr>
</thead>
<tbody class="table_tbody">

</tbody>
</table>

点击a标签导出时:

<script type="text/javascript">

$(function () {
$('#downloadExcel').ExportExcel('table_a');
})

</script>

此次转载的是:<文大官人>(虚拟称呼)

注意:转载请注明。 

 

转载于:https://www.cnblogs.com/QXHao/p/6030321.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值