How to write a PHP script that dynamically builds Excel spreadsheets

By marcain

Rate: (5 Ratings)

How to build a PHP script that can be used to dynamically build an Excel spreadsheet.

Instructions

Difficulty: Moderate

Things You’ll Need:

  • A Mac setup as a web server running PHP 4.x or later
  • Safari 2.x/3.x or Firefox 2.x or later

Step1
Create a file called export.php in the document root of your hard drive and make sure that the file's permissions are set so it's accessible by web clients.

Typically the document root on MacOS X Server will be in /Library/WebServer/Documents/

If you're using the non-Server version of MacOS X, your document root is probably setup in the 'Sites' folder of your home folder.
Step2
The export.php PHP code Edit the export.php file and add the text contained in this screen shot.
Step3
The Excel spreadsheet built by the PHP script Save the file, open your browser and navigate to http://localhost/export.php and then check your downloads folder for a file named 'downloaded_from_website.xls'.

You will NOT see a page in your browser. Typically this PHP script will be used in an anchor tag within another PHP page.

Open the file in your spreadsheet application and you should see the html table.
Step4
If the file does not download then look at your apache error logs to locate the problem. Open the Terminal application in /Applications/Utilities/

On default installations of MacOS X Tiger run this

cd /var/log/httpd/

On default installations of MacOS X Leopard run this

cd /var/log/apache2/

Now we're going to 'tail' the error log so that we can watch as new errors are appended to the log. To do that on default installations of MacOS X type

tail -f error_log

Select your browser again and navigate to http://localhost/export.php while watching the Terminal window. If you see new errors added to the log as the page loads go through them and check the code in export.php as you may have a syntax error.

When you're done tailing the log file hit the 'Control' key and 'c' at the same time to stop the process.

Please comment and rate this article. Thanks!

Tips & Warnings

  • You can use HTML to decorate and align the HTML table.
  • If you're going to offer dynamic downloads you might want to consider adding the date and time to the filename or insert it into the data.
  • If you want your files with a .php extension to open in a custom application when you double click them just follow the directions here http://www.ehow.com/how_2135899_open-files-application-choosing-mac.html.
  • Empty the cache in your browser if you're making changes to export.php and you're not seeing the changes in the downloaded file.

Post a Comment

POST A COMMENT

Request a New How-To Article

Looking for more How To information? Chances are there’s an eHow member who knows how to do what you’re looking to do. Submit an article request now!

eHow Article:  How to write a PHP script that dynamically builds Excel spreadsheets

eHow Member: marcain

marcain

Enthusiast Enthusiast | 1990 Points

Category: Computers

Articles: See my other articles

Related Ads