PHP Classes

File: Export.php

Recommend this page to a friend!
  Classes of ganesh kavhar   Export MySQL Data to Excel in PHP Example   Export.php   Download  
File: Export.php
Role: Example script
Content type: text/plain
Description: Example script to export the results of a MySQL query to a Excel spreadsheet file to download
Class: Export MySQL Data to Excel in PHP Example
Execute a MySQL query and and export to Excel XLS
Author: By
Last change:
Date: 11 months ago
Size: 1,059 bytes
 

Contents

Class file image Download
<?php
//export.php
$connect = mysqli_connect("localhost", "root", "", "testing");
$output = '';
if(isset(
$_POST["export"]))
{
 
$query = "SELECT * FROM tbl_customer";
 
$result = mysqli_query($connect, $query);
 if(
mysqli_num_rows($result) > 0)
 {
 
$output .= '
   <table class="table" bordered="1">
                    <tr>
                         <th>Name</th>
                         <th>Address</th>
                         <th>City</th>
       <th>Postal Code</th>
       <th>Country</th>
                    </tr>
  '
;
  while(
$row = mysqli_fetch_array($result))
  {
  
$output .= '
    <tr>
                         <td>'
.$row["CustomerName"].'</td>
                         <td>'
.$row["Address"].'</td>
                         <td>'
.$row["City"].'</td>
       <td>'
.$row["PostalCode"].'</td>
       <td>'
.$row["Country"].'</td>
                    </tr>
   '
;
  }
 
$output .= '</table>';
 
header('Content-Type: application/xls');
 
header('Content-Disposition: attachment; filename=download.xls');
  echo
$output;
 }
}
?>