PHPExcel: Automatically download and open an Excel file

主宰稳场 提交于 2019-12-13 14:27:56

问题


i have managed to create and save an excel file:

// Rename the file
$fileName = URL . "MODEL/case" . $caseNO . ".xlsx";

// Write the file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($fileName);

I would like now PHPExcel run Excel automatically, open the file created and maximize it. Is it possible? Will this work even if Excel is already running?

Thank you for your help,

Donato


回答1:


As per my above comment, you can only force to have a download option. For this you can set headers in this way -

header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
header("Cache-Control: max-age=0");

Reference - PHP Excel Reader

For more options you can also check the cheat sheet - Cheat Sheet

Although the best way to read here - Codeplex

EDIT

Do something like this -

$excel = new PHPExcel();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');  

// Do your stuff here

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');  

// This line will force the file to download    
$writer->save('php://output');



回答2:


PHPExcel can't run MS Excel on the client.... but you can download a file directly to the browser which will offer the client the options of saving it to disk or opening it directly in MS Excel (if they have MS Excel installed) by sending the appropriate http headers, and "saving" the file to php://output.

Of course, if the client doesn't have MS Excel installed, then opening in MS Excel isn't an option; although it will still prompt for save.

The 01simple-download-xlsx.php file in the /Tests or /Examples directory does exactly this

And "yes", it will work if MS Excel is already running on the client




回答3:


Insert the following headers just before creating the Writer

$filename = "filedetail". date("Y-m-d-H-i-s").".xlsx";
header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0'); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');



回答4:


header("Location: ".URL . "MODEL/case" . $caseNO . ".xlsx");


来源:https://stackoverflow.com/questions/15189296/phpexcel-automatically-download-and-open-an-excel-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!