强制下载excel文件无法在Web服务器上运行

时间:2015-06-11 05:38:32

标签: php excel

以下编码完全适用于localhost,强制下载excel文件,但不能在Web服务器上运行。

$par_name="worker_report";
$nwdatetime=date('d-m-Y h-i-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'];  

2 个答案:

答案 0 :(得分:1)

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

或尝试使用此代码。

$par_name="worker_report";
$nwdatetime=date('d-m-Y h-i-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 M Y H:i:s').' GMT'); // always modified
    header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header ('Pragma: public'); // HTTP/1.0

    $_REQUEST['datatodisplay']; 

答案 1 :(得分:1)

application/vnd.ms-excel适用于使用Excel5 Writer创建的.xls个文件。

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet用于使用Excel2007 Writer

创建的.xlsx文件

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

  

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

     

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