-3

我有一个将 mysql 表转换为 excel 表的代码。它会转换并下载 excel 表,但是每当我尝试打开文件时,它会在 excel 2007 中给出错误消息:“您尝试打开的文件的格式与扩展名指定的格式不同......”。单击“是”后工作表打开,但它只是一张白色的空白表。我看过类似的帖子,但没有一个能解决我的问题...请帮助...谢谢

    <?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";

    ?>
4

1 回答 1

-2

尝试添加这个

header("Content-Type: application/vnd.ms-excel");
于 2013-05-06T12:31:53.163 回答