强制下载Excel文件不能在Web服务器上工作

这下面的编码工作完全强制本地主机下载Excel文件,但不能在Web服务器上工作。

$par_name="worker_report"; $nwdatetime=date('dmY hi-s'); $fi = $par_name."_". $nwdatetime; $extns=".xls"; header('Content-Type: application/force-download'); header('Content-disposition: attachment; filename='.$fi.$extns); header("Pragma: "); header("Cache-Control: "); $_REQUEST['datatodisplay']; 

将内容types更改为Content-Type: application/vnd.ms-excel

或者试试这个代码,如果工作。

 $par_name="worker_report"; $nwdatetime=date('dmY hi-s'); $fi = $par_name."_". $nwdatetime; $extns=".xls"; // Redirect output to a client's web browser (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename='.$fi.$extns); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header ('Last-Modified: '.gmdate('D, d MYH:i:s').' GMT'); // always modified header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header ('Pragma: public'); // HTTP/1.0 $_REQUEST['datatodisplay']; 

application/vnd.ms-excel是用Excel5 Writer创build的.xls文件。

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet用于使用Excel2007 Writer创build的.xlsx文件

所以你必须检查你在那里做什么

header('Content-Type: application/vnd.ms-excel');.xls文件

header('Content-Type: application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet');.xlsx文件