Menu

Download Mysql Database to Excel Using PHP

PHP By Oct 21, 2013 4 Comments

Download Mysql Database to Excel

Hello friends today i am going to help you in Download Mysql Database to Excel using PHP. Many times we need it due to downloading the reports to our PC or our Client PC. Generating report in excel is too easy and we also use the easiest way to do it. One more thing from this code you cannot modify your excel too much for getting advanced facility click here for the package.

Here we will use the simple and straight method to get the MYSQL data as Excel sheet so lets start.

Things what we need.

  • database
  • PHP Code
  • Excel
  • Stylesheet

Step 1. Create Database in Mysql.

First of all we will create a database and a table in our database. We will create the database with the name of “testing”. After creating the database we will create the table in the database with the two column username and password. Here i am not going to tell you the whole code for creating the database and tables.

Step 2. We will create some stylesheet.

You will think that why we need a stylesheet for this. We are saving our data in the excel not in pdf or somewhere else. So let me tell you if you dont put the stylesheet you will get some of  the blank fields in the excel. So we will create the stylsheet which will be followed by the excel too. Create the stylesheet and see it over your php page. Create your stylesheet as you like. I have mine like.

.list{width: 100%;float: left;border-collapse: collapse;border: 1px solid #E1E1E1;margin:5px 0px; }
.list tr th{text-align: left;color: #0D2F73 ;font-size: 13px;background-color: #F2F2F2;font-weight: bold;line-height: 30px;padding: 5px;}
.list tr td{font-size: 13px;line-height: 20px;border: 1px solid #E1E1E1;border-left: 1px solid #E1E1E1;border-right: 0;padding: 5px;}
.list tr td img{height: 30px}

 Step 3. PHP Code for generating

This is our last step we will create the excel from our database a very short code will do all the things for you. Its easy to understand and easy to integrate.

$filename ="Employee Report";
header ( "Content-Type: application/vnd.ms-excel" );
header ( "Content-disposition: attachment; filename=$filename.xls" );
header ( "Content-Type: application/force-download" );
header ( "Content-Transfer-Encoding: binary" );
header ( "Pragma: no-cache" );
header ( "Expires: 0" );
$contents.=downloadReport();
echo $contents;

You will see a function downloadReport(). In this function will call our table and than the echo $contents will download the excel sheet. As these all things are done with the help of headers.

That’s it you can use it for downloading the mysql database table to excel format. I hope you will like Download Mysql Database to Excel using PHP post if you have any issues than comment and i will try to solve them ASAP. Thank You.

~~~~ HAPPY CODING ~~~~

Author

Hello i am Vivek Moyal PHP developer and writes tutorials for students and beginners. Love to explore new cities

4 Comments

  1. pls help me

    i download the databasr mtct.bak
    i wish to export to excel
    how can do this?

  2. ahmad says:

    i am unable to download package please send me other link 

  3. deep kumar says:

    Really nice……i was searching this one from llast 4 days…and today i got…..tanku a lot sir

    1. Vivek Moyal says:

      Thank You….. my efforts works for you nothing is better than this.

Leave a comment

Your email address will not be published. Required fields are marked *