PHP Classes

Export MySQL Data to Excel in PHP Example: Execute a MySQL query and and export to Excel XLS

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-08 (4 days ago) RSS 2.0 feedNot enough user ratingsTotal: 101 This week: 101All time: 9,744 This week: 1Up
Version License PHP version Categories
export-mysql-data-to 1.0The PHP License5HTML, PHP 5, Databases, Files and Fol...
Description 

Author

This package can execute a MySQL query and export it to Excel.

It provides two example scripts that can execute a given MySQL query.

One script can output the query results to a Web page in an HTML table.

The other script can generate an HTML table that can be served for download in a file format for opening with Microsoft Excel.

Picture of ganesh kavhar
Name: ganesh kavhar <contact>
Classes: 3 packages by
Country: Indonesia Indonesia

Example

<?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;
 }
}
?>


Details

Export-MySql-Data-to-Excel-in-PHP

Php Mysql Database to Excel.

Normal Databse View

image

Click on Export to Excel

One popup show the filename.xlx save it with any name according to requirement.


  Files folder image Files  
File Role Description
Accessible without login Plain text file Export.php Example Example script to export the results of a MySQL query to a Excel spreadsheet file to download
Accessible without login Plain text file Index.php Example Example script to display the results of a MySQL query
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:101
This week:101
All time:9,744
This week:1Up