3

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

user1956370
  • 55
  • 1
  • 1
  • 5
  • I think this is impossible. After saving your excel file is passive. and also your php file cannot access to user's computer and cannot control. That would be a disaster! – zkanoca Mar 03 '13 at 18:40
  • The only thing you can do it to have a download box popup instead of simple saving...for this you need to set headers – swapnesh Mar 03 '13 at 18:43
  • Hello swapnes, forgive me for the dull question. Could you explain a bit more? – user1956370 Mar 03 '13 at 18:48
  • @user1956370 Most of the things I mentioned in my answer posted below...and let me know in case of query – swapnesh Mar 03 '13 at 18:51

4 Answers4

12

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');
Community
  • 1
  • 1
swapnesh
  • 26,318
  • 22
  • 94
  • 126
  • Forgive me swapesh, the code you wrote is javascript correct? When you say it will force a download do you mean it will open the file? – user1956370 Mar 03 '13 at 19:18
  • @user1956370 These are PHP headers ..and no need for forgiveness kina words..its ok if u dont know that I know, but there are many other things that U definitely know but i don http://php.net/manual/en/function.header.php – swapnesh Mar 03 '13 at 19:21
  • 1
    @user1956370 Use this $objWriter->save('php://output'); – swapnesh Mar 03 '13 at 19:23
  • When using $objWriter->save('php://output'); in shared hosting, I get an strange error. Any clues? Uncaught exception 'PHPExcel_Writer_Exception' with message 'Could not open php://output for writing.' in third_party/PHPExcel/Writer/Excel2007.php:241 Stack trace: #0 – Enrique Oct 28 '15 at 15:44
  • 1
    @Enrique you might be interested with this http://stackoverflow.com/a/21440309/639406 – swapnesh Oct 28 '15 at 17:08
  • how to do this via ajax call? – shorif2000 Jul 19 '16 at 10:12
  • To add to this answer and for those still using PHPExcel, `ob_clean()` should be added before `$writer->save('php://output');` and `exit;` should be added at the end of the script. – Maurice Aug 18 '17 at 20:36
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

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
1

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');
HARDIK
  • 72
  • 8
0
header("Location: ".URL . "MODEL/case" . $caseNO . ".xlsx");
Abduhafiz
  • 3,318
  • 5
  • 38
  • 48
  • Please don't simply post the code. Give some explanation or information or usage about your code. For example, see [this answer](http://stackoverflow.com/a/16893057/756941). – Nazik Dec 23 '13 at 05:17