如何使用PHP导出Excel / CSV中的Mysql数据表?
问题描述:
我已经编写了以下代码来生成excel中的报告,但它只显示网页上的标题,甚至没有生成excel文件。请在这方面帮助我。
I have written the below code to generate a report in excel but it is only showing the headers on webpage,not even generating excel file.please help me in this regards.
<?php
error_reporting(0);
include("database.php");
$fromdate= $_GET['fromdate'];
$todate = $_GET['todate'];
echo $fromdate;
//First we'll generate an output variable called out. It'll have all of our text for the CSV file.
$out = '';
$csv_hdr .= "\n\n\n Id,Date,Time,Movie,Name,Email,Phone,Approved";
$out .= $csv_hdr;
$out .= "\n\n";
$result1 = mysql_query("SELECT * from bookings where (date)date between '$fromdate' and '$todate'");
while($row = mysql_fetch_array($result1))
{
$id= $row["id"];
$date= $row["date"];
$time= $row["time"];
$movie= $row["movie"];
$name= $row["name"];
$email= $row["email"];
$phone= $row["phone"];
$approved= $row["approved"];
}
$csv_output .= $id .", ".$date.", ".$time.", ".$movie.", ".$name.", ".$email.", ".$phone.", ".$approved;
$csv_output .= "\n";
$out .= $csv_output ."\n\n";
$filename = "FirstLook-$fromdate-$todate";
//Generate the CSV file header
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: attachment; filename=".$filename.".csv");
//Print the contents of out to the generated file.
echo "\n "." Report of FirstLook Preview";
print $out;
exit;
?>
新更新:何时被评论(0)使用error_reporting;我收到这些错误:
NEW UPDATE:when is commented error_reporting(0); I get these errors:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/public_html/mysite.com/admin/loggedin/bet_date_excel.php on line 18
Warning: Cannot modify header information - headers already sent by (output started at /home/vrwebqbj/public_html/mysite.com/admin/loggedin/bet_date_excel.php:8) in /home/public_html/mysite.com/admin/loggedin/bet_date_excel.php on line 37
Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/mysite.com/admin/loggedin/bet_date_excel.php:8) in /home/public_html/mysite.com/admin/loggedin/bet_date_excel.php on line 39
Report of FirstLook Preview Id,Date,Time,Movie,Name,Email,Phone,Approved , , , , , , ,
答
fromdate =
fromdate=
_GET [' fromdate'];
_GET['fromdate'];
todate =
todate =