Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
Export data from datagrid to Excel
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.aspnet.datagridcontrol.


Mary
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...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
Harry Simpson
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...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
    
estagio sunviauto
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...
 
 
    
yamini
(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...
 
Email Archiving and Email Filing - what’s the difference?
Web-based task/todo list management
 
    
Praveen Jhurani
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...
 
Open source windows
The Law Society’s guidelines on e-mail management
 
    
Rishi Bhushan
(Type your message here)

--------------------------------
From: Rishi Bhushan
Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Rishi Bhushan
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...
 
Google Docs… no.
Twitter Elite
 
    
Manish Kaushik
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...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
Guillermo ?lvarez
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...
 
 
 
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.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734