导出到excel时如何从表中隐藏第3行?

时间:2015-02-09 07:36:07

标签: javascript php export-to-excel

我有一个jquery代码从html表导出到excel但是在导出时留下一个空行,我使用bootstrap进行表设计,如何在导出到excel时删除这个空行。我得到了代码表here

我的php / table代码是 -

<table id="empTable" class="table table-striped table-bordered" cellspacing="0" border="1">
    <thead class="table-th">
        <tr valign="middle">
            <th>SN</th>
            <th>Employee Name</th>                
            <th>DOB</th>
            <th>City</th>            
        </tr>
    </thead>
    <tbody>                           
            <?php 
            $count = 0;            
            foreach ($record as $key => $data) { $count+=1; 
            $employee_name=$data['employee_name'];
            $city=$data['city'];
            $dob=$data['dob'];                         
            echo "<tr>";                
            echo "<td>".$count."</td>";                
            echo "<td>".$employee_name."</td>";
            echo "<td>".$dob."</td>";
            echo "<td>".$city."</td>";           
            echo "</tr>";
            ?>                
        <?php } ?> 
    </tbody>
    <tfoot class="table-tf">  
        <tr><td colspan="4">&nbsp;</td></tr>
    </tfoot>
</table>

我的js代码是 -

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]--></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,table_title ,name) {
    if (!table.nodeType) table = document.getElementById(table)
    var ctx = {worksheet: name || 'Worksheet', table: '<center><b>'+table_title+'</b></center><br/>'+table.innerHTML}
    window.location.href = uri + base64(format(template, ctx))
  }
})()

和我的输出是 -

Employee Detail         
SN  Employee Name   DOB City

1   John    12-18-1983  Texas
2   Sarah   08-26-1985  CA
3   David   06-15-1986  HX
4   Nick    06-14-1982  Miami

1 个答案:

答案 0 :(得分:1)

您可以使用:

$('tr td:first-child:empty').closest('tr').hide()

工作原理:

- 在tr

中选择所有第一个td元素

- 过滤元素只能在它们之间变空。

-get最近的tr元素并隐藏它。