How to Create a CSV File in ASP

Active Server Pages (ASP) is a web scripting language produced by Microsoft. It operates on the server, so it can manipulate files that reside on the server. A Comma Separated Values (CSV) file is a text file that contains a record on each line comprised of a series of data fields separated by a comma. ASP can create, write to and read a CSV text file. CSV text files are often used in lieu of a database for storing information that will be needed later.

Instructions

    • 1

      Create an ASP page using a web editor or a text editor. Save the file with the ".asp" extension. This tells the server that the file contains ASP code. The code can be placed anywhere within the ASP page because the server will recognize all of the code between the "<%" and "%>" tags as ASP.

    • 2

      Create an instance of the File System Object. The File System Object is a class included with the ASP class library. It allows files to be created and manipulated on the server.

      <%

      set fso=Server.CreateObject("Scripting.FileSystemObject")

      %>

    • 3

      Create a text file using the CreateTextFile() function of the File System Object. This function takes a file name argument and a Boolean overwrite argument. In this example, the file name is "CSV.txt" and the overwrite argument is set to true.

      <%

      set fso=Server.CreateObject("Scripting.FileSystemObject")

      set filename=fso.CreateTextFile("CSV.txt",true)

      %>

    • 4

      Write a line of text in CSV format to the file using the WriteLine() function. The text values in the string written to the text file must be separated by commas. In this example, the line written to the file is "value1,value2,value3" but you can write as many values as you wish.

      <%

      set fso=Server.CreateObject("Scripting.FileSystemObject")

      set filename=fso.CreateTextFile("CSV.txt",true)

      filename.WriteLine("value1,value2,value3")

      %>

    • 5

      Close the file, and destroy the text file variable and the File System Object. This final step is necessary to free up system resources.

      <%

      set fso=Server.CreateObject("Scripting.FileSystemObject")

      set filename=fso.CreateTextFile("CSV.txt",true)

      filename.WriteLine("value1,value2,value3")

      filename.Close

      set filename=nothing

      set fso=nothing

      %>

    • 6

      Upload the file to the server using the file management interface provided by your web host. When the page loads, the ASP code will run and a CSV file will be created.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured