Export data feature is very useful where the data is saved on the local drive for offline uses. Export data to file functionality provides a user-friendly way to maintain a large number of data in the web application. There are various file formats are available to export data and download it as a file. Microsoft Excel is a widely used spreadsheet format that organizes and maintains data.
Generally, export data functionality is used in the data management section of the web application. 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 will show you how to export data to Excel in PHP.
The example PHP script lets you integrate export data to excel functionality. With one click, the user can export data from the MySQL database to Excel and download it in MS Excel file format [.xls/.xlsx].
Export Data to Excel with PHP
In this example script, we will export data from the array [defined in the script] to an excel file.
The $data
variable holds the data in array format which will be exported to Excel using PHP.
$data = array[
array["NAME" => "John Doe", "EMAIL" => "", "GENDER" => "Male", "COUNTRY" => "United States"],
array["NAME" => "Gary Riley", "EMAIL" => "", "GENDER" => "Male", "COUNTRY" => "United Kingdom"],
array["NAME" => "Edward Siu", "EMAIL" => "", "GENDER" => "Male", "COUNTRY" => "Switzerland"],
array["NAME" => "Betty Simons", "EMAIL" => "", "GENDER" => "Female", "COUNTRY" => "Australia"],
array["NAME" => "Frances Lieberman", "EMAIL" => "", "GENDER" => "Female", "COUNTRY" => "United Kingdom"]
];
The filterData[]
function is used to filter string before added to the excel sheet row.
function filterData[&$str]{
$str = preg_replace["/\t/", "\\t", $str];
$str = preg_replace["/\r?\n/", "\\n", $str];
if[strstr[$str, '"']] $str = '"' . str_replace['"', '""', $str] . '"';
}
The following code helps to export data in excel and download it as a file.
- The
$fileName
variable defines the name of the excel file. - The Content-Disposition and Content-Type headers force the excel file to download.
- Run the loop through each key/value pair in the
$data
array. - Display column names as the first row using the
$flag
variable. - The PHP array_walk[] function is used to filter the data together with
filterData[]
function.
// Excel file name for download
$fileName = "codexworld_export_data-" . date['Ymd'] . ".xlsx"; // Headers for download
header["Content-Disposition: attachment; filename=\"$fileName\""];
header["Content-Type: application/vnd.ms-excel"]; $flag = false;
foreach[$data as $row] {
if[!$flag] {
// 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";
}exit;
Export Data from Database to Excel with PHP and MySQL
In this example script, we will export data from the MySQL database in an excel file using PHP.
Create Database Table:
For this example, we will create a members
table with some basic fields in the MySQL database. The members
table holds the records which will be exported to excel.
CREATE TABLE `members` [ `id` int[11] NOT NULL AUTO_INCREMENT, `first_name` varchar[25] COLLATE utf8_unicode_ci NOT NULL, `last_name` varchar[25] COLLATE utf8_unicode_ci NOT NULL, `email` varchar[50] COLLATE utf8_unicode_ci NOT NULL, `gender` enum['Male','Female'] COLLATE utf8_unicode_ci NOT NULL, `country` varchar[20] COLLATE utf8_unicode_ci NOT NULL, `created` datetime NOT NULL, `status` tinyint[1] NOT NULL DEFAULT 1 COMMENT '1=Active | 0=Inactive', PRIMARY KEY [`id`] ] ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Database Configuration [dbConfig.php]:
The following code is used to connect the database using PHP and MySQL. Specify
the database host [$dbHost
], username [$dbUsername
], password [$dbPassword
], and name [$dbName
] as per your database credentials.