[英]How to export html body table to Excel?
我想導出除頁腳以外的整個表格。 反正還有不導出表的<tfoot>
嗎?
這是我的桌子的骨架結構:
<div id="table">
<table data-filter="#filter" class="footable" style="background-color:white;">
<thead>
<tr>
<th>Employee ID</th>
<th>Branch Code</th>
<th>Client ID</th>
<th>Amount</th>
<th>Report Timestamp</th>
</tr>
</thead>
<tbody>
<?php
if(count($result)==0)
{ echo "<tr><td colspan='5' style='text-align:center; font-weight:bold;'>No data available</td></tr>"; }
else
{
for($i=0; $i<count($result); $i++){?>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<?php }
}
?>
</tbody>
<tfoot>
<tr>
<td colspan="8">
<div class="pagination pagination-centered"></div>
</td>
</tr>
</tfoot>
</table>
</div>
這是我在將表格導出到excel中使用的腳本:
<script>
(function () {
var cache = {};
this.tmpl = function tmpl(str, data) {
// Figure out if we're getting a template, or if we need to
// load the template - and be sure to cache the result.
var fn = !/\W/.test(str) ? cache[str] = cache[str] || tmpl(document.getElementById(str).innerHTML) :
// Generate a reusable function that will serve as a template
// generator (and which will be cached).
new Function("obj",
"var p=[],print=function(){p.push.apply(p,arguments);};" +
// Introduce the data as local variables using with(){}
"with(obj){p.push('" +
// Convert the template into pure JavaScript
str.replace(/[\r\t\n]/g, " ")
.split("{{").join("\t")
.replace(/((^|}})[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)}}/g, "',$1,'")
.split("\t").join("');")
.split("}}").join("p.push('")
.split("\r").join("\\'") + "');}return p.join('');");
// Provide some basic currying to the user
return data ? fn(data) : fn;
};
})();
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<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"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>Sheet 1</x:Name><x:WorksheetOptions><x:Print><x:ValidPrinterInfo/></x:Print></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><body>{{for(var i=0; i<tables.length;i++){ }}<table>{{=tables[i]}}</table>{{ } }}</body></html>',
base64 = function (s) {
return window.btoa(unescape(encodeURIComponent(s)))
},
format = function (s, c) {
return s.replace(/{(\w+)}/g, function (m, p) {
return c[p];
})
}
return function (tableList, name) {
if (!tableList.length > 0 && !tableList[0].nodeType) table = document.getElementById(table)
var tables = [];
for (var i = 0; i < tableList.length; i++) {
tables.push(tableList[i].innerHTML);
}
var ctx = {
worksheet: name || 'Worksheet',
tables: tables
};
window.location.href = uri + base64(tmpl(template, ctx))
}
})();
function download(){
tableToExcel(document.getElementsByTagName("table"), "Sheet 1");
}
var btn = document.getElementById("btn");
btn.addEventListener("click",download);
</script>
編輯
好的,我已經嘗試了上述腳本,這就是結果: 知道為什么會這樣嗎? 我需要網格線。 另外,我需要刪除頁腳。 我已經嘗試過Unix的第二個答案,但失敗了。
這是代碼:
<script type="text/javascript">
$(document).ready(function () {
var something = '<table><tfoot></tfoot></table>'.replace(/<\/?tfoot>/g, '');
$(something).appendTo('#table');
$("#btn").click(function () {
$("#table").btechco_excelexport({
containerid: "table"
, datatype: $datatype.Table
});
});
});
</script>
您可能想嘗試這樣的事情
var something = '<table><tfoot></tfoot></table>'.replace(/<\/?tfoot>/g, '');
$(something).appendTo('something');
並以某種方式將其合並到您的代碼中。 這將擺脫您的excel正文html內容中的所有內容。
您可以在調用函數tableToExcel
之前使用JQuery
從table
刪除tfoot
元素
$('table.footable').find('tfoot').remove().end();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.