---
description: https://www.phpflow.com/php/exporting-data-to-excel-with-php-and-mysql/#
---

# Exporting Data To Excel With PHP And MySQL

## Exporting Data To Excel With PHP And MySQL

{% file src="https://3988971916-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LXUV09EJOZ8BKB8YL5I%2F-LzzN1Gvrqj0G2dmzWWZ%2F-LzzNGNxvpxzl3X48XMB%2Fexport_to_excel_code.zip?alt=media&token=ebe869bf-47e9-4d59-b66c-45405d00ba5a" %}

Last Updated On: [October 22, 2017](https://www.phpflow.com/php/exporting-data-to-excel-with-php-and-mysql/)| By: [Parvez](https://www.phpflow.com/author/admin/)

Here in this quick **tutorial** I will let you know how to **export** grid data into **excel file**. **Export/Import** is very common functionality for web development, some time we need to export all grid data into excel file that time we need below method to export data into excel file. It’s very easy and simple steps in PHP, we need to set header information to force browser to open download window.

You can also check other tutorial of Export Data with PHP,

* [Exporting Data to Excel with PHP and MySQL](https://www.phpflow.com/php/exporting-data-to-excel-with-php-and-mysql/)
* [Export Data to CSV and Download Using PHP and MySQL](https://www.phpflow.com/php/export-data-to-csv-and-download-using-php-and-mysql/)
* [Import CSV File Into MySql Using PHP](https://www.phpflow.com/php/import-csv-file-into-mysql/)
* [Export HTML Table Data to Excel, CSV, PNG and PDF using jQuery Plugin](https://www.phpflow.com/php/export-html-table-data-to-excel-csv-png-and-pdf-using-jquery-plugin/)
* [Export the jQuery Datatable data to PDF,Excel,CSV and Copy](https://www.phpflow.com/php/how-to-export-the-jquery-datatable-data-to-pdfexcelcsv-and-copy/)

Here we have sample result set from MySQL and want to export in excel file.

**Step 1:** Created sample MySQL data in key value pair as like below.

| 1234567891011121314151617 | Array(    \[14] => Array        (            \[Name] => Parvez Alam            \[Age] => 21            \[Gender] => Male        )     \[15] => Array        (            \[Name] => Rajesh Sharma            \[Age] => 21            \[Gender] => Male        ) ) |
| ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

**Step 2:** PHP code to get options type and force to browser download file instead of display.

| 12345678910111213141516171819202122232425262728293031 | if(isset($\_POST\["ExportType"])){    switch($\_POST\["ExportType"])    {        case "export-to-excel" :            // Submission from $filename = $\_POST\["ExportType"] . ".xls";            header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=\\"$filename\\""); ExportFile($data); //$\_POST\["ExportType"] = '';            exit();        default :            die("Unknown action : ".$\_POST\["action"]);            break;    \}}function ExportFile($records) { $heading = false; if(!empty($records))   foreach($records as $row) { if(!$heading) {   // display field/column names as a first row   echo implode("\t", array\_keys($row)) . "\n";   $heading = true; } echo implode("\t", array\_values($row)) . "\n";   } exit;} |
| ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |

Above code use to get format type and tells that browser to download excel file.

**Step 3:** Define html layout for display data in table and button to fire `export-to-csv` action.

| 12345678910111213141516171819202122 | \<div>\<a href="javascript:void(0)" id="export-to-excel">Export to excel\</a>\</div> \<form action="\<?php echo $\_SERVER\["PHP\_SELF"]; ?>" method="post" id="export-form"> \<input type="hidden" value='' id='hidden-type' name='ExportType'/>   \</form>                  \<table id="" class="table table-striped table-bordered">                    \<tr>                        \<th>Name\</th>                        \<th>Status\</th>                        \<th>Priority\</th>                        \<th>Salary\</th>                  \</tr>                \<tbody>                  \<?php foreach($data as $row):?>   \<tr>   \<td>\<?php echo $row \['Name']?>\</td>   \<td>\<?php echo $row \['Status']?>\</td>   \<td>\<?php echo $row \['Priority']?>\</td>   \<td>\<?php echo $row \['Salary']?>\</td>   \</tr>   \<?php endforeach; ?>                \</tbody>              \</table> |
| ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

**Step 4:** Now we will use jQuery code to get click event.

| 123456789101112131415 | \<script  type="text/javascript">$(document).ready(function() {jQuery('#Export to excel').bind("click", function() {var target = $(this).attr('id');switch(target) { case 'export-to-excel' : $('#hidden-type').val(target); //alert($('#hidden-type').val()); $('#export-form').submit(); $('#hidden-type').val(''); break\}});    });\</script> |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

#### Result:

![export-to-excel with php and mysql](https://www.phpflow.com/wp-content/uploads/2014/11/export-to-excel.png)\
I hope its help you!.

### Demo And Download Source Code From Below Link

[ Live Demo](https://www.phpflow.com/demo/export_to_excel_demo/)

[ Download](https://www.phpflow.com/source_code/export_to_excel_code/export_to_excel_code.zip)**Debug:** it's the locked content, but now it's revealed. [Why?](https://www.phpflow.com/php/exporting-data-to-excel-with-php-and-mysql)

&#x20;[Php](https://www.phpflow.com/category/php/)  [php tutorials](https://www.phpflow.com/tag/php-tutorials/).  [permalink](https://www.phpflow.com/php/exporting-data-to-excel-with-php-and-mysql/).
