You are here:   Home
Register   |  Login
Call Now: 877-370-3646

Captured Technology - Blog

Minimize

Exporting a DataSet to a CSV File in ASP.Net

Oct 2

Written by:
10/2/2009 3:50 PM  RssIcon

Exporting a DataSet to a CSV File in ASP.Net

Save up to 90% + GET FREE SHIPPING!

 

 

 

 

 

 

 

If you have the need to export data from a SQL Server Dataset to a CSV file through your website application, you can write to the response object in the Page_Init function of your page to produce the file. The user will not see the page at all but will be prompted to open or save the file on their desktop.

Here is the Page_Init function with the Response Write that you will need:

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        Dim objData As System.Data.DataSet = New System.Data.DataSet

        Dim objCSV As String = BuildCSV(objData)
        Dim objFilename As String = "Excel" & Today.ToString("yyyy-mm-dd") & ".csv"

        With Response
            .AddHeader("Content-disposition", "attachment;filename=" & objFilename)
            .ContentType = "text/plain"
            .Write(objCSV)
            .End()
        End With
        
    End Sub

And here is the supporting function to return the DataSet as a String:

Public Shared Function BuildCSV(ByRef objData As System.Data.DataSet) As String

        Dim objList As System.Data.DataSet = objData
        Dim objCSVOverall As New System.Text.StringBuilder
        Dim objCSVRow As New System.Text.StringBuilder

        '*** Build the header row
        For intCount As Integer = 0 To objList.Tables(0).Columns.Count - 1
            Dim CurrentColumn As System.Data.DataColumn = objList.Tables(0).Columns(intCount)
            objCSVRow.Append(Chr(34) & CurrentColumn.ColumnName & Chr(34) & ",")
        Next

        objCSVOverall.AppendLine(objCSVRow.ToString.Substring(0, objCSVRow.ToString.Length - 1))

        '*** iterate through the rows
        For Each CurrentRow As System.Data.DataRow In objList.Tables(0).Rows
            objCSVRow = New System.Text.StringBuilder

            For intCount As Integer = 0 To objList.Tables(0).Columns.Count - 1
                Dim objValue As String = CurrentRow.Item(intCount).ToString
                Dim CurrentColumn As System.Data.DataColumn = objList.Tables(0).Columns(intCount)

                objCSVRow.Append(Chr(34) & objValue.Replace(Chr(34), "'") & Chr(34) & ",")
            Next

            objCSVOverall.AppendLine(objCSVRow.ToString.Substring(0, objCSVRow.ToString.Length - 1))
        Next

        Return objCSVOverall.ToString
    End Function

Grad Pic 468x60

Tags:
Categories:

1 comment(s) so far...


Gravatar

Re: Exporting a DataSet to a CSV File in ASP.Net

ASP.Net and .net framework provides many ways to deal with Data , many times in web scenario , we need to export data back to user , this scenario ranges from Some custom addressbook entries to custom lists and other complex transaction details , which are a result of persistant data or dta in memory.

By Venice Hotels on   8/18/2010 4:28 AM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 

FaceBook

Minimize

Mobile Version

Minimize
Add CapturedTech - Technology Mippin widget

Translate

Minimize

Sponsors

Minimize

Recent Comments

Minimize
Re: Three Benefits to Using a Royalty-Free Image on Your Website or Blog
Don't pay for royalty free images. They are free after all! I offer links to 96 websites that have tens of thousands (maybe even hundreds of thousands) of absolutely free images, including ones for commercial use! All you need to do is subscribe to my blog at www.davesinternetmarketing.info/royalty-free-images-free-for-commercial-use/
Re: Buzzom Offers Featured User Options
This is when discovered this great new way to grow your follow list. You can pay between $10 and $100 for the number of times you want to app ar at the top of the list.
Re: Microsoft Test Bounties
I had spent a good deal of my time looking for someone to explain this subject clearly and I have really enjoyed reading your blog posts and I collected a lot of interesting things as well as I done a research on the subject and learn most peoples will agree with your blog.
Re: 10 Ways to Source Great Sales Leads
Hoovers.com, that have contact information for tens of millions of businesses. If necessary, you can work with these companies' research teams to order lists specifically tailored to your organization's sales needs.
Re: Microsoft Test Bounties
To find bugs and showing it to microsoft is quite admirable. Keep blogging more i will appreciate.

u comment, i follow