save as...

Nov 1, 2008 at 9:43 AM

I read an excel file from and fill in some data. After that, I want to output it to the user. So that he gets 'save as...' possibility. But for now it only works by saving it to my webspace... How can I fix this?

$objReader        = new PHPExcel_Reader_Excel5();
$objPHPExcel    = $objReader->load('1.XLS');
$objWriter        = new PHPExcel_Writer_Excel5($objPHPExcel);

Nov 1, 2008 at 3:41 PM
Here is the method for Excel5 writer (Excel 2003 format). It allows you to specify the suggested filename in the download dialog. Moreover it ensures that Excel file will not be cached. This is important if you later modify the Excel file on server. Otherwise the browser may not deliver the newest version of the Excel file the second time the download link is clicked.

$excel = new PHPExcel();

// do stuff to build the spreadsheet

$writer = new PHPExcel_Writer_Excel5($excel);

header('Content-Type: application/');
header('Content-Disposition: attachment;filename="My Excel File.xls"');
header('Cache-Control: max-age=0');


For the Excel 2007 format replace lines above with these:

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="My Excel File.xlsx"');

Previous experience tells me that it can be quite tricky to get this stuff working the way one want it. I am not even sure that the above headers are technically correct. Everyone seems to have their own favorite set of headers. Due to bugs, there can especially be problems with Internet Explorer (both IE6 and IE7). If you are not careful, IE may 1) ignore the suggested filename, 2) not recognize filetype, 3) refuse to download at all. Therefore, I strongly recommend that you test extensively on all the browsers / platforms that you expect your users to work on.

Nov 1, 2008 at 5:09 PM
Thanks for the replay. When I use the php://output... I get following error:

<b>Fatal error</b>: Uncaught exception 'Exception' with message 'Can't open php://test.xls. It may be in use or protected.' in C:\wamp\www\library\PHPExcel\Shared\OLE\OLE_Root.php:103


Nov 1, 2008 at 5:26 PM
Do not do like this:

Do like this:

Nov 3, 2008 at 10:56 AM
Ok, but what is 'output'? Is it a variable? 'Cause I'm getting an empty spreadsheet... (what does the php:// do? Don't know that command?)

this is my code:


// loading the template.xls
$objReader = new PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load($config->application->upload.'/riolering/onderstation/inspectieformulier/template.XLS');

// filling in some data

// output
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=test.xls");
header("Content-Transfer-Encoding: binary ");


Nov 3, 2008 at 12:49 PM
Sorry, I managed to fix the problem. 'Cause I'm working with the Zend Framework I still had to do a render. Forgot that :). thanks anyway.
Oct 14, 2009 at 2:59 PM
Edited Oct 14, 2009 at 3:00 PM

How did u solve it?


Oct 14, 2009 at 2:59 PM


I am having the same problem using Zend Framework with PHPExcel.

Did you use $objWriter->save('php://output');?

Could you please post a sample of code?


Oct 14, 2009 at 8:16 PM

Hey Jia,

I'm also working with the Zend Framework and PHPExcel. Here is a little example. If it doesn't work out, post your code, or give us the error you get.

I think it's the "die()" that you didn't do. I found it out myself that it works this way.

public function testAction() {

  $objPHPExcel = new PHPExcel();
  $objPHPSheet = $objPHPExcel->getActiveSheet();
  $objPHPSheet->setCellValue('A1', 'blabla');
  ... input ... layout ...
  header("Pragma: public");
  header("Expires: 0");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Content-Type: application/force-download");
  header("Content-Type: application/octet-stream");
  header("Content-Type: application/download");
  header("Content-Disposition: attachment; filename=blabla.xls");
  header("Content-Transfer-Encoding: binary");

  $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);


Nov 6, 2009 at 12:05 PM

You should use Action Helper Context Switch.

In the init() of your Controller:


public function init()
	$excelConfig = array(
		'excel' => array(
		'suffix'  => 'excel',
		'headers' => array(
			'Content-type' => 'application/', // for excel2007: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
			'Cache-Control' => 'max-age=0',
			'Content-Disposition' => 'attachment;filename=My File.xls"'
	$contextSwitch = $this->_helper->contextSwitch();
	$contextSwitch->addActionContext('excel-export', 'excel');

then in the action do your Zend Framework stuffs, remember that with context switch you should create two views: excel-export.phtml and excel-export.excel.phtml, put your PHPExcel logic in this last view.

Apr 1, 2010 at 12:35 AM
Edited Apr 1, 2010 at 12:36 AM

I don't know what writer's save method does, but if all you want is to disable rendering using setNoRender() method of ViewRenderer would be more appropriate (i.e.


) instead of using die().


Aug 31, 2010 at 7:07 PM

Just curious. How are you integrating the library into the Zend Framework? 

Feb 18, 2014 at 9:30 AM
i made same, but error 'Cannot read file because wrong file extension