tags:

views:

98

answers:

7

Hey Everyone,

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 :)

A: 

Try the XLS file wrapper, ala this post on SO.

cam8001
A: 

Take a look at this PEAR package: Spreadsheet Excel Writer

Davide Gualano
A: 

Yes, that is one possible way - if you need a plain CSV file that will open in Ms Excel.

If you need a rich XLS file, there are many options including using third party DLLs such as 'CarlosAg.ExcelXmlWriter.dll' to create the XLS file. You can search for samples of using this dll on the net or Look here

J Angwenyi
A: 

if you want to give out something that opens with excel, that might work. be sure to give the "csv" extension to the generated file.

otherwise, if you want to actually generate xls files, you should use Spreadsheet Excel Writer.

you can also convert to xls files using unoconv

kgb
+1  A: 

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.

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

Mark Baker
+1  A: 

You could use one of the XML formats that Microsoft Office understands.

See Microsoft's OpenXML Developer site for details/specs.

There is a library on CodePlex called PHPExcel that you can look at to help you with this.

Of course, this all depends on what you mean by:

"get the most out of that file format"

If you just have simple tables with no formatting, CSV files may be all you need; however, if you want to use more features of a spreadsheet, I would recommend taking a look at OpenXML.

Robert
+1  A: 

You can just output html and newer versions of Excel will read it. I don't know how much formatting you can do with it though.

If you need data typing, rich formatting or formulas, I have had success with PHPExcel ( http://phpexcel.codeplex.com/ ).

My preference is to write out CSV files. CSV is a very easy format to write and an easy conversion from existing html table scripts. It also has the advantage of being readable by a wide variety of non-microsoft spreadsheet programs. If you can make CSV the file format of choice for your web application, you will reap rewards when you have to accept a spreadsheet as input. It is much, much easier to read and parse a CSV file than an Excel file.

Scott Saunders
I agree with you about CSV files Scott. If it was up to me that is the format I would be using. But unfortunately I need to use XLS....
Metropolis