Best way to generate .xls file from PHP

5 replies
I need enable the website users to export some data into Microsoft Excel format. I googled around, but most solutions require Windows platform and that MS Office is installed to create the file. The website runs PHP on a Linux server, so I'm looking for PHP-only solution.

I'm currently using CSV format (via fputcsv function) and on some Windows machines the browser correctly opens the file with Excel, but it looks like different versions of Excel expect different field separators. I'm trying to find some solution that would work with all versions of MS Office.

Thanks.
#file #generate #php #xls
  • Profile picture of the author Bruce Hearder
    As the XLS is complicated format, and I'm sure you could buy a library that would do it for you, but another way you can handle the data is to export the data in XML format.

    Every version of Excel since 2003 has the ability to import and XML files.

    Well worth looking into

    Bruce
    {{ DiscussionBoard.errors[3546356].message }}
  • Profile picture of the author KirkMcD
    I usually create tab delimited files.
    You need to create a single file script that exports the data.
    Put these headers in it:
    header('Content-type: application/msexcel');
    header('Content-Disposition: attachment; "filename='.$filename.'.xls"'>
    Then loop through your data and "echo" each row. Seperate the columns with a tab "\t" and put a newline at the end "\n".

    That's the easy way.
    If you need to format the data, there is an alternate way. Just a tad more complex.
    I'll explain that way if needed.
    {{ DiscussionBoard.errors[3546828].message }}
    • Profile picture of the author Bruce Hearder
      KirkMcD is right, a tab-delimitered file is a great way to go, if the data you are exporting consists of numbers and text.

      But if you want to export other cell based data as well, then its a bit lacking.

      If you need to export formatting info (eg colours, fonts etc), or formulas etc then the TDF won't cut it..

      I guess its up to the OP to decided whats he needs to export and then choose the appropriate path..

      Take care

      Bruce
      {{ DiscussionBoard.errors[3547222].message }}
    • Profile picture of the author Bruce Hearder
      KirkMcD is right, a tab-delimitered file is a great way to go, if the data you are exporting consists of numbers and text.

      But if you want to export other cell based data as well, then its a bit lacking.

      If you need to export formatting info (eg colours, fonts etc), or formulas etc then the TDF won't cut it..

      I guess its up to the OP to decided whats he needs to export and then choose the appropriate path..

      Take care

      Bruce
      {{ DiscussionBoard.errors[3547246].message }}
      • Profile picture of the author Tashi Mortier
        I think I found something that you might want to consider using. Seems like the xslx format is easier to generate.

        I'd consider using that format since most new versions of Excel can open it, too. OpenOffice also has no problem with that format.

        Generate Excel spreadsheets in PHP

        PHPExcel
        Signature

        Want to read my personal blog? Tashi Mortier

        {{ DiscussionBoard.errors[3555373].message }}

Trending Topics