ASP Q&A: Creating a CVS File Using Classic ASP | CodeGuru

ASP Q&A: Creating a CVS File Using Classic ASP

This is the fourth in a series of tips being posted based on the most read posts in one of our ASP Forums. (Previous Q&A) Problem / Question: I have generated a report that displays on the screen as a table in the web browser. Now the customer would like to export to a CSV […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 12, 2016
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

This is the fourth in a series of tips being posted based on the most read posts in one of our ASP Forums. (Previous Q&A)

Problem / Question:

I have generated a report that displays on the screen as a table in the web browser. Now the customer would like to export to a CSV file so they can import the data into whatever app they’d like.

The fields to be exported are:

  • CustomerName
  • ContactName
  • Firstinput
  • EmployeeName
  • EmployeeRate
  • Hours
  • TOTAL

I’ve found a few examples online, but none that I could get to work.

Solution:

I would not try to generate the CSV file on the same ASP page that generates browser output. The  “option” to export to CSV should be a link or button that takes me to a completely different page. If you do that, then code like the following should work to generate  the entire CVS file without the need for looping or other oddities:

CONST QT = """"
...
SQL = "SELECT field1, field2, ... FROM table ... "
Set RS = conn.Execute( SQL )
If RS.EOF Then Response.End

For f = 0 To RS.Fields.Count-1
    hdr = hdr & "," & QT & RS.Fields(f).Name & QT
Next
Response.Write Mid(hdr,2) & vbNewLine ' lop off leading comma

prefix = QT
midfix = QT & "," & QT
suffix = QT & vbNewLine
rows = RS.getString( , , midfix, suffix & prefix )
Response.Write prefix & Left(rows, Len(rows) - Len(prefix) )
RS.Close
...

Based on posts by forum members ITJoe and Bill Wilkerson

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.