How would I take data that I am displaying in a gridview on an asp.net page and export it to a excel spread sheet.This all has to be done with vb.net and asp.net.
The whole point is so that the user can view a phone list in a company and then from that page print the entire phone list.How do I export data from a gridview to an excel spreadsheet?
You'd be surprised how easy this is..... If you've ever copied and pasted a table in an HTML document into Excel, you would notice it copies perfectly, with preserved formatting and everything. Turns out you can just render the GridView with its data into an Excel file response stream, and it just works:
Response.AddHeader( ';content-disposition'; , ';attachment;filename=data.xls'; )
Response.Charset = ';';
Response.ContentType = ';application/vnd.xls';
Dim dt As DataTable = CType( ViewState.Item( ';ExportDataTable'; ), DataTable)
Dim gridView As New GridView()
gridView.DataSource = dt
gridView.DataBind()
Dim sr As New StringWriter()
Dim hr As New HtmlTextWriter(sr)
gridView.RenderControl(hr)
Dim result As String = sr.ToString()
Response.Write(result)
Response.End()
The DataTable that I am rendering was placed in the viewstate, this will popup a dialog box to download the Excel file.How do I export data from a gridview to an excel spreadsheet?
You can make export file and in file you can save all data in between you must put '; ; '; and file save like '; .cvs ';.
I was ones make like this in javascript and then I was open this file in excel and it was work.
No comments:
Post a Comment