2

I am using this script for exporting data from HTML table to Excel.

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

I found this here but when i export this data it includes all columns in HTML table as expected to do. but my last row contains some icons that i don't want to export to excel.

<div class="row" style="margin-left:20px;">
    <div class="grid_4">
        <div class="da-panel collapsible">
        <input type="button" class="btn btn-success" onclick="tableToExcel('testTable', 'W3C Example Table')" value="Export to Excel" style="float:right">
            <div class="da-panel-content">

            <div class="da-panel-title" style="border-top:1px solid #ccc;border-bottom:1px solid #ccc">
            <h3 style="padding-left:10px;font-weight:bold;">Staff Training Information</h3></div>

         <table  class="da-table da-ex-datatable-numberpaging" id="testTable" width="100%">
          <thead width="100%">
            <tr>
            <th width="10%">Staff ID</th>
              <th width="10%">Name</th>
              <th width="10%">Location</th>
              <th width="10%">POCT Test</th>
              <th width="10%">Initial Training Date</th>
              <th width="10%">Annual Competency Date</th>
              <th width="10%">Competency Type</th>
              <th width="1%">Next Competency Date</th>
              <th width="39%">Action</th>
            </tr>
          </thead>
          <tbody width="100%">
          <?php 
           include_once('database.php');
           $pdo = Database::connect();
           $sql = 'SELECT * FROM competency';

           foreach ($pdo->query($sql) as $row) {
                    $id = $row['staff_id'];
                    echo '<tr>';
                        echo '<td width="10%">'. $row['staff_id'] . '</td>';
                        $sql1 = "SELECT *FROM staff WHERE StaffID='$id'";
                        foreach($pdo->query($sql1) as $res)
                        {
                            echo '<td width="10%">'. $res['StaffName'] . '</td>';
                        }
                    echo '<td width="10%">'. $row['location'] . '</td>';
                    ?>
                        <td width="10%">
                        <?php
                             $s = $row['poct_test'];
                             $val = explode(" ",$s);
                             for ($i=0; $i<sizeof($val); $i++)
                             {
                                 $v = $val[$i];
                                 echo $v."<br/>";
                             }
                        ?>
                        </td>
                <?php
                    echo '<td width="10%">'. $row['date_of_initial_training'] . '</td>';

                        echo '<td width="10%">'. $row['annual_competency'] . '</td>';
                            echo '<td width="10%">'. $row['type_of_competency'] . '</td>';
                                echo '<td width="1%">'. $row['next_competency'] . '</td>';

                                        echo '<td width="39%">';
                        echo '<a href="viewtrainingdetails.php?id='.$row['id'].'"><img src="images/ic_zoom.png" height="16" width="16" /></a>';
                        echo ' ';
                        echo '<a href="updatetraining.php?id='.$row['id'].'"><img src="images/icn_edit.png"/></a>';

                        echo ' ';
                ?>
                <a href="javascript:DeleteRecord('<?php echo $row['id'];?>')"><img src="images/icn_logout.png"/></a>
                <?php
                        echo '</td>';
                    echo '</tr>';
           }
           Database::disconnect();
          ?>
          </tbody>
    </table>
        </div>
        </div>
    </div>
</div>

As shown in code that last 3 echo contains update/delete icons. I just want to exclude Action column when exporting the table content in excel. Any help would be highly appreciated.

4

4 回答 4

4

您可以使用 jQuery 中的选择器,在内存中克隆您的表,然后使用适当的选择器删除您不想要的元素。

var $table =  $('#testTable').clone();

$table = filterNthColumn($table, 9); //remove Action column

function filterNthColumn($table, n){
    return $table.find('td:nth-child('+n+'), th:nth-child('+n+')').remove();
}
于 2013-11-18T08:27:53.750 回答
3

我认为您可以先克隆表,删除操作列,然后“tableToExcel”克隆表。

为了使列删除更容易,将类“action_th”添加到操作 th,并将类“action_td”添加到操作 td。

然后是这样的,

 var exTable = $('#testTable').clone();
 //remove the action th/td
 exTable.find('.action_th, .action_td').remove();

 //then tableToExcel(exTable, ..
于 2013-11-18T07:42:56.593 回答
2

make a hidden div under the table

<div class="exportData"> </div>

Then on click of the export button call export.php through ajax and put the result into exportData div. Then you can call your print script on the new data brought.

$.post( "export.php", function( data ) {
  $( ".result" ).html( data );
});

Copy past your for loop on export.php and delete the two cols.

于 2013-11-18T07:26:57.560 回答
1

这对我有用 -

$('#divTableContainer').clone().find('table tr th:nth-child(7),table tr td:nth-child(7)).remove().end().prop('outerHTML')
于 2016-10-30T03:19:31.700 回答