Google SEO | PHP | Mysql | Ajax | Jquery | Photoshop | Illustrator | Database | Web Design | Web Development | Sitemap |

In most of web portals admin need the report of website in daily or monthly basis to monitor his website. In this article i will explain you how to generate an report from your database and exporting it as a excel file.

Export database record to excel in PHP

You can view demo and download the example file below

Download File Demo

In this example i have created one form and when u submitting a form the report will generate and you can download it. Find below the PHP code.

//Enter the headings of the excel columns
$contents="id,day name,Date,urls,titles,status\n";

//Mysql query to get records from datanbase
//You can customize the query to filter from particular
date and month etc...Which will depends your database structure.
$user_query = mysql_query('select id,display_date,url,
title,status from tablename');

//While loop to fetch the records
while($row = mysql_fetch_array($user_query))
{
$contents.=$row['id'].",";
$contents.=$row['date'].",";
$contents.=$row['page_url'].",";
$contents.=$row['title'].",";
$contents.=$row['status']."\n";
}

// remove html and php tags etc.
$contents = strip_tags($contents); 

//header to make force download the file
Header("Content-Disposition: attachment; filename=
WebinfopediaDemoExcelExport".date('d-m-Y').".csv");
print $contents;
PHP will fetch the record and export it as an excel file. You can customize the sql query and get your required output.

Top Λ

Comments (31)

  • sachin

    it's 100% works well for me
    Monday-Feb-2014 | 02:43:34 PM
  • shadow

    thanks so much. this is what i wanted.. :)
    Sunday-Feb-2014 | 11:04:16 AM
  • shana

    hi..........this code work very successfully but i wand one more table of data how can i fetch this data?
    Tuesday-Feb-2014 | 11:22:24 AM
  • shana

    how can fetch other table of data with a table of data
    Tuesday-Feb-2014 | 11:20:03 AM
  • fawaz

    the solve to avoid open csv as txt and dowload it directly open php.ini in your server and change output_buffering to ON
    Monday-Feb-2014 | 03:47:10 AM
  • chetan

    I am getting data on html pageand download does not start nor make any csv file, is there any server side changes require?
    Sunday-Feb-2014 | 08:52:21 PM
  • Ankit Kumar

    Hii..I have used your code and its very effective...I have to make header column in bold how can I do it...any idea bcoz its very argent..????
    Friday-Jan-2014 | 05:17:51 PM
  • Iman

    Thanks alot
    Where the excel file is located?
    Monday-Dec-2013 | 09:43:26 PM
  • chill

    Was searching whole day regarding this and finally found this amazing code. Thankyou so much.
    Tuesday-Dec-2013 | 05:11:21 PM
  • Admin

    Hi kali
    Its because of PHP header. Kindly check your PHP header to save the data as excel file.
    Wednesday-Nov-2013 | 02:34:52 PM
  • kali

    I got the data from mysql, but it displays as text format on my page. How can i solve this, please help me. Thank You.
    Thursday-Oct-2013 | 06:11:09 PM
  • vasanth

    Thank you very much.. this code help me to resolve the problem.
    Tuesday-Oct-2013 | 05:04:57 PM
  • sovat

    This code worked fine, but it is unknown Japanese language in excel file. How to solve it? Thank in advance.
    Sunday-Sep-2013 | 11:49:51 AM
  • Admin

    Hi hasti,
    Please make sure that you have declared php header properly.. Please take a look at Mr. Rahil's comment in this article, which will be helpful for you..
    Saturday-Sep-2013 | 03:32:55 PM
  • hasti

    excel file is not downloading please help me ASAP....
    Thursday-Sep-2013 | 03:57:22 PM
  • hasti

    its not downloading result will be displayed in that page only what can i do?
    Thursday-Sep-2013 | 03:39:24 PM
  • karthik

    Thanks you so much.Its working for me. amazing code.
    Tuesday-Sep-2013 | 02:11:52 PM
  • karthik

    Thanks you so much.Its working for me. amazing code.
    Tuesday-Sep-2013 | 02:11:27 PM
  • Rahil

    Solution to "HTML codes in the downloaded Excelfile"
    just mention the following statement before while loop in the source code provided in this article:
    $fp = fopen('php://output','w');
    
    Friday-Aug-2013 | 02:53:05 AM
  • Michael

    Amazing post, thanks a lot, straight to the point! Keep up the good work man~!
    Wednesday-Jul-2013 | 12:49:17 PM
  • Reggie Jan Tolibas

    Thank you it really helps me a lot.!GREAT JOB!
    Wednesday-Jun-2013 | 07:05:23 PM
  • Prasad

    Nice.. Sweet
    Sunday-Jun-2013 | 02:24:02 PM
  • Agus M. S

    Thank you very much, your script work with well..
    Tuesday-Jan-2013 | 08:36:41 AM
  • Raman

    thank you very much
    Wednesday-Dec-2012 | 01:04:01 PM
  • miznAah

    Good example. it really helps..
    Tuesday-Dec-2012 | 01:22:27 PM
  • nous

    it displays the html code too..wat to do, to avoid this???
    Monday-Nov-2012 | 04:24:30 PM
  • Yogesh Jadhav

    Its really fabulous. Thanx a lot.
    Monday-Nov-2012 | 05:48:33 PM
  • keireret

    God Expl, Thank you
    Wednesday-Oct-2012 |
  • ABcd

    it also downloads html code, how to solve this problem??
    Monday-Apr-2012 | 4:7:44 PM
  • rajuram

    Thanks you very much for export database example
    Monday-Feb-2012 | 6:2:39 PM
  • softtech

    when you search in Google , lots of example but this one is easiest one among them. Thank you very much
    Wednesday-Jan-2012 | 10:29:43 AM

Post Comment

Name:
Email:
Divider