Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Active Server Pages (ASP)
Expertise: Beginner
Oct 5, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Returning data in different file type

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 and with the correct mime type so the browser opens it with Excel?


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 separated by 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:


March<%=chr(9)%>3<%=chr(9)%>4<%=chr(9)%>Sunny Day

A more reliable solution might be to use Scripting.FileSystemObject to write the recordset data to a temporary .CSV file. You could use some scripting to format the name of 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 elegant solution.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date