Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Gridview to CSV

Reply
Thread Tools

Gridview to CSV

 
 
Vincent
Guest
Posts: n/a
 
      08-24-2006
Hi, I use the following code to export to Excel, which works fine.

GVAPInv.AllowSorting = "False"
Gridvew1.DataBind()
Gridvew1.AllowSorting = "False"
Gridvew1.DataBind()
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Dim frm As HtmlForm = New HtmlForm()
Me.Controls.Add(frm)
frm.Controls.Add(Gridvew1)
frm.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
Gridvew1.AllowSorting = "False"
Gridvew1.DataBind()

I'm having trouble figuring out how to export to a simple Csv file.
I've tried to change the response.contenttype to:

Response.ContentType = "text/csv"

This only generates html code (pasted sample below). Any help would be
appreciated. Thank you.

 
Reply With Quote
 
 
 
 
Siva M
Guest
Posts: n/a
 
      08-24-2006
See if this thread helps: http://forums.asp.net/thread/1343504.aspx

"Vincent" <> wrote in message
news: ps.com...
Hi, I use the following code to export to Excel, which works fine.

GVAPInv.AllowSorting = "False"
Gridvew1.DataBind()
Gridvew1.AllowSorting = "False"
Gridvew1.DataBind()
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Dim frm As HtmlForm = New HtmlForm()
Me.Controls.Add(frm)
frm.Controls.Add(Gridvew1)
frm.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
Gridvew1.AllowSorting = "False"
Gridvew1.DataBind()

I'm having trouble figuring out how to export to a simple Csv file.
I've tried to change the response.contenttype to:

Response.ContentType = "text/csv"

This only generates html code (pasted sample below). Any help would be
appreciated. Thank you.

 
Reply With Quote
 
 
 
 
Vincent
Guest
Posts: n/a
 
      08-24-2006
Thanks that pointed me in the right direction and I've gotten this to
work...

Here's the code I used:

*******************************************
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("c:\myfiles\mycsvfile.csv")
'Write text.

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

Dim headertext =
"field1,field2,field3,field4,field5,field5,fie ld6"
objStreamWriter.WriteLine(headertext)
For i = 0 To (Me.GridView2.Rows.Count - 1)
For j = 0 To (Me.GridView2.Columns.Count - 1)

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

************************************************** *****

Siva M wrote:
> See if this thread helps: http://forums.asp.net/thread/1343504.aspx
>
> "Vincent" <> wrote in message
> news: ps.com...
> Hi, I use the following code to export to Excel, which works fine.
>
> GVAPInv.AllowSorting = "False"
> Gridvew1.DataBind()
> Gridvew1.AllowSorting = "False"
> Gridvew1.DataBind()
> Response.ContentType = "application/vnd.ms-excel"
> Response.Charset = ""
> Me.EnableViewState = False
> Dim tw As New System.IO.StringWriter()
> Dim hw As New System.Web.UI.HtmlTextWriter(tw)
> Dim frm As HtmlForm = New HtmlForm()
> Me.Controls.Add(frm)
> frm.Controls.Add(Gridvew1)
> frm.RenderControl(hw)
> Response.Write(tw.ToString())
> Response.End()
> Gridvew1.AllowSorting = "False"
> Gridvew1.DataBind()
>
> I'm having trouble figuring out how to export to a simple Csv file.
> I've tried to change the response.contenttype to:
>
> Response.ContentType = "text/csv"
>
> This only generates html code (pasted sample below). Any help would be
> appreciated. Thank you.


 
Reply With Quote
 
jondack jondack is offline
Junior Member
Join Date: Nov 2010
Posts: 2
 
      03-14-2011
Check out this blog post on the subject. I found exporting to CSV more efficient and product neutral than excel:

http://www.inspiredbytechnology.com/...-2010-webpart/

Thanks

Jon
http://www.inspiredbytechnology.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView's selection isn't causing other GridView's to load data asexpected Rasika WIJAYARATNE ASP .Net 0 12-13-2007 11:25 PM
Save Child GridView only when parent GridView Saved crpietschmann ASP .Net Web Controls 5 09-07-2006 01:58 PM
Deleting row from GridView causes ArgumentOutOfRangeException during System.Web.UI.WebControls.GridView.set_SelectedIndex(Int32 value) loga123 ASP .Net 2 06-28-2006 08:32 PM
GRIDVIEW: Effecting buttonfields on single gridview rows =?Utf-8?B?TWFya0F1cml0?= ASP .Net 0 05-23-2006 08:11 PM
GridView Hierarchical View - Gridview in Gridview =?Utf-8?B?bWdvbnphbGVzMw==?= ASP .Net 1 05-09-2006 06:48 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57