简体   繁体   English

将数据从Mysql传输到Excel

[英]transfer data from Mysql to excel

I had created a website using php . 我已经使用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? 在该网站中,我想创建一个类似东西的报告,该报告涉及将详细信息从一个表传输到一个可下载的excel工作表。有什么办法可以完成这项工作? 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 您可以从数据库中获取所有行,并使用fputcsv函数将其作为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 您可以使用PHPExcel生成数据

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 : 您可以创建一个导出CSV文件的脚本,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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