简体   繁体   中英

force download excel file not working on web server

This following coding working on localhost perfectly for force to download excel file, but doesn't working on web server.

$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'];  

Change the content type to Content-Type: application/vnd.ms-excel

Or try this code if works.

$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 is for .xls files created with the Excel5 Writer.

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet is used for .xlsx files created with the Excel2007 Writer

So you have to check what are you doing there

header('Content-Type: application/vnd.ms-excel'); is for .xls files

header('Content-Type: application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet'); is for .xlsx files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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