超級好用的網頁table 匯出excel

來源:互聯網
上載者:User

標籤:xmlns   ade   script   point   current   min   round   lock   sheet   

最近發現一個封裝的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>

此次轉載的是:<文大官人>(虛擬稱呼)

注意:轉載請註明。 

 

超級好用的網頁table 匯出excel

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.