Export HTML to Excel CSV Txt in PHP using JQuery
Export HTML Table Data to Excel, CSV and Text with jQuery, PHP
This article shows you, how to export a static or a dynamic table to different file formats. Here we are using 3 types of files to export such as CSV, XLS and Text. In this example, we are using simple HTML that is exported to specific format when user select it from the drop-down. You can use a dynamic (using MySQL data) HTML instead of a simple static table. The below code examples show you how to implement the logic. In this file, first you will find a drop-down, an HTML table and a jQuery code snippet. And on the top you need to link the specified links.
Download the code and run the index.php, then you will see the result as shown in a given image below.
Note: scan the code before use.
Index.php
<!DOCTYPE html> <html lang="en"> <head>
<title>coding-zon tutorials demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js">
</script> <script type="text/javascript" src="tableExport/jquery.base64.js"></script> <script src="tableExport/tableExport.js"></script> <script src="js/export.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> </head> <div class="btn-group pull-right"> <button type="button" class="btn btn-warning btn-lg dropdown-toggle" data-toggle="dropdown">Export <span class="caret"></span></button> <ul class="dropdown-menu" role="menu"> <li><a class="dataExport" data-type="csv">CSV</a></li> <li><a class="dataExport" data-type="excel">TXT</a></li> <li><a class="dataExport" data-type="txt">XLS</a></li> </ul> </div> <h2>Export HTML Table Data to Excel, CSV and Text with jQuery, PHP and MySQL</h2> <table id="dataTable" class="table table-striped table-bordered" > <thead> <tr class='warning'> <th>Country</th> <th>Capital</th> <th>Currency</th> </tr> </thead> <tbody> <tr> <td>China</td> <td>Beijing</td> <td>Yuan</td> </tr> <tr> <td>India</td> <td>New Delhi</td> <td>Rupee</td> </tr> <tr> <td>United States</td> <td>New York</td> <td>Dollar</td> </tr> </table> </div> <script> $(document).ready(function() { $(".dataExport").click(function() { var exportType = $(this).data('type'); $('#dataTable').tableExport({ type : exportType, escape : 'false', ignoreColumn: [] }); }); }); </script> </body> </html>
Conclusion:
Please comment below if you like the tutorial.
Comments
Post a Comment