krishna kumar
krishna kumar

Reputation: 153

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.

Upvotes: 0

Views: 536

Answers (3)

Amit
Amit

Reputation: 1385

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);
}

Upvotes: 1

user2219905
user2219905

Reputation: 38

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);

Upvotes: 0

George Sharvadze
George Sharvadze

Reputation: 570

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();

}

Upvotes: 0

Related Questions