To generate reports or to print data or for some other purpose we often need data to be export in excel or csv format. It is quiet easy to fetch records from mysql and export the result set as .csv file or .xls file and let user to force download it. Here is the code:

Here, first we will extract the data which we want to export.
Then, will make make headers for exported file and then append the exported data
then will forcefully download the file.

–Export MySql data into Excel or CSV format–