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   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-08 (9 months ago) RSS 2.0 feedNot enough user ratingsTotal: 137 All time: 9,252 This week: 36Up
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: 4 packages by
Country: Indonesia Indonesia
Age: ???
All time rank: 360856 in Indonesia Indonesia
Week rank: 215 Up6 in Indonesia Indonesia Up

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 (3)  
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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:137
This week:0
All time:9,252
This week:36Up