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:
<%Response.ContentType="application/vnd.ms-excel"%>
January<%=chr(9)%>1<%=chr(9)%>4<%=chr(9)%>Rain
February<%=chr(9)%>2<%=chr(9)%>4<%=chr(9)%>Overcast
March<%=chr(9)%>3<%=chr(9)%>4<%=chr(9)%>Sunny 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.