繁体   English   中英

强制下载Excel文件在Web服务器上不起作用

[英]force download excel file not working on web server

以下代码可在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'];  

将内容类型更改为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']; 

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文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM