Exporting Data To Excel With PHP And MySQL

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

Exporting Data To Excel With PHP And MySQL

Last Updated On: October 22, 2017| By: Parvez

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,

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:

Live Demo

DownloadDebug: it's the locked content, but now it's revealed. Why?

Php php tutorials. permalink.

Last updated