We often need to generate reports in excel sheet, but sometime we have our data stored in MySQL database. It is quiet easy to fetch records from MySQL and export the result in .csv file or .xls file and let user to force download it. We will make use of PHP and MySQL to do this functionality. This downloading option is very important in web application. Using this code we can generate a report from our MySQL data.

This tutorial will explain about downloading the MySQL data into excel sheet format using PHP MySQL. Here we will select all the data as per our MySQL query and generate a excel file.

This tutorial contain three files and one folder as follow:

  1. index.php
  2. connection.php (to connect with database)
  3. ExportToExcel.php (it contain function to export excel sheet)
  4. uploads (folder for uploading exported excel sheet)

Here is connection.php file, it will contain our script to database.

ExportToExcel.php is main file which contain function to export excel sheet. You can simply include this file to your file from which you want to export excel sheet. Include this file like this:

And then call this function:

Replace table_name with your required table.

(Also Read: Import Excel File Data in MySQL Database using PHP)

Here is ExportToExcel.php file:

I have made a simple HTML form which contain one input button and after hitting this button your excel sheet will generate and will upload in you “uploads” folder and then you will see a “Download Link” to download your excel sheet. Here is what I have made:

Note: It will generate .csv file.


(Also Read: Import Excel File Data in MySQL Database using PHP)


  1. Thousand of thanks, with your code I can develop it as my need. I spend my weeks to find it and you give me a great enlightenment

  2. Hi. Should I just change the [$filename = “uploads/”.strtotime(“now”).’.csv’;] into [$filename = “uploads/”.strtotime(“now”).’.XLS’;] for me to get an xls file?

  3. Hello I need a method of .XLS format is there a way to do so can you please let me know as I searched a lot but not happening I need .xls format file what should i DO FOR it as i changed format to .xls but not working brother can you help me out

  4. Hello, thank you for a great script. How can I export it with Turkish characters? It also causes problems while importing same file to database with your import script.

  5. I set this all up almost exactly but instead of just altering the table I have an entire SQL Statement however when i run it the file does not get uploaded I had php echo the statement and it is going through but i just cant get it to upload. Are there limitations that I don’t know of?

  6. Hello if I want to update the same csv file, what I must do ? I want the data to be exported to the same csv file
    Thanks in advance

  7. Hello. Tanks a lot for code, u helped me very much 🙂 but I have one problem, i am downloading excel file and it doesn’t divide them to columns, everything in one row and column for each ID. What should I do divide them for different columns, can u help me plz?

  8. Hi ,

    Thanks for the code..
    probably the only code i found that works for me…
    only 1 small issue

    data gets shifted to next tab for values if any cell value is a statement.

Leave a Reply