Multimobile Development: Building Applications for any Smartphone
Export data from datagrid to Excel
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.aspnet.datagridcontrol.


Mary
GOOD ANSWER
The examples I found online of exporting data from a web page to Excel
either display the data in Excel from the page_load event or write the data
to an excel file and e-mail it to the user. I want to do this from a button
click event.

I display data in a datagrid and have a dropdownlist (server control) with a
go button below it where users can choose to export data to Excel, MS Word,
or text. When the user clicks the go button an OnServerClick event is
triggered and the aspx.vb code is called to respond to the event. I can't
get the event handler code to display the data in an Excel spreadsheet. A
blank window is displayed. Anybody have any thoughts on what I should
do???? Thanks for any help.

This is the code (modified slightly from an MSDN article on exporting
datagrid data from the page_load event):

Protected Sub btnExport_onServerClick(ByVal Sender As Object, ByVal E As
EventArgs) _

Handles btnExport.Click

' Excel Export code

Dim strResult As String = Request("ddlExport_Options")

If strResult = "Excel" Then

' Set the content type to Excel.

Response.ContentType = "application/vnd.ms-excel"

ElseIf strResult = "MS Word" Then

' Set the content type to MSWord.

Response.ContentType = "application/msword"

ElseIf strResult = "Text" Then

' Set the content type to MSWord.

Response.ContentType = "text/plain"

End If

' Remove the charset from the Content-Type header.

Response.Charset = ""

' Turn off the view state.

Me.EnableViewState = False

Dim tw As New System.IO.StringWriter()

Dim hw As New System.Web.UI.HtmlTextWriter(tw)

' Get the HTML for the control.

dgrORF.RenderControl(hw)

' Write the HTML back to the browser.

Response.Write(tw.ToString())

' End the response.

Response.End()

End Sub

Reply to this message...
Vote that this is a GOOD answer... (3 votes from other users already)
 
 
    
Harry Simpson
GOOD ANSWER
Mary,

Try this: button opens new window with another aspx within it.

Export to Excel

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

<INPUT style="Z-INDEX: 117; LEFT: 604px; WIDTH: 73px; POSITION: absolute;
TOP: 449px; HEIGHT: 24px" onclick="DoClick()" type="button" value="Export">

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

function DoClick()

{

window.open('LoadXLS.aspx','MyWindow','width=500,height=500');

}

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

LoadXLS.aspx code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' Set the content type to Excel.

Response.ContentType = "application/vnd.ms-excel"

' Remove the charset from the Content-Type header.

Response.Charset = ""

' Turn off the view state.

'Me.EnableViewState = False

Dim dsExport As DataSet = Session("dsExport")

Dim tw As New System.IO.StringWriter()

Dim hw As New System.Web.UI.HtmlTextWriter(tw)

Dim dgGrid As New DataGrid()

dgGrid.DataSource = dsExport

dgGrid.DataBind()

'dgGrid = Session("GridExport")

' Get the HTML for the control.

dgGrid.RenderControl(hw)

' Write the HTML back to the browser.

Response.Write(tw.ToString())

' End the response.

Response.End()

End Sub

You could probably do it any number of ways though.

HTH

Harry

"Mary" <Click here to reveal e-mail address> wrote in message
news:ekAfCL$NCHA.1256@tkmsftngp13...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
estagio sunviauto
GOOD ANSWER
I do this, and works.
The problem is that i use the css and then dont write the name of the textbox (textbox1.rendercontrol(h)) but write the css line. why?
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
yamini
GOOD ANSWER
(Type your message here)

Even i was trying to export data from datagrid to excel.
I tried this coding, but this doesn't work for me, since my datagrid has
paging and sorting option. I am not able to solve this problem, if any one knows
how to solve this, plz suggest me.Thanks.
--------------------------------
From: yamini
Reply to this message...
Vote that this is a GOOD answer... (6 votes from other users already)
 
 
    
Praveen Jhurani
GOOD ANSWER
Were you able to figure this one or get an answer on exporting datagrid with paging option enabled.

--------------------------------
From: Praveen Jhurani
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Rishi Bhushan
GOOD ANSWER
(Type your message here)

--------------------------------
From: Rishi Bhushan
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Rishi Bhushan
GOOD ANSWER
Even i was trying to export data from datagrid to excel.
I tried this coding, but this doesn't work for me, since my datagrid has
paging and sorting option. I am not able to solve this problem, if any one knows
how to solve this, plz suggest me.Thanks.

--------------------------------
From: Rishi Bhushan
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Manish Kaushik
GOOD ANSWER
Hi Friends

How can i do the same if i have to export the data of the datagrid control in VB.net form in windows 2000 platform

Please guide me

--------------------------------
From: Manish Kaushik
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Guillermo ?lvarez
GOOD ANSWER
I export data from datagrid yo excel. Ok.
But then I can't save the document into an excel file because in the window doesn't appear any menu bar.
Can you help me?
Thanks

--------------------------------
From: Guillermo ?lvarez
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
 
System.Data.DataSet
System.EventArgs
System.IO.StringWriter
System.Object
System.Web.UI.HtmlTextWriter
System.Web.UI.WebControls.DataGrid
System.Windows.Forms.DataGrid




Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734