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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to parse a csv...... a csv that has CRLF in the fields sso Java 20 04-26-2009 11:04 AM
read and write csv file using csv module jliu66 Python 0 10-19-2007 03:12 PM
How to move data from a CSV file to a JTable, and from a JTable to a CSV file ? Tintin92 Java 1 02-14-2007 06:51 PM
Re: csv writerow creates double spaced excel csv files Skip Montanaro Python 0 02-13-2004 08:50 PM
csv writerow creates double spaced excel csv files Michal Mikolajczyk Python 0 02-13-2004 08:38 PM



Advertisments