Export datagrid to Excel with a lot of rows

A

Amirallia

Hello

Here's my code to export a datagrid to Exel :
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Dim monStringWriter As StringWriter = New StringWriter
Dim monHtmlTextWriter As HtmlTextWriter = New
HtmlTextWriter(monStringWriter)
monDG.RenderControl(monHtmlTextWriter)
Response.Write(monStringWriter.ToString())
Response.End()

It's work perfectly, but when my datagrid displays for exeample 10000
rows,
the export doesn't work.
Instead I have the page impossible to display !!!!

Any idea ?
 
G

Guest

You could write it out to disk and then redirect to it, providing the
directory the file is written to is a virtual one.
 
A

Amirallia

Demetri a formulé la demande :
You could write it out to disk and then redirect to it, providing the
directory the file is written to is a virtual one.

Do you have an example ?

Thanks
 
G

Guest

No tangible example but its not that hard.

You have a web folder setup where the file will be written out to. Make sure
permissions are setup for ASPNet User to write out the files there.

Your method that extracts the data from the grid then converts that data to
an excel format (most common is CSV) and then outputs it to a file in the web
folder mentioned above.

Then at the end of that method it does a Redirect to that file.
 
A

Amirallia

Demetri avait écrit le 31.07.2006 :
No tangible example but its not that hard.

You have a web folder setup where the file will be written out to. Make sure
permissions are setup for ASPNet User to write out the files there.

Your method that extracts the data from the grid then converts that data to
an excel format (most common is CSV) and then outputs it to a file in the web
folder mentioned above.

Then at the end of that method it does a Redirect to that file.

sorry but it makes the same thing, directly redirect to a white page.

If I put a break point in my code, when I click on the button, It'
doesn't stop... Here'is my code

Dim Separator As String = ";"
Dim lLigne, lCol As Integer
Dim item As DataGridColumn
Dim strValeur As String
Dim monStreamWriter As System.IO.StreamWriter = New
System.IO.StreamWriter(Server.MapPath("./") & "upload\xxx.csv")

Try
For Each item In dg.Columns
monStreamWriter.WriteLine(item.HeaderText & Separator)
Next item

monStreamWriter.WriteLine(ControlChars.NewLine)
For lLigne = 1 To Me.dg.Items.Count - 1
For lCol = 0 To Me.dg.Items(lLigne).Cells.Count - 1
If Me.dg.Items(lLigne).Cells(lCol).Text = " "
Then
strValeur = ""
Else
strValeur =
Me.dg.Items(lLigne).Cells(lCol).Text
End If
strValeur = strValeur & Separator
Next

monStreamWriter.WriteLine(strValeur & Separator)
monStreamWriter.WriteLine(ControlChars.NewLine)
Next

monStreamWriter.Close()

Response.Redirect("Upload\xxx.csv")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top