Export To Excel In PHP With MySQL | Complete demo code

Spread the love with your friends

Download mysql record in excel using PHP
Download mysql record in excel using PHP

Dear friends, Today we will discuss how to download MySQL database record in excel using PHP.  To download the record in excel or csv carefully see the following complete program below and after that we will discuss each and every step how to possible to download PHP MySQL records download in excel sheet

First of all make a separate file named excel.php and write this complete code :

<?php
ob_start();
ini_set(‘error_reporting’, E_STRICT);
include(“connect.php”);
$output = ”;

$sql = “SELECT * from `survey_details` ORDER BY `sr` DESC”;
$result = mysqli_query($conn, $sql) or die (mysqli_error());
if(mysqli_num_rows($result)>0)
{
$output .= ‘
<table class=”table” bordered=”1″>
<tr style=”background-color:#000″>
<td><font color=”#FFFFFF”>Sr</font></td>
<td><font color=”#FFFFFF”>Username</font></td>
<td><font color=”#FFFFFF”>Project ID</font></td>
<td><font color=”#FFFFFF”>Status</font></td>
<td><font color=”#FFFFFF”>IP Address</font></td>
<td><font color=”#FFFFFF”>Date(Time)</font></td>
</tr>’;
$SR=0;
while($row=mysqli_fetch_array($result))
{
$SR=$SR+1;
$output .=’
<tr>
<td>’.$SR.'</td>
<td>’. $row[‘username’].'</td>
<td>’.$row[‘project_id’].'</td>
<td>’.$row[‘status’].'</td>
<td>’.$row[‘ipaddress’].'</td>
<td>’.$row[‘datee’].'(‘.$row[‘timee’].’)’.'</td>

</tr>
‘;
}
$output .='</table>’;
header(“Content-Type: application/xls”);
header(“Content-Disposition: attachment; filename=download.xls”);
echo $output;

}
?>

In this above code, We write the complete code about how to download MySQL records easily in PHP.

we also just run a PHP script, you can write the PHP script as per your requirement. Here we fetch all the records from MySQL database and download in excel sheet.

Here we are fetching data from a `survey_details` table. you can write another MySQL table name here as per your database.

after that we write a code for a header, we make a header in excel sheet as per format of records.

<table class=”table” bordered=”1″>
<tr style=”background-color:#000″>
<td><font color=”#FFFFFF”>Sr</font></td>
<td><font color=”#FFFFFF”>Username</font></td>
<td><font color=”#FFFFFF”>Project ID</font></td>
<td><font color=”#FFFFFF”>Status</font></td>
<td><font color=”#FFFFFF”>IP Address</font></td>
<td><font color=”#FFFFFF”>Date(Time)</font></td>
</tr>’;

In this code that exist on above given code have the header or tilte in excel sheet. and then as per respective titles we fetch all the records from MySQL database.

Now below be focus on this code:

header(“Content-Type: application/xls”);
header(“Content-Disposition: attachment; filename=download.xls”);
echo $output;

This is the major code which is responsile to download files in a given (user define) file format. We are downloading file in ‘xls’ format. so that we already mentioned the file format  and we also mention the name of file which will be download with ‘download.xls’. With this name the file will be download. in Content-Type you can write the file extension like xls, csv and other supportive formats.

 

So guys this post was about , How to download MySQLrecords in excel using PHP. If you like this post and want another help in codings then make a comment and share this valuable information to your friends. Thanks


Spread the love with your friends

Leave a Comment

PHP Code Snippets Powered By : XYZScripts.com