我正在使用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);
}
?><input type="button" name="excel_file" onclick="<?php excel_file(); ?>" value="Download File">问题是:
function excel_file()在加载页面时执行,而不是单击。$tabledata“的数据。发布于 2014-05-27 09:37:17
在按钮中包含函数的输出。不能将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()函数。
https://stackoverflow.com/questions/23885838
复制相似问题