I need to create a couple of Excel files and output the results. The first use PHPExcel and works great
...
$sheet1->setCellValue('B' . $row, 'Date:');
}
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="TradeDirectivesForm.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
// Once we have finished using the library, give back the
// power to Yii...
// spl_autoload_register(array('YiiBase', 'autoload'));
Yii::app()->end();
The second spreadsheet uses macros and PHPexcel can’t handle that. So I create it with perl Excel Writer. That creates the file correctly but I can’t get it to download. No amount of figdeting with headers or ob_* made a difference, it displays raw text gook. Is there a simple way sending an excel to a user? I could put the file somewhere else. How can I download it so that it knows it is an xlsm? emailing seems to work as a last resort.
private function perlExcel()
{
// perl excel writer supports macros, phpexcel doesn't
$x = exec("/usr/bin/perl (PATH)/protected/extensions/hramacro.pl 2>&1");
// var_dump($x); die;
$file = (PATH)/protected/extensions/macros.xlsm";
ob_start();
//Prepare the headers
header('Content-Description: File Transfer');
// header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Type: application/vnd.ms-excel.sheet.macroEnabled.12');
// header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="macros.xlsm"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
//Header added after the file was created to be able to check its size
header('Content-Length: ' . filesize($file));
// var_dump(headers_list());
// var_dump(headers_sent());
// Clean the buffer, read the file so that the user sees/downloads it, then remove the file.
ob_end_flush();
ob_end_clean();
readfile($file);
usleep(50000);// delay minimum of .05 seconds to allow ie to flush to screen
// $form = ob_get_clean();
//echo $form;
// $this->renderText($form, false);
// unlink($file);
// $this->redirect(array('employer/index'));
Yii::app()->end();
}