[英]PHP: How to save mysql to excel file to a directory and not prompt user to download
I have got a script that will take my sql statement and convert to excel. 我有一个脚本,它将使用我的sql语句并将其转换为excel。 I would like the excel file to be saved in a folder instead of prompting to download cause I would like to work on the excel file.
我希望将excel文件保存在文件夹中,而不是提示下载原因,因为我想使用excel文件。
MY SCRIPT 我的脚本
<?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;
?>
Just remove the file download headers and save the file 只需删除文件下载标题并保存文件
/*
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.