0

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.

3
Contributors
2
Replies
31
Views
4 Years
Discussion Span
Last Post by pixelsoul
-1
<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>

            </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>';
                    echo '</tr>';
           }
           Database::disconnect();
          ?>
          </tbody>
    </table>
        </div>
        </div>
    </div>
</div>

You need to remove line 21 from your code and all lines from line 59 to 67

Votes + Comments
I just want to exclude the action column when exporting to excel. you didn't get my point.
1

I decided to turn this into a jQuery plugin (mostly from out of boredom). While I was doing that I put in the option to exclude rows from the output.

http://jsfiddle.net/pixelsoul/2MBDq/

Pretty easy to use the plugin:

$('#exportButton1').tableToExcel({
    table: '#testTable',
    exclude: '.exclude',
    name: 'testing-export'
});

Typically I would use some server side code like PHP to generate a csv file for down rather than use JavaScript, but if this is for something like an intranet site, then I guess it could be useful.

Votes + Comments
Thanks for the solution. In case i want to remove some column?
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.