Question

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.

Was it helpful?

Solution

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

PHPExcel

OTHER TIPS

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.

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.

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.

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.

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");

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top