How to Build a Tab Delimited File From ASP

How to Build a Tab Delimited File From ASP thumbnail
ASP.net makes spreadsheets more intuitive and presentable.

Microsoft has its own programming language -- ASP.net, which stands for Active Server Pages. This application runs inside IIS (Internet Information Services), which is a free component of certain Microsoft operating systems, such as Windows 2000 and Windows NT 4.0. You can use ASP for various projects, such as creating reports and building Web pages. ASP can contain elements such as HTML tags and scripts, so it is very flexible and easy to use.

Instructions

    • 1

      Run Microsoft Visual Studio .NET. Point to "New" on the File menu, and then click "Project." When the New Project dialog pops up, click "Visual Basic Project" from the Project Types, and then under Templates, click "ASP.NET Web Application."

    • 2

      Type "ExcelVBTest" in the Name box and click the appropriate server in the Location box. Leave the server name as "http://localhost," if your files are housed in your local server, and click "OK." Drag a "HyperLink" control from the toolbox to the Web form in the WebForm1.aspx file. Right-click the "WebForm1.aspx" file, and then click "View Code" to display your project's source code.

    • 3

      Paste the code below to the top of the page:

      Imports System.Data

      Imports System.Data.Common

      Imports System.Data.SqlClient

      Imports System.IO

      Imports System.Text

      And then include the succeeding code to the "Page_Load" event:

      Dim i As Integer

      Dim strLine As String, filePath, fileName, fileExcel, link

      Dim objFileStream As FileStream

      Dim objStreamWriter As StreamWriter

      Dim nRandom As Random = New Random(DateTime.Now.Millisecond)

      Dim fs As Object, myFile As Object

      Dim cnn As SqlConnection = New SqlConnection("server=(local);database=pubs;" & _

      "Integrated Security=SSPI")

      'Create a pseudo-random file name.

      fileExcel = "t" & nRandom.Next().ToString() & ".xls"

      'Set a virtual folder to save the file.

      'Make sure that you change the application name to match your folder.

      filePath = Server.MapPath("\ExcelVBTest")

      fileName = filePath & "\" & fileExcel

      'Use FileStream to create the .xls file.

      objFileStream = New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)

      objStreamWriter = New StreamWriter(objFileStream)

      'Use a DataReader to connect to the Pubs database.

      cnn.Open()

      Dim sql As String = "select au_id,au_lName,au_fname,phone," & _

      "address,city,state,zip,contract from authors"

      Dim cmd As SqlCommand = New SqlCommand(sql, cnn)

      Dim dr As SqlDataReader

      dr = cmd.ExecuteReader()

      'Enumerate the field names and records that are used to build the file.

      For i = 0 To dr.FieldCount - 1

      strLine = strLine & dr.GetName(i).ToString & Chr(9)

      Next

      'Write the field name information to file.

      objStreamWriter.WriteLine(strLine)

      'Reinitialize the string for data.

      strLine = ""

      'Enumerate the database that is used to populate the file.

      While dr.Read()

      For i = 0 To dr.FieldCount - 1

      strLine = strLine & dr.GetValue(i) & Chr(9)

      Next

      objStreamWriter.WriteLine(strLine)

      strLine = ""

      End While

      'Clean up.

      dr.Close()

      cnn.Close()

      objStreamWriter.Close()

      objFileStream.Close()

      'Show a link to the Excel file.

      HyperLink1.Text = "Open Excel"

      HyperLink1.NavigateUrl = fileExcel

    • 4

      Click "Save All" on the File menu to save the project files. Click "Build" to create the project on the Build menu in the Visual Studio .NET Integrated Development Environment. Right-click the "WebForm1.aspx" form in Visual Studio .NET Integrated Development Environment Solution Explorer, and run the code by clicking "View in Browser."

Tips & Warnings

  • Microsoft offers free online tutorials on the uses of ASP.net and Visual Basic. Also, join discussion forums to enable you to seek advice and guidance from expert ASP users.

Related Searches:

References

Resources

  • Photo Credit Stockbyte/Stockbyte/Getty Images

Comments

Related Ads

Featured