PHPExcel File format or extension is not valid

后端 未结 2 731
后悔当初
后悔当初 2020-12-21 00:44

I\'m using phpexcel for export my query in excel file; however after I created file(which is xslx format), I can not open my file in excel. It gives \"the file format or ext

相关标签:
2条回答
  • 2020-12-21 01:22

    As described in the manual.... if anything else is being output to the browser, this will corrupt the output file.

    Open the file in a text editor, and look for leading or trailing whitespace characters (spaces, tabs, newlines) or a BOM marker at the beginning of the output, or for any obvious PHP plaintext error messages in the content. These are the most obvious causes of this problem. Once you've identified the spurious characters, check through your script to see where that output is being generated, and remove it.

    In your case, that means don't output your css and html.

    EDIT

    xlsx is the extension for an OfficeOpenXML Excel2007 file, not for a BIFF 8 xls file.... be consistent in your headers (mime type and file extension) and Writer

    Either:

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="userList.xls"');
    

    or

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="userList.xlsx"');
    
    0 讨论(0)
  • 2020-12-21 01:40

    Following documentation or comments somewhere I read, I had this:

    $objWriter = PHPExcel_IOFactory::createWriter($spreadsheet, 'Excel2007');
    header('Content-Type: application/vnd.ms-excel');
    // etc
    

    instead of

    $objWriter = PHPExcel_IOFactory::createWriter($spreadsheet, 'Excel2007');
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    // etc
    

    And that caused Firefox and IE/Edge to append an .xls to the filename and also give the error message when trying to open the file directly instead of downloading it. When downloading the file and opening the spreadsheet though, everything was fine.

    0 讨论(0)
提交回复
热议问题