Question

I have this code:

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'test');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="file.xls"');
$objWriter->save('php://output');

I have 2 problems. Any headers I tried to save the file with .xlsx extension didn't worked. they all gave me a corrupted file.

My second problem which is much more important - this code worked with .xls extension.

But it download a file with all of the HTML content of the page and when I opened it Excell is giving me an error that I missing the css file.

I'm using this code on the page index.php with include(), the css file is called in index.php.

any help would be appreciated, thx!

Was it helpful?

Solution

i used this code in 4-5 projects and working perfectly, Check if helps

$objPHPExcel->getActiveSheet()->setTitle('Customers');
            $objPHPExcel->setActiveSheetIndex(0);

            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename=Customers-"' . date('d-m-y-h-i-s', time()));
            header('Cache-Control: max-age=0');

            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            $objWriter->save('php://output');
            exit;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top