簡體   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