-1

looking for solution in VBA not C# to export fom DataGridView my all columns and rows to .txt file comma or ":" separated values. Can not find anything on youtube or google, some topic will help me read and learn, thank you.

PS: I dont want to open it in excel or pdf print.

Brandon
  • 11
  • 7
  • http://stackoverflow.com/questions/6674555/export-gridview-data-into-csv-file Quick search on Stack overflow yields this. – GVashist Dec 05 '14 at 15:11
  • thx but I need VBA, VB .Net code, and sometimes there is problem with converting c# to vb .net, so I keep looking. AN its csv and not .txt :) – Brandon Dec 05 '14 at 15:28

1 Answers1

0

VB.NET code:

Protected Sub btnExportCSV_Click(sender As Object, e As EventArgs)
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.txt")
        Response.Charset = ""
        Response.ContentType = "text/plain"

        GridView1.AllowPaging = False
        GridView1.DataBind()

        Dim sb As New StringBuilder()
        For k As Integer = 0 To GridView1.Columns.Count - 1
            'add separator
            sb.Append(GridView1.Columns(k).HeaderText + ","c)
        Next
        'append new line
        sb.Append(vbCr & vbLf)
        For i As Integer = 0 To GridView1.Rows.Count - 1
            For k As Integer = 0 To GridView1.Columns.Count - 1
                'add separator
                sb.Append(GridView1.Rows(i).Cells(k).Text + ","c)
            Next
            'append new line
            sb.Append(vbCr & vbLf)
        Next
        Response.Output.Write(sb.ToString())
        Response.Flush()
        Response.End()
    End Sub
GVashist
  • 427
  • 4
  • 16
  • It doesnt even start as there is many missign references , Like Response I dont have anythign liek that and is it in RED error, then also if i change GridView1 to my DataGridView, it show blue underline error too, so i cant really use it. – Brandon Dec 06 '14 at 17:34
  • also it doesnt have any openfile dialog to ask me to save in pc, too, so i need the user select path in pc and then choose name by their own: http://postimg.org/image/q1frqpxu3/ – Brandon Dec 06 '14 at 17:35