我正在使用此代码从数据库中获取我的数据并下载 xls 文件,但它没有将我的实际数据写入工作表。我认为这行不通。我应该在我的代码中添加更多内容吗?
<?php
include 'functions.php';
ob_start();
?>
<?php
if($_POST['types'] == 'name') {
$query = "SELECT * FROM visitor_detail WHERE name='".$_POST['filter']."' ORDER BY id DESC ";
}
elseif($_POST['types'] == 'mobile') {
$query = "SELECT * FROM visitor_detail WHERE mobile='".$_POST['filter']."' ORDER BY id DESC ";
}
elseif($_POST['types'] == 'OccasionType') {
$query = "SELECT * FROM visitor_detail WHERE OccasionType='".$_POST['filter']."' ORDER BY id DESC ";
}
elseif($_POST['types'] == 'InquiryDate') {
$query="SELECT * FROM visitor_detail WHERE TodayDate between '".$_POST['From']."' and '".$_POST['TO']."' ORDER BY id DESC " ;
}
elseif($_REQUEST['types'] == 'OccasionDate') {
$query="SELECT * FROM visitor_detail WHERE date between '".$_POST['From']."' and '".$_POST['To']."' ORDER BY id DESC ";
}
else {
$query = "SELECT * FROM visitor_detail ORDER BY id DESC ";
}
$result = mysql_query($query);
$sep = "\t"; //tabbed character
$fp = fopen('database.xls', "w");
$schema_insert = "";
$schema_insert_rows = "";
//start of printing column names as names of MySQL fields
//start of adding column names as names of MySQL fields
for ($i = 1; $i < mysql_num_fields($result); $i++)
{
$schema_insert_rows.=mysql_field_name($result,$i) . "\t";
}
$schema_insert_rows.="\n";
echo $schema_insert_rows;
fwrite($fp, $schema_insert_rows);
//end of adding column names
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=1; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= strip_tags("$row[$j]").$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
//this corrects output in excel when table fields contain \n or \r
//these two characters are now replaced with a space
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\n";
//$schema_insert = (trim($schema_insert));
//print $schema_insert .= "\n";
//print "\n";
fwrite($fp,$schema_insert); }
fclose($fp);
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=my.xls');
header('Pragma: no-cache');
?>