eHow launches Android app: Get the best of eHow on the go.

How To

How to write a PHP script that dynamically builds Excel spreadsheets

Member
By marcain
User-Submitted Article
(10 Ratings)

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

Difficulty: Moderate
Instructions

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
  1. Step 1

    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.

  2. Step 2
    The export.php PHP code
     
    The export.php PHP code

    Edit the export.php file and add the text contained in this screen shot.

  3. Step 3
    The Excel spreadsheet built by the PHP script
     
    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.

  4. Step 4

    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.
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. † requires javascript

eHow Computers
eHow_eHow Technology and Electronics