我有一个将mysql表转换为excel表的代码。它会转换并下载Excel工作表,但每当我尝试打开文件时,它会在excel 2007中给出错误消息:“您尝试打开的文件格式与扩展名指定的格式不同......”。excel单击“是”后页面打开但是它只是一张白色的空白表。我看过类似的帖子,但没有一个能解决我的问题...请帮助......谢谢
<?php
include('dbcon.php');
?>
<?php
$sql = "SELECT * FROM stu_gen_info";
$rec = mysql_query($sql) or die (mysql_error());
$num_fields = mysql_num_fields($rec);
for($i = 0; $i < $num_fields; $i++ )
{
$header .= mysql_field_name($rec,$i)."\\t";
}
while($row = mysql_fetch_row($rec))
{
$line = '';
foreach($row as $value)
{
if((!isset($value)) || ($value == ""))
{
$value = "\\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\\n";
}
$data = str_replace("\\r" , "" , $data);
if ($data == "")
{
$data = "\\n No Record Found!\n";
}
header("Content-Type: application/vnd.ms-excel");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=reports.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\\n$data";
?>
答案 0 :(得分:-2)
尝试添加此
header("Content-Type: application/vnd.ms-excel");