Output file to browser

Topics: Developer Forum, User Forum
Apr 12, 2011 at 3:28 PM

Hi all

I am using PHPExcel 1.7.5 with Zend Framework 1.11.2 on XAMP set up on Windows 7.

I am trying to load an existing xls file and then add data to it then output it to user's browser to be opened/downloaded.

The file does get output to the browser, however upon opening it is full of errors:

Here is my code:

		// create reader
		$objReader = PHPExcel_IOFactory::createReader('Excel5');
		$objReader->setReadDataOnly(false);
		
		$objPHPExcel = $objReader->load(APPLICATION_PATH . "/views/scripts/template.xls");
		$objWorksheet = $objPHPExcel->getActiveSheet(0); 
		$objWorksheet->getCell('A1')->setValue('tet');
		
		ob_end_clean();
		
		// this is to prevent ZF generating all layout
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();

		// Redirect output to a client�s web browser
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.$safe_filename.'"');
		header('Cache-Control: max-age=0');	
		
    	$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
		ob_end_clean();
		
		$objWriter->save('php://output');
		$objPHPExcel->disconnectWorksheets();
		unset($objPHPExcel);

And here is the error message within the returned xls file:

Notice: ob_end_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete. in C:\xampplite\htdocs\PPCGearbox\application\controllers\BuildController.php on line 603

Fatal error: Uncaught exception 'Zend_Controller_Response_Exception' with message 'Cannot send headers; headers already sent in C:\xampplite\htdocs\PPCGearbox\application\controllers\BuildController.php, line 603' in C:\xampplite\htdocs\PPCGearbox\library\Zend\Controller\Response\Abstract.php:321 Stack trace: #0 C:\xampplite\htdocs\PPCGearbox\library\Zend\Controller\Response\Abstract.php(339): Zend_Controller_Response_Abstract->canSendHeaders(true) #1 C:\xampplite\htdocs\PPCGearbox\library\Zend\Controller\Response\Abstract.php(766): Zend_Controller_Response_Abstract->sendHeaders() #2 C:\xampplite\htdocs\PPCGearbox\library\Zend\Controller\Front.php(992): Zend_Controller_Response_Abstract->sendResponse() #3 C:\xampplite\htdocs\PPCGearbox\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch() #4 C:\xampplite\htdocs\PPCGearbox\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run() #5 C:\xampplite\htdocs\PPCGearbox\public\index.php(36): Zend_Application->run() #6 {main} in C:\xampplite\htdocs\PPCGearbox\library\Zend\Controller\Response\Abstract.php on line 321

Sincerely appreciate the help,

Sid.