-2

我正在使用此代码从数据库中获取我的数据并下载 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');

?>
4

2 回答 2

1

两个误解:

  1. 也许该功能称为“下载 Excel 文件”,但这并不意味着您必须创建一个真实文件。

  2. PHP 不会神奇地将您在服务器端创建的所有文件发送到浏览器。

摆脱所有与文件相关的代码并简单地改变它:

fwrite($fp,$schema_insert);

...进入这个:

echo $schema_insert;

另外,将这些行移到第一行之前echo

header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=my.xls');
header('Pragma: no-cache');

编辑:尽管多次提到 XLS 格式,但有问题的代码基本上会创建一个 CSV 文件,并使用伪造的 HTTP 标头发送它以欺骗 Excel 打开它。正如 Mark 指出的那样,PHP 具有创建 CSV 文件的内置函数,因此您不必自己编写代码。这个函数需要一个文件指针,但同样,实际上不需要创建一个文件:你可以调用fopen()作为文件名,php://output所有内容都会自动发送到浏览器。

于 2013-03-15T10:25:55.927 回答
0

也许服务器不能让你写文件。您只能从数据库中查询数据并将其格式化为表格,并使用标题功能将其下载。

于 2013-03-15T10:17:54.603 回答