Exclude Specific Columns when exporting data from HTML table to excel

11,835

Solution 1

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.

Solution 2

You can use selectors from jQuery, clone of your table in memory then remove elements you don't want with appropriate selector.

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();
}

Solution 3

I think you can just clone the table firstly, remove the action column, and "tableToExcel" the cloned table.

To make the column removing easier, add class "action_th" to action th, and class "action_td" to action td.

Then it's like this,

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

 //then tableToExcel(exTable, ..

Solution 4

This works for me -

$('#divTableContainer').clone().find('table tr th:nth-child(7),table tr td:nth-child(7)).remove().end().prop('outerHTML')
Share:
11,835
james
Author by

james

Updated on June 05, 2022

Comments

  • james
    james almost 2 years

    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.

  • TecHunter
    TecHunter over 10 years
    that's like creating a clone but you also manipulate the DOM which is somehow heavy operation
  • Fareed Ud Din
    Fareed Ud Din over 10 years
    yes, I was thinking in most of the cases easy implementation is preferred.