tags:

views:

1032

answers:

7

What is the state generating Excel documents from a PHP application on a Linux server?

I am interesting in creating Office 97 (xls) Excel files. My limited research on the subject has turned up this Pear package. It appears to be in beta status since 2006.

Can you share your success or failures in generating Excel files from PHP? Is there a reliable and mature tool available?

Update: For this application I do need to generate an Excel file, not just a CSV file.

+2  A: 

I've used that PEAR package and it works great for me for Office '97-2000 compatible files.

If it's a simple spreadsheet, Office can handle CSVs as well, which PHP can output natively and easily.

ceejayoz
I'm just commenting on your profile...another SOer in the Rochester, NY area...I've found 3 so far.
Thomas Owens
+2  A: 

How about using tables (a spreadsheet is a table, so it's OK) and throwing these lines on the top of the PHP script that produces the table(s):

header("Content-Type: application/vnd.ms-excel");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

Or you can just produce a CSV file, as Excel can open those nicely.

Thomas Owens
I think I've seen this used before in an ASP app too. It seemed to work ok, but same thing as a CSV in technical limitations. I think Excel might warn you about inconsistent file types, too.
Nick
A: 

Everything is in beta status nowadays. Do you actually need excel files with forumlas and all that or do you just need to open a list of records in excel to work on?

Usually, I've found that when a client wants an Excel file, really I can just generate a CSV file and they can work on it in Excel and add formulas that they want after the fact.

Nick
+1  A: 

The easiest way is indeed to use the header to "fake" an Excel file. Here's the code to start a download as an Excel file, the client's Excel will just parse any HTML code that's found (tables, csv, ...) and display it in a nice Excel-like format.

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=excel.xls");
Mojah
+2  A: 

I've used the PEAR package with lots of success, but there are some limitations when implementing complex formulas. It has something to do with the way excel stores the formula, along with the last calculated result. Sometimes even though the formula is correct, when first opening the sheet the cells show as empty. Once the cell has focus and then loses focus, the calculation is performed and the cell populated.

The library is actually a port from a perl library, which is slightly more complete but has the same issues.

Dave Marshall
+2  A: 

I have used PERL Spreadsheet::WriteExcel in several proyects without problem. You may need to make some fixes and wrappers to enhance functionality and make it easier but I find it ok.

I like it because its platform independent, fast and easy to set up.

Cesar
+3  A: 

There is something much better than the PEAR package out there!

PHPExcel

tharkun