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 = ""
' 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