export datagrid data to Excel file

G

Grey

is it possible to export excel directly from ASP.NET to Excel? My requirement is that the data on the datagrid is exported to MS Excel file after user clicks the button on the web page. Excel will be open automatically with imported data. If so, is need to add any additional controls or tools?


Million Thanks..

Eric Yum
 
P

pdm2

I have looked high and low for a simple solution to export a DataGrid to MS Excel in ASP.NET. I weeded out this code from Microsoft support. Simply create a button called ExportToExcel and place this code in the Click event and rename DataGrid1 in the code to whatever your datagrid is called. When you run the page and click your button you will be prompted to open excel in ie or save whatever data is in your datagrid to an excel file of your choice. I hope this saves beginner programmers such as myself some time and effort

sub ExportToExcel_Click(sender As Object, e As EventArgs

' 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 = Fals

Dim tw As New System.IO.StringWriter(
Dim hw As New System.Web.UI.HtmlTextWriter(tw
' Get the HTML for the control
DataGrid1.RenderControl(hw
' Write the HTML back to the browser
Response.Write(tw.ToString()
' End the response
Response.End(
End I

End Su
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top