devxlogo

Returning data in different file type

Returning data in different file type

Question:
We have an ASP application that queries a database and returns the data to formatted pages.

The user would also like the data returned in a .CSV format for further analysis in Excel. How can I return query results as a .csv file andwith the correct mime type so the browser opens it with Excel?

Answer:

I don’t have a real solution for you yet, but I can offer a couple of leads.

I couldn’t get Excel to recognize values that were fed into the stream when separatedby a comma. However, a tab character seemed to behave correctly as a separator.To use this method, you need to set the Content Type to application/vnd.ms-excel with no HTML tags at all. Here’s a little ASP code that demonstrates the technique using hardcoded values. You’d want to loop through your recordset to create this effect:

January14Rain
February24Overcast
March34Sunny Day

A more reliable solution might be to use Scripting.FileSystemObject to write therecordset data to a temporary .CSV file. You could use some scripting to format the nameof the temporary file as a hyperlink. If you click on the filename name (e.g. “199809152311.csv”), Internet Explorer should call on Excel to display and parse the data.

Although one of these kludges might get you by, let’s keep looking for an elegantsolution.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist