所以我在这里得到了这段代码我下网了,它应该将数据从mySQL数据库导出到excel文件。 问题是我应该如何触发这个?这没有图形方面,没有触发脚本的按钮,没有。 我怎样才能使它发挥作用?
<?PHP
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
include('config.php');
function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
// filename for download
$filename = "website_data_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$flag = false;
$result = pg_query("SELECT * FROM norse5_proov ORDER BY id") or die('Query failed!');
while(false !== ($row = pg_fetch_assoc($result))) {
if(!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
array_walk($row, 'cleanData');
echo implode("\t", array_values($row)) . "\r\n";
}
exit;
?>
答案 0 :(得分:1)
以下是一些适用于商业应用的代码;
if($_REQUEST["EXCEL"]){
header("Content-Type: application/excel");
header("Content-Disposition: attachment; FileName=assets.xls");
header("Cache-Control: private");
header("Content-Length: ".strLen($data));
echo $data;
exit;
}
$ data是制表符分隔文本,但excel标题打开Excel并导入文本