0

我有一个脚本,它将接受我的 sql 语句并转换为 excel。我希望将 excel 文件保存在文件夹中,而不是提示下载,因为我想处理 excel 文件。

我的脚本

<?php


/* Connecting, selecting database */
$db_link = mysql_connect("localhost", "root", "");
if (!$db_link) {
   die("Could not connect: " . mysql_error());
}
mysql_select_db("mydb") or die("Could not select database");

/* Performing SQL query */
$sQuery="select * from comments where `comment` like '%apple%';";


$rResult = mysql_query( $sQuery) or die();
$count = mysql_num_fields($rResult);

$html = '<table border="1"><thead><tr>%s</tr><thead><tbody>%s</tbody></table>';
$thead = '';
$tbody = '';
$line = '<tr>%s</tr>';
for ($i = 0; $i < $count; $i++){      
  $thead .= sprintf('<th>%s</th>',mysql_field_name($rResult, $i));
}


while(false !== ($row = mysql_fetch_row($rResult))){
  $trow = '';

  foreach($row as $value){
   $trow .= sprintf('<td>%s</td>', $value);
  }

  $tbody .= sprintf($line, $trow);

}


header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: attachment; filename=exportfile.xls");
header("Pragma: no-cache");
header("Expires: 0");

print sprintf($html, $thead, $tbody);
exit;

?>
4

1 回答 1

1

只需删除文件下载标题并保存文件

/*
header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: attachment; filename=exportfile.xls");
header("Pragma: no-cache");
header("Expires: 0");
print sprintf($html, $thead, $tbody);
exit;
*/

file_put_contents("exportfile.xls", $html.$thead.$tbody); // make sure that write permissions are set
于 2013-10-09T07:51:49.957 回答