Providing Data in Excel Format

If your users love Excel, or if you want to provide database query results or other data in a format that can be easily manipulated, then this technique is for you.

It's surprisingly easy to take any data you have and send it to the client as an Excel spreadsheet. When the user clicks on a link requesting the data, a dialog box comes up asking whether they'd like to open or save the document that's being sent. If they choose to open, Excel opens with your data in it. If they choose to save, an Excel spreadsheet file is saved on their hard drive.

How do you accomplish this magic? Well, on most computers with Excel installed, CSVs (comma-separated values) are associated with Excel. So all you have to do is tell the browser that the file you're sending back is of type .CSV and give it a filename. These two lines accomplish that task:

  Resonse.ContentType="application/csv"
  Response.AddHeader "Content-Disposition", "filename=ReqData.csv;"

Now all you have to do is format your data in CSV, and you're ready to go! For those of you who are a little rusty on that front, CSV separates fields or columns with commas and separates records or rows with a new line character. To display the contents of three 10-element arrays -- FirstName, LastName, and Phone -- it would look something like this:

For i=1 To 10
   Response.Write FirstName & "," & LastName & "," & Phone
   Response.Write vbNewLine
Next

You can use this technique with recordsets or virtually any other data source.

Bonus tip: Excel comes with a moving average analysis tool built in, but you have to install the Analysis ToolPak.

1