如何禁用JavaScript上点击导出到Excel表?

我有一个表格与variables从数据库:

<table id="itemall2" class="itemall" border="1"> <thead> <tr><th></th> <th><?= $lang['quantity']; ?></th> <th><?= $lang['article']; ?></th> <th><?= $lang['desc']; ?></th> <th><?= $lang['weight']; ?> (Kg)</th> <th><?= $lang['price']; ?> (Kč)</th> <th><?= $lang['cat_id']; ?></th> <th><?= $lang['cat_name']; ?></th></tr> </thead> <tbody> <?php foreach ($this->items as $value) : ?> <tr><td><input type="checkbox" name="checked[]" value="<?= $value['id_item']?>" onclick="enableName(this, 'quantity<?= $value['id_item']?>');"/></td> <td><input type="number" class="quantity<?= $value['id_item']?>" name="quantity[]" disabled></td> <td><?= $value['id_item']?></td> <td><?= $value[$en] ?></td> <td><?= $value['weight'] ?></td> <td><?= $value['price'] ?></td> <td><?= $value['code'] ?></td> <td><?= $value['name'] ?></td> </tr> <?php endforeach; ?> <input type="hidden" name="id_warehouse" type="text" value="<?= $this->id_warehouse ?>"> </tbody> </table> 

我有一个button,点击后将表格转换为Excel格式并保存到磁盘:

 <input type="button" onclick="tableToExcel('itemall2', 'W3C Example Table')" value="<?= $lang['export']; ?>"> 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>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</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(table, name) { if (!table.nodeType) table = document.getElementById(table) var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML} window.location.href = uri + base64(format(template, ctx)) } })(); 

问题是,表中的一些单元格使用此函数与rowspans和colspans合并在一起:

 function MergeCommonRows(table) { var firstColumnBrakes = []; // iterate through the columns instead of passing each column as function parameter: for(var i=1; i<=table.find('th').length; i++){ var previous = null, cellToExtend = null, rowspan = 1; table.find("td:nth-child(" + i + ")").each(function(index, e){ var jthis = $(this), content = jthis.text(); // check if current row "break" exist in the array. If not, then extend rowspan: if (previous == content && content !== "" && $.inArray(index, firstColumnBrakes) === -1) { // hide the row instead of remove(), so the DOM index won't "move" inside loop. jthis.addClass('hidden'); cellToExtend.attr("rowspan", (rowspan = rowspan+1)); }else{ // store row breaks only for the first column: if(i === 1) firstColumnBrakes.push(index); rowspan = 1; previous = content; cellToExtend = jthis; } }); } // now remove hidden td's (or leave them hidden if you wish): $('td.hidden').hide(); } 

而且因为我不使用remove()从表中删除重复的单元格,而是用hide()隐藏它们,所以导出的表格被打破。 它正确地输出合并的单元格,但它也打印完全破坏表格的隐藏单元格。 我不能删除()的单元格,因为我用他们的其他东西,所以有办法告诉出口脚本导出表,因为它会没有合并?

下面是jQuery的方式做Crisim II Numenoreano做的同样的事情+感谢他的小提琴,我用来testing下面的代码:

 var clonedTable = $('#test').clone(); clonedTable.find('.hidden').remove(); 

小提琴

你可以像这样修改tableToExcel函数

 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>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</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(table, name) { if (!table.nodeType) table = document.getElementById(table); // clone the table var modifiedTable = $('<table/>').html(table.innerHTML); // remove elements from the clone modifiedTable.find('.hidden').remove(); // get the modified html var ctx = {worksheet: name || 'Worksheet', table: modifiedTable.html()} window.location.href = uri + base64(format(template, ctx)) } })(); 

克隆和删除示例https://jsfiddle.net/yqwk2d0a/

这将不会修改原始表格