×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: Office FAQ

Tips and Tricks

Exporting from a Database to Excel by Glowball
Posted: 3 Oct 07

You may want to generate an Excel spreadsheet on your Web site using data in your database.  This is the easiest and most reliable way to do it.
  1. Put together a SQL query that gets the data from your Web site database and puts it into the query results.  For example:

    CODE

    SELECT firstname, lastname, message FROM mytable
  2. Using the programming language of your choice, build a CSV file (comma-separated values), putting the header row at the top.  Output it to your Web browser's screen in the usual way, so you can verify the CSV.  Unless you are absolutely sure that your field will be numeric you should enclose it with quotes.  If you think that your field results might have quotes in it, change these to double quotes first.  For example:

    CODE

    "First Name","Last Name","Message"
    "Bob","Smith","Hi there!"
    "Jane","Van Wild","You are ""so"" cool"
  3. Run the Web page and verify that your content looks like a CSV file (you'll have to view the page source)
  4. Above the output on your page and before any and all output, you'll need to tell the page that it needs to generate a CSV.  It's also good practice to name the file that your user is downloading.  This is done by sending header information.  The exact function you use depends on the programming language you're using.  Here is an example for PHP, with the month and year set earlier in the script:

    CODE

    $filename = "download_" . $year . sprintf("%02d", $month) . ".csv";
    header("Content-Type: text/csv");
    header('Content-Disposition: attachment; filename="' . $filename . '"');
  5. Run the page again to verify that it works.  You may need to hold your CTRL button down during the entire process to get it to work successfully.
I hope this helps someone!

Back to Microsoft: Office FAQ Index
Back to Microsoft: Office Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close