Export Gridview to csv file - btnExportCSV_Click

Posted on 2009-12-17
Last Modified: 2012-05-08
I am having problem with this line I am trying to pput the file on the desk top

Line 144:        'make sure this is a path that you have permissions to save in
Line 145:
Line 146:        objStreamWriter = New IO.StreamWriter("..\mycsvfile.csv")
Line 147:        'Write text.
Line 148:
Protected Sub btnExportCSV_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim objStreamWriter As IO.StreamWriter

        'Pass the file path and the file name to the StreamWriter constructor.
        'make sure this is a path that you have permissions to save in

        objStreamWriter = New IO.StreamWriter("~\mycsvfile.csv")
        'Write text.

        Dim Str As String
        Dim i As Integer
        Dim j As Integer

        Dim headertext = "field1,field2,field3,field4,field5,field5,field6"
        For i = 0 To (Me.uxItemDetailGrid.Rows.Count - 1)
            For j = 0 To (Me.uxItemDetailGrid.Columns.Count - 1)

                'this IF statement stops it from adding a comma after the last field
                If j = (Me.uxItemDetailGrid.Columns.Count - 1) Then
                    Str = (Me.uxItemDetailGrid.Rows(i).Cells(j).Text.ToString)
                    Str = (Me.uxItemDetailGrid.Rows(i).Cells(j).Text.ToString & ",")
                End If
        'Close the file.

    End Sub
Question by:westdh
    1 Comment
    LVL 4

    Accepted Solution

    try the follwing code

     Public Shared Sub ExportToCSVl(ByVal fileName As String, ByVal query As String)
            Dim cmd As MySqlCommand
            cmd = New MySqlCommand(query)
            Dim dt As DataTable = GetData(cmd)
            'Create a dummy GridView
            Dim GridView2 As New GridView()
            GridView2.AllowPaging = False
            GridView2.DataSource = dt
            Current.Response.Buffer = True
            Current.Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.csv", fileName))
            Current.Response.Charset = "HEllo"
            Current.Response.ContentType = "application/"
            Dim sw As New StringWriter()
            Dim hw As New HtmlTextWriter(sw)
            For i As Integer = 0 To GridView2.Rows.Count - 1
                'Apply text style to each Row
                GridView2.Rows(i).Attributes.Add("class", "textmode")
            'style to format num bers to string
            ' Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"
            ' Current.Response.Write(style)
        End Sub

    u just have to pass the filename and the query thats all

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
    For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now