[英]How to export a excel file using php script
我正在使用php腳本導出Excel工作表,我的代碼是
<?php
function excel_file(){
header("Content-Disposition: attachment; filename=\"demo.xls\"");
header("Content-Type: application/vnd.ms-excel;");
header("Pragma: no-cache");
header("Expires: 0");
$out = fopen("php://output", "w");
foreach ($tabledata as $data)
{
fputcsv($out, $data,"\t");
}
fclose($out);
}
?>
HTML
<input type="button" name="excel_file" onclick="<?php excel_file(); ?>" value="Download File">
問題是這樣的:
該function excel_file()
在加載頁面時執行,而不是單擊。
我得到的excel文件,包括整個頁面的數據而不是那個垂直數組“ $tabledata
”的數據
您可以在按鈕中包含函數的輸出。 您不能將javascript onclick與php函數結合使用。
HTML:
<a href="your_url.php?action=xls">Excel</a>
然后在你的代碼中
if(isset($_GET['action']) && $_GET['action'] == 'xls')
{
excel_file();
exit;
}
這樣,您可以單擊Excel鏈接並將該操作發送到服務器。 使用PHP捕獲$_GET['action']
並調用excel_file()
函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.