简体   繁体   中英

transfer data from Mysql to excel

I had created a website using php . In that website,I want to create a report like thing,which involves transferring details from one table to an excel sheet which is downloadable.Is there any way to do this work? Please help me to find the solution.

Thank you.

You can fetch all rows from the database and use fputcsv function to put it as csv

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=export.csv");
header("Content-Transfer-Encoding: binary");

$csvHandler = fopen("php://output", 'w');
while ($row = mysql_fetch_array ($res))
{
    fputcsv($csvHandler, $row);
}

You can use PHPExcel for generating data

and a simple function for file download:

function show_download($data, $name, $type = "unknown/unknown") {
header('Content-Type: ' . $type);
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Content-Length: ' . strlen($data));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

print $data;

exit();

}

You can create a script that exports a CSV file like this :

header('Content-type: text/csv');
header("Content-Disposition: attachment; filename=clients_" . date("Ymd") . ".csv");

//Open PHP output stream
$fd = fopen('php://output', 'w');

$tab = array();

//get your results from your database
foreach ($results as $res)
   $tab[] = array($res->Client_Name, $res->Client_Email);

foreach ($tab as $val)
    fputcsv($fd, $val);

fclose($fd);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM