我想导出除页脚之外的整个表格。反正是否还要导出表格的<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>
答案 0 :(得分:2)
你可能想尝试这样的事情
var something = '<table><tfoot></tfoot></table>'.replace(/<\/?tfoot>/g, '');
$(something).appendTo('something');
以某种方式将其合并到您的代码中。这将从您的excel body html内容中删除所有内容。
答案 1 :(得分:0)
您可以在调用函数tfoot
table
从JQuery
删除tableToExcel
元素
$('table.footable').find('tfoot').remove().end();