[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 592
  • Last Modified:

Export Gridview to csv file - btnExportCSV_Click

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"
        objStreamWriter.WriteLine(headertext)
        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)
                Else
                    Str = (Me.uxItemDetailGrid.Rows(i).Cells(j).Text.ToString & ",")
                End If
                objStreamWriter.Write(Str)
            Next
            objStreamWriter.WriteLine()
        Next
        'Close the file.
        objStreamWriter.Close()


    End Sub
0
westdh
Asked:
westdh
1 Solution
 
Maheshwar RSoftware DeveloperCommented:
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
        GridView2.DataBind()
        Current.Response.Clear()
        Current.Response.Buffer = True
        Current.Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.csv", fileName))
        Current.Response.Charset = "HEllo"
        Current.Response.ContentType = "application/vnd.ms-excel"
        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")
        Next
        GridView2.RenderControl(hw)
        'style to format num bers to string
        ' Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"
        ' Current.Response.Write(style)
        Current.Response.Output.Write(sw.ToString())
        Current.Response.Flush()
        Current.Response.End()
    End Sub


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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now