0
votes

I got stuck in PHPExcel issue. To generate xls file I use the following code

require_once dirname(__FILE__) . '/PHPExcel/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('Data');

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Hello')
            ->setCellValue('B2', 'world!')
            ->setCellValue('C1', 'Hello')
            ->setCellValue('D2', 'world!');

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

If I launch this php file directly I recieve correct xls file. So this code works correctly. But I have to add launchinig this code on form submit. And in such case I recieve html file with filename "01simple.xlsx".

Why it happened? Any ideas?

Thanks in advance.

1
It's likely that your application is still echoing output to the browser even though you're trying to send the output from PHPExcel to the browser as well. Make sure your own application doesn't output anything to the browser when sending a file to php://output - Mark Baker

1 Answers

0
votes

You can add ob_end_clean(); before the output is rendered.