Exporting Data to Excel with PHP and MySQL

Here in this quick tutorial I will let you know how to export grid data into excel file. Export/Import is very common functionality for web development, some time we need to export all grid data into excel file that time we need below method to export data into excel file. It’s very easy and simple steps in PHP, we need to set header information to force browser to open download window.

You can also check other tutorial of Export Data with PHP,

Here we have sample result set from MySQL and want to export in excel file.

Step 1: Get MySql data in key value pair as like below.

Step 2: PHP code to get options type and force to browser download file instead of display.

Above code use to get format type and tells that browser to download excel file.

Step 3: Define html layout for display data in table and button to fire export-to-csv action.

Step 4: Now we will use jQuery code to get click event.


export-to-excel with php and mysql
I hope its help you!.

Demo and Download source Code From Below Link

  • kk

    Dear friend this is more like csv format, excel format contains many features like formatting ,auto calculations ..etc.
    You should use open source excel libraries for that.


    • Yes you r right but if you want your custom features then you need your own created lib.

  • lutfi setiono

    thank you very much …

  • Aravind Kumar

    Thank You very much

  • Shahid

    Worked from me thanks…..

  • do you need export particular index or row?currently code export an array of records

  • Dhawal Naik

    Thank you very much.

  • IT Solution

    Please help me fix this link, i can not download it. Thank

    • You need to share this article using social buttons after that download button enable.

  • Prafful Panwar Mali

    its just showing data in firebug.not able to download Excel

    • i am checking demo on chrome v55 and its working fine.

  • thank you so much !!!
    it was really helpful

  • กรรมกร สร้างซอฟต์แวร์

    thank you