Export MySQL Table Data into Excel Sheet Format in PHP

Export MySQL Table Data into Excel Sheet Format in PHP

Export MySQL Table Data into Excel Sheet Format in PHP

DOWNLOADDEMO

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.

DOWNLOADDEMO

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

27 Comments

  1. ayikocharlz May 19, 2015
  2. Hafiz Abdullah Majid May 19, 2015
  3. yasser July 22, 2015
  4. Hafiz Abdullah Majid July 23, 2015
  5. CrunchyBench July 29, 2015
  6. Hafiz Abdullah Majid August 5, 2015
  7. prathyusha October 9, 2015
  8. Bao Tran December 9, 2015
  9. Abdullah Majid December 10, 2015
  10. Usman Khan December 24, 2015
  11. Muhammad Saad Salman January 2, 2016
  12. iceberk January 9, 2016
  13. Abdullah Majid January 13, 2016
  14. mais January 27, 2016
  15. Robert Holden February 10, 2016
  16. hizbullah February 29, 2016
  17. Abdullah Majid February 29, 2016
  18. Svetly March 1, 2016
  19. Abdullah Majid March 3, 2016
  20. ankit verma April 22, 2016
  21. Baghirli Orkhan April 28, 2016
  22. __N3WL!F3 May 9, 2016
  23. Elias May 13, 2016
  24. DildarKhan July 12, 2016
  25. ABRA July 18, 2016
  26. ABRA July 18, 2016
  27. Ravi Kumar August 22, 2016

Leave a Reply