繁体   English   中英

PHP:如何将mysql的excel文件保存到目录,而不提示用户下载

[英]PHP: How to save mysql to excel file to a directory and not prompt user to download

我有一个脚本,它将使用我的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;

?>

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

/*
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM