stream to browser fails when using a template

Topics: Developer Forum
Aug 19, 2010 at 4:01 AM

Hi. I've been able to successfully output an Excel file and also stream the file for download to the user's browser. However, when I introduce an Excel template as the basis for my file, the streaming outputs an Excel file, but it's full of errors. If I take out the streaming and just write the Excel file to the server, no problems--so I don't think it's my code to actually create the file. I've tried changing file names, etc., no luck. Any ideas for how I can use a template and stream the contents so I don't have to save Excel files on the server? Here's my code (note I'm inserting content from a database query above this code snippet):

$objPHPexcel = PHPExcel_IOFactory::load('template.xls');

$objWorksheet = $objPHPexcel->getActiveSheet();

$objWorksheet->getCell('B4')->setValue(''.$row_getCompanyName['company'].'');
$objWorksheet->getCell('B6')->setValue(''.$row_GetKnowBoard['project_name'].'');
$objWorksheet->getCell('B7')->setValue( 'Last Updated: '.$row_GetKnowBoard['last_updated'].'');

//Loop through our SQL results to populate the rows...
$row = 10;
while ($rows = mysql_fetch_array($GetItems, MYSQL_NUM)) {
   $column = 'B';
   foreach($rows as $columnData) {
      $cellRef = $column.$row;
      $objWorksheet->getCell($cellRef)->setValue($columnData);
      $column++;
   }
   $row++;
}

// Output the headers so we can stream to the user's browser:
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=template.xls");
header("Content-Transfer-Encoding: binary ");

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
Any help would be greatly appreciated!

Aug 19, 2010 at 4:51 AM

Nevermind folks! (Can't believe I didn't catch the typo when referencing the object in createWriter.) I ended up using a different method, but problem solved nonetheless.