You are here:   Home
Register   |  Login

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
Fast and Free Expedited Shipping on orders over $59 offer applies to Bookbyte inventory only - 160x600 banner

Recent Comments

Minimize
Re: Three Benefits to Using a Royalty-Free Image on Your Website or Blog
I also prefer to use royalty-free images. They give more credibility in your site or blog posts.
Re: Websites vs. Facebook Pages (Infographic)
Internet can be very useful for small business to market them self, there is always some free way to reach your audience via Internet.
Re: Is Keyword Density Still Important For On Page SEO
The search engines hadn’t quite caught up yet. As a result, it wasn’t uncommon to see sites rank highly when their content read something like this.that they follow SEO best practices than you will by trying to figure out a way to get your keywords into your page content another one or two times.
Re: Websites vs. Facebook Pages (Infographic)
Very interesting topic, infographics was a new concept for me, will definitely use it.
Re: Reducing Small Business Blues
Running a business is tough and you are right, most of the time you are the core aspect of each area of a business. I don't think I could handle doing my own accounting, like you I am too terrible with numbers!

u comment, i follow