我正在尝试从MySql数据库记录中下载一个excel文件,但是我没弄错我的意思。这是我的代码。

    $sql_ft1 = "SELECT `Id`,`fname`,`lname` FROM users";
$rs_ft1  = mysql_query($sql_ft1) or die(mysql_error());
$total1=mysql_num_rows($rs_ft1);
$output.='  <table border="1">
<tr>
    <th>Sr NO.</th>
    <th width="120">User Name</th>
    <th>Password</th>
</tr>
';` while($data = mysql_fetch_assoc($rs_ft1))
{`$output.= '
<tr>
<td>'.$data["Id"].'</td>
<td>'.$data["fname"].'</td>
<td>'.$data["lname"].'</td>
</tr>
'; }
$output.='</table>';


header("Content-type: application/xls");
header("Content-Disposition: attachment; filename=User_Detail_Reoprt.xls");

echo $output;


我用您的代码做了一个新代码,因为它给了php错误。我将某些内容更改为“,并且在内容标题中也更改了文件名”,但此方法不起作用

$strtable.='<table border="1"><tr><th>Sr NO.</th><th width="120">User Name</th><th>Password</th>';
 $sql_ft1 = "SELECT `Id`,`fname`,`lname` FROM users";
$rs_ft1  = mysql_query($sql_ft1) or die(mysql_error());
$total1=mysql_num_rows($rs_ft1);`    while($data = mysql_fetch_assoc($rs_ft1)){$strtable.='<tr>

        <td>'.$data["Id"].'</td>
        <td>'.$data["fname"].'</td>
        <td>'.$data["lname"].'</td>';
}
$strtable.='</tr>';
$strtable.='</table>';

echo $strtable;
$data = ob_get_contents();`
ob_end_clean();
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment;  filename=User_Detail_Reoprt.xls");`header("Pragma: no-cache");
header("Expires: 0");
print $data;
  echo $filename;

最佳答案

尝试这个,

$strtable.="<table border="1"><tr><th>Sr NO.</th><th width="120">User Name</th><th>Password</th>";

 $sql_ft1 = "SELECT `Id`,`fname`,`lname` FROM users";
    $rs_ft1  = mysql_query($sql_ft1) or die(mysql_error());
    $total1=mysql_num_rows($rs_ft1);
$strtable.="<tr>
            <td>$data["Id"]</td>
            <td>$data["fname"]</td>
            <td>$data["lname"]</td>";
$strtable.="</tr>";
$strtable.="</table>";
echo $strtable;

$data = ob_get_contents();
ob_end_clean();
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment;  filename="User_Detail_Reoprt.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$data";

10-06 14:37