phpsymfonyphpexcel

PHPExcel : header full of giberish


I wan't to extract an html table to a .xsl file. I've installed PHPExcel in symfony2, but when i just want to test it by extracting a random page to an .xls file, first part's file is full of gibberish and i really don't know why : http://images4.hiboox.com/images/2714/f657dfd1d406e941cfbe3ff9dbc10f7b.png

And below this i've got my html table.

here 's the code, thanks :

$objPHPExcel = new \PHPExcel();
    $objPHPExcel->getProperties()->setCreator("foo");
    $objPHPExcel->getProperties()->setLastModifiedBy("bar");
    $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
    $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
    $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
    $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
    $objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
    $objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
    $objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');
    echo date('H:i:s') . " Write to Excel2007 format\n";
    $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
    //$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
    //$objWriter->save('a.xls');
    header('Content-type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename="PV AAPC"');
    $objWriter->save('php://output');

Solution

  • Make sure that you're not sending anything else to output. Ensure that you have no echo or print statements; no BOM headers in your script file; and no blank lines between ?> and <?php tags.

    You're also writing the file using the Excel2007 Writer (which generates an OfficeOpenXML-format .xlsx file) but sending the content type for a BIFF-format .xls file.

    You need to send a content type header of application/vnd.openxmlformats-officedocument.spreadsheetml.sheet for an OfficeOpenXML-format .xlsx file (generated using the Excel2007 Writer); or use the Excel5 Writer if you want to create a BIFF-format .xls file, which requires a content type header of application/vnd.ms-excel