0

I am making one application in which the data is generated from my sql in html table format. I want to generate those data in excel format. Here is my code:

demo.php

<?php   
 include 'include/db_connection.php';   
 $query = "select * from main_master";  
 $result = mysql_query($query);  
?> 

 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | Export HTML table to Excel File using Jquery with PHP</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
      </head>  
      <body>  
           <br />  
           <div class="container" style="width:700px;">  
                <h3 class="text-center">Export HTML table to Excel File using Jquery with PHP</h3><br />  
                <div class="table-responsive" id="employee_table">  
                     <table class="table table-bordered">  
                          <tr>  
                               <th width="10%">Id</th>  
                               <th width="30%">Name</th>  
                               <th width="10%">Gender</th>  
                               <th width="50%">Designation</th>  
                          </tr>  
                          <?php   
                          while($row = mysql_fetch_assoc($result))  
                          {  
                          ?>  
                          <tr>  
                               <td><?php echo $row['B']; ?></td>  
                               <td><?php echo $row['D']; ?></td>  
                               <td><?php echo $row['E']; ?></td>  
                               <td><?php echo $row['F']; ?></td>  
                          </tr>  
                          <?php                           
                          }  
                          ?>  
                     </table>  
                </div>  
                <div align="center">  
                     <button name="create_excel" id="create_excel" class="btn btn-success">Create Excel File</button>  
                </div>  
           </div>  
           <br />  
      </body>  
 </html>  
 <script>  
 $(document).ready(function(){  
      $('#create_excel').click(function(){  
           var excel_data = $('#employee_table').html();  
           var page = "excel.php?data=" + excel_data;  
           window.location = page;  
      });  
 });  
 </script>  

excel.php

<?php   
 header('Content-Type: application/vnd.ms-excel');  
 header('Content-disposition: attachment; filename='.rand().'.xls');  
 echo $_GET["data"];  
 ?>

While executing above code, nothing is done. excel not generated. Please help.

Maistrenko Vitalii
  • 994
  • 1
  • 8
  • 16
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Mar 06 '18 at 09:20
  • I want solution. Not advise. Pl. help if you can otherwise ignore. – Collector Girsomnath Mar 06 '18 at 09:21
  • have a look at [PHPSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) – RiggsFolly Mar 06 '18 at 09:22
  • I am afraid on SO you get it whether you want it or not – RiggsFolly Mar 06 '18 at 09:22
  • I want dear. Pl help me to resolve the issue – Collector Girsomnath Mar 06 '18 at 09:23
  • How about html table to Excel CSV format? Take a look: https://stackoverflow.com/questions/10498632/converting-html-table-to-a-csv-automatically-using-php – suchislife Mar 06 '18 at 09:31

1 Answers1

0

I guess everything is working fine it's just you are trying to send too much data in url

 $('#create_excel').click(function(){  
           var excel_data = $('#employee_table').html();  
           var page = "excel.php?data=" + excel_data;  
           window.location = page;  
 }); 

You can not send a complete html of a table in get request (Query string)

Just redirect user to excel.php page send required parameter in query string if required to create a sql query.

Then instead of echo $_GET["data"];

write complete code of demo.php on excel.php

Now excel.php will look like

<?php   
 header('Content-Type: application/vnd.ms-excel');  
 header('Content-disposition: attachment; filename='.rand().'.xls');  
 include 'include/db_connection.php';   
 $query = "select * from main_master";  
 $result = mysql_query($query);  
?> 

 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | Export HTML table to Excel File using Jquery with PHP</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
      </head>  
      <body>  
           <br />  
           <div class="container" style="width:700px;">  
                <h3 class="text-center">Export HTML table to Excel File using Jquery with PHP</h3><br />  
                <div class="table-responsive" id="employee_table">  
                     <table class="table table-bordered">  
                          <tr>  
                               <th width="10%">Id</th>  
                               <th width="30%">Name</th>  
                               <th width="10%">Gender</th>  
                               <th width="50%">Designation</th>  
                          </tr>  
                          <?php   
                          while($row = mysql_fetch_assoc($result))  
                          {  
                          ?>  
                          <tr>  
                               <td><?php echo $row['B']; ?></td>  
                               <td><?php echo $row['D']; ?></td>  
                               <td><?php echo $row['E']; ?></td>  
                               <td><?php echo $row['F']; ?></td>  
                          </tr>  
                          <?php                           
                          }  
                          ?>  
                     </table>  
                </div> 
           </div>  
           <br />  
      </body>  
 </html>

And you can remove css and js files from it.

Also you need to update your script

 <script>  
 $(document).ready(function(){  
      $('#create_excel').click(function(){  
           var page = "excel.php";  
           window.location = page;  
      });  
 });  
 </script>  

Or you can try this lib (Datatable) : https://datatables.net/extensions/buttons/examples/initialisation/export.html

PHP Worm...
  • 4,109
  • 1
  • 25
  • 48