Export Data To Excel In PHP

Hello guys,
PHP HURDLES is once again here with yet another intersting and commonly asked topic Export Data to Excel in PHP.

Exporting data from an Access database to in a file format that can be read by Excel. This article shows you how to prepare and export your data to Excel, and also gives you some troubleshooting tips for common problems that might occur.
Sometimes we need to save data list on our computer for offline use. Export data feature is very useful for this purpose.It makes our web application more user friendly and helps the user to maintain the data list. Excel is the best format to Export data in a file and you can easily export data to Excel using PHP.

In this tutorial, we’ll show you how to export data to Excel in PHP. Our simple PHP script lets you implement export data to excel functionality. By one click, the user can export data to Excel and download it in a .xls file.

lets start with the topic,

First of all we will fetch data from database for that purpose we will create a file user_info.php and in that file we will create a function userinfo() which will return a array $info with values.



And finally we will create index.php which will contain export_data() function and a export link for exporting data

//code for export_data() function in index.php which will export data into excel on export link click
export_data()function will have a filterData() function for filter string provided by the data array and headers for the download
function filterData(&$str)//We have created filterData() function for filter string provided by the data array.
$str = preg_replace(“/\t/”, “\\t”, $str);
$str = preg_replace(“/\r?\n/”, “\\n”, $str);
if(strstr($str, ‘”‘)) $str = ‘”‘ . str_replace(‘”‘, ‘””‘, $str) . ‘”‘;
$fileName variable hold the name of file for download and also content header is defined for download.

// file name for download
$fileName = “phphurdles_export_data” . date(‘Ymd’) . “.xls”;

// headers for download
header(“Content-Disposition: attachment; filename=\”$fileName\””);
header(“Content-Type: application/vnd.ms-excel”);
Now we have run loop through each key/value pair in $data. Using of $flag variable we will display column names as first row. array_walk() is used to filter the data together with filterData() function.

$flag = false;
foreach($user as $row)//$user is the array which we will call from user_info.php {
// display column names as first row
echo implode(“\t”, array_keys($row)) . “\n”;
$flag = true;
// filter data
array_walk($row, ‘filterData’);
echo implode(“\t”, array_values($row)) . “\n”;


//code for export link on index.php which on click will export datalist in excel


So this is how we can export data into excel using a simple PHP script.we have tried to use the most easy PHP script so that the export data functinality can be understood by our fellow developers easily.
For the working Demo please download our demoDownload Here

Hope this tutorial will help our fellow developers dont hesitate to comment in case of any doubt or suggestions.

keep moving
Because hurdles aren’t really hurdles


  1. Saurabh Dubey said:

    Thank You

    April 22, 2017
    • Deepak Shah said:

      thankyou so much for that much of support and appreciation

      May 21, 2017
  2. Raj Roshan said:

    Nice bro really helpful

    July 1, 2017
    • Saurabh Dubey said:

      Thank You, Raj! We will continue helping you in future

      July 1, 2017

Leave a Reply

Your email address will not be published. Required fields are marked *