Search code examples
phpcsvxls

What is the best way to create XLS file in PHP


I am currently trying to figure out the best way to create a .xls file from PHP. It seems like to me that all I need to do is change the header content type to "application/vnd.ms-excel", and then output data in a comma separated format. Is this the best way to do this?

Thanks for any help!
Metropolis

EDIT

Would it be bad to just output the data as HTML? I already have a class that I would like to extend for this problem, so I would prefer to stay away from a third party solution. I just need to know what the best way to output the data would be in order to get the most out of that file format.

After looking at PHPExcel it seems to be the best solution for this issue. I appreciate all of your answers and I would have given three correct answers if I could lol. But up votes will have to do :)


Solution

  • Depends if you want a CSV file or an XLS file. An XLS file can include formatting information for the cells, as well as row/column locking, protections and other features that are impossible in a CSV file. Also, keep in mind that Excel does not correctly support UTF-8 encoded content when opening CSV files.

    If you want a formatted XLS file, then you need a library such as PhpSpreadsheet that can write a file with that formatting, or COM if you're server is running on Windows with Excel installed